Example usage for javax.swing JFrame add

List of usage examples for javax.swing JFrame add

Introduction

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

Prototype

public Component add(String name, Component comp) 

Source Link

Document

Adds the specified component to this container.

Usage

From source file:Main.java

public static void main(String[] args) {
    String[] items = new String[] { "One", "Two", "Three", "Four" };
    JList<String> list = new JList<>(items);
    JFrame f = new JFrame();
    f.add(list, BorderLayout.CENTER);
    JButton btn = new JButton("Get selected");
    btn.addActionListener(new ActionListener() {
        @Override/*from   w  w w . j  a va2  s  .c  o  m*/
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showConfirmDialog(f, "You Selected : " + list.getSelectedValue(), "Display",
                    JOptionPane.PLAIN_MESSAGE);
        }
    });

    f.add(btn, BorderLayout.SOUTH);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JToolBar toolbar = new JToolBar(JToolBar.VERTICAL);

    JButton selectb = new JButton(new ImageIcon("select.gif"));
    JButton freehandb = new JButton(new ImageIcon("freehand.gif"));
    JButton shapeedb = new JButton(new ImageIcon("shapeed.gif"));
    JButton penb = new JButton(new ImageIcon("pen.gif"));

    toolbar.add(selectb);/*www.  ja  v  a  2s.c  o  m*/
    toolbar.add(freehandb);
    toolbar.add(shapeedb);
    toolbar.add(penb);

    JFrame f = new JFrame();
    f.add(toolbar, BorderLayout.WEST);

    f.setSize(250, 350);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    final JTextField textField = new JTextField(5);

    textField.addFocusListener(new FocusListener() {
        public void focusGained(FocusEvent e) {
        }//from w  ww .j  a  v  a  2 s.co  m

        public void focusLost(FocusEvent e) {
            if (!e.isTemporary()) {
                String content = textField.getText();
                if (!content.equals("a")) {
                    System.out.println("illegal value! " + content);
                    SwingUtilities.invokeLater(new FocusGrabber(textField));
                }
            }
        }
    });
    JFrame frame = new JFrame();
    frame.add(textField, BorderLayout.CENTER);
    frame.setSize(300, 300);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String args[]) {
    Main anim = new Main();
    JFrame app = new JFrame("Animator test");
    app.add(anim, BorderLayout.CENTER);
    app.setSize(300, 300);/*from  ww  w.  jav  a  2 s .  com*/
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    app.setSize(anim.getPreferredSize().width + 10, anim.getPreferredSize().height + 30);
}

From source file:Toolbars.java

public static void main(String[] args) {
    JToolBar toolbar1 = new JToolBar();
    JToolBar toolbar2 = new JToolBar();

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    JButton newb = new JButton(new ImageIcon("new.png"));
    JButton openb = new JButton(new ImageIcon("open.png"));
    JButton saveb = new JButton(new ImageIcon("save.png"));

    toolbar1.add(newb);/*from ww w. j  a  v  a2 s . co m*/
    toolbar1.add(openb);
    toolbar1.add(saveb);
    toolbar1.setAlignmentX(0);

    JButton exitb = new JButton(new ImageIcon("exit.png"));
    toolbar2.add(exitb);
    toolbar2.setAlignmentX(0);

    exitb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });

    panel.add(toolbar1);
    panel.add(toolbar2);

    JFrame f = new JFrame();
    f.add(panel, BorderLayout.NORTH);

    f.setSize(300, 200);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

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

    frame.add(new Main(), BorderLayout.CENTER);
    frame.setSize(300, 300);//  w  ww.  j av a  2  s.co  m
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame("Test");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(createLogin(), BorderLayout.CENTER);
    JLabel admonition = new JLabel("CopyRight java2s.com.", JLabel.CENTER);
    f.add(admonition, BorderLayout.SOUTH);
    f.pack();/*from www  .  ja  v a2  s.c  om*/
    f.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame f = new JFrame("JToggleButton Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JToggleButton("North"), BorderLayout.NORTH);
    f.add(new JToggleButton("East"), BorderLayout.EAST);
    f.add(new JToggleButton("West"), BorderLayout.WEST);
    f.add(new JToggleButton("Center"), BorderLayout.CENTER);
    f.add(new JToggleButton("South"), BorderLayout.SOUTH);
    f.setSize(300, 200);//from w w  w.j a  v a 2  s . com
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();

    JTextArea textArea = new JTextArea();
    f.add(new JScrollPane(textArea), BorderLayout.CENTER);

    Timer timer = new Timer(1000, new ActionListener() {
        @Override/*from  w  ww.j av a  2 s .  co  m*/
        public void actionPerformed(ActionEvent e) {
            textArea.append("bla");
        }
    });
    timer.setRepeats(true);
    timer.start();
    JButton button = new JButton("Click me");
    button.addActionListener(e -> {
        System.out.println("Before option pane");
        JOptionPane.showMessageDialog(f, "A message dialog");
        System.out.println("After option pane");
    });
    f.add(button, BorderLayout.SOUTH);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Name: ");
    label.setDisplayedMnemonic(KeyEvent.VK_N);
    JTextField textField = new JTextField();
    label.setLabelFor(textField);/*from w  w w.  java 2  s. c  o  m*/
    panel.add(label, BorderLayout.WEST);
    panel.add(textField, BorderLayout.CENTER);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(panel, BorderLayout.NORTH);
    frame.add(new JButton("Somewhere Else"), BorderLayout.SOUTH);
    frame.setSize(250, 150);
    frame.setVisible(true);
}