Example usage for javax.swing JFrame JFrame

List of usage examples for javax.swing JFrame JFrame

Introduction

In this page you can find the example usage for javax.swing JFrame JFrame.

Prototype

public JFrame(String title) throws HeadlessException 

Source Link

Document

Creates a new, initially invisible Frame with the specified title.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("JFrame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField nameTextField = new JTextField();
    JLabel nameLabel = new JLabel("Name:");
    nameLabel.setDisplayedMnemonic('N');
    nameLabel.setLabelFor(nameTextField);

    frame.add(nameLabel, BorderLayout.WEST);
    frame.add(nameTextField, BorderLayout.CENTER);

    frame.pack();//w w  w  .  j a v  a2s.  com
    frame.setVisible(true);
}

From source file:BasicArrowButtonSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Popup JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new GridLayout(0, 5));
    frame.add(new BasicArrowButton(BasicArrowButton.EAST));
    frame.add(new BasicArrowButton(BasicArrowButton.NORTH));
    frame.add(new BasicArrowButton(BasicArrowButton.SOUTH));
    frame.add(new BasicArrowButton(BasicArrowButton.WEST));
    frame.setSize(300, 200);/*from  ww w  .j  av a2 s.c  om*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JButton close = new JButton("Close me programmatically");
    final JFrame f = new JFrame("Close Me");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(close);/*w w  w  .j a  v  a 2s . c o m*/
    close.addActionListener(e -> {
        f.dispose();
    });
    f.pack();
    f.setLocationByPlatform(true);
    f.setVisible(true);
}

From source file:MainClass.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(0, 1));
    JTextField textField = new JTextField("Left");
    textField.setHorizontalAlignment(JTextField.LEFT);
    frame.add(textField);/*from  w  ww . j a  v  a  2 s .  c o m*/
    textField = new JTextField("Center");
    textField.setHorizontalAlignment(JTextField.CENTER);
    frame.add(textField);
    textField = new JTextField("Right");
    textField.setHorizontalAlignment(JTextField.RIGHT);
    frame.add(textField);
    textField = new JTextField("Leading");
    textField.setHorizontalAlignment(JTextField.LEADING);
    frame.add(textField);
    textField = new JTextField("Trailing");
    textField.setHorizontalAlignment(JTextField.TRAILING);
    frame.add(textField);
    frame.pack();
    frame.setSize(250, (int) frame.getSize().getHeight());
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame("Ancestor Sampler");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    AncestorListener ancestorListener = new AncestorListener() {
        public void ancestorAdded(AncestorEvent ancestorEvent) {
            System.out.println("Added");
        }/*from   www  .j av  a2 s.c  o  m*/

        public void ancestorMoved(AncestorEvent ancestorEvent) {
            System.out.println("Moved");
        }

        public void ancestorRemoved(AncestorEvent ancestorEvent) {
            System.out.println("Removed");
        }
    };
    f.getRootPane().addAncestorListener(ancestorListener);
    f.getRootPane().setVisible(false);
    f.getRootPane().setVisible(true);
    f.setSize(300, 200);
    f.setVisible(true);
}

From source file:TryGridLayout.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Grid Layout");
    aWindow.setBounds(30, 30, 300, 300);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridLayout grid = new GridLayout(3, 4, 30, 20);
    Container content = aWindow.getContentPane();
    content.setLayout(grid);// w ww . ja  v a2s .c  om
    JButton button = null;
    for (int i = 1; i <= 10; i++) {
        content.add(button = new JButton(" Press " + i));
    }
    aWindow.pack();
    aWindow.setVisible(true);
}

From source file:CreatingSerifItalicFont.java

public static void main(String args[]) {
    JFrame f = new JFrame("JColorChooser Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JButton button = new JButton("Pick to Change Background");

    Font myFont = new Font("Serif", Font.ITALIC, 12);

    button.setFont(myFont);/*from www  .ja v  a2  s . c  om*/

    f.add(button, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Sample Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border emptyBorder = new EmptyBorder(5, 10, 5, 10);

    JLabel aLabel = new JLabel("Bevel");
    aLabel.setBorder(emptyBorder);//from   w w w . j a va2 s.  c  om
    aLabel.setHorizontalAlignment(JLabel.CENTER);

    frame.add(aLabel);
    frame.setSize(400, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("JFileChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JFileChooser fileChooser = new JFileChooser(".");
    ActionListener[] lis = fileChooser.getActionListeners();

    frame.add(fileChooser, BorderLayout.CENTER);

    frame.pack();/*w  w  w  .  j ava  2  s  .com*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("SpringLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    // Set the content pane's layout as SpringLayout
    SpringLayout springLayout = new SpringLayout();
    contentPane.setLayout(springLayout);

    // Add two JButtons to the content pane
    JButton b1 = new JButton("Button 1");
    JButton b2 = new JButton("Little Bigger   Button   2");
    contentPane.add(b1);/*  w  ww . ja v  a  2s .  c om*/
    contentPane.add(b2);

    frame.pack();
    frame.setVisible(true);
}