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(Component comp) 

Source Link

Document

Appends the specified component to the end of this container.

Usage

From source file:Main.java

public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel pnl = new JPanel();
    pnl.add(new MyButton());
    frame.add(pnl);
    frame.setSize(600, 600);// w  ww.ja v a 2 s. c  o m
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    JRadioButton button1 = new JRadioButton("Red");
    JRadioButton button2 = new JRadioButton("Green");

    button2.setMnemonic(KeyEvent.VK_G);

    JRadioButton button3 = new JRadioButton("Blue");
    ButtonGroup colorButtonGroup = new ButtonGroup();
    colorButtonGroup.add(button1);//from   w w  w.  ja  v a  2 s  .com
    colorButtonGroup.add(button2);
    colorButtonGroup.add(button3);
    button1.setSelected(true);

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

    frame.add(new JLabel("Color:"));
    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    frame.pack();
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] argv) {
    JTextComponent textcomp = new JTextArea();
    final UndoManager undo = new UndoManager();
    Document doc = textcomp.getDocument();

    doc.addUndoableEditListener(new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent evt) {
            undo.addEdit(evt.getEdit());
        }//from w  w w  .ja v a 2s . com
    });

    textcomp.getActionMap().put("Undo", new AbstractAction("Undo") {
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canUndo()) {
                    undo.undo();
                }
            } catch (CannotUndoException e) {
            }
        }
    });

    textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");

    textcomp.getActionMap().put("Redo", new AbstractAction("Redo") {
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canRedo()) {
                    undo.redo();
                }
            } catch (CannotRedoException e) {
            }
        }
    });

    textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(textcomp));
    frame.setSize(380, 320);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JLabel label = new JLabel("java2s.com");

    JFrame frame = new JFrame("Testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(label);

    JPanel buttons = new JPanel(new GridLayout(0, 1));
    for (int index = 0; index < 10; index++) {
        buttons.add(new JButton(String.valueOf(index)));
    }//from   w ww. ja  v a  2  s  .co m

    JPanel right = new JPanel(new BorderLayout());
    right.add(buttons, BorderLayout.NORTH);
    frame.add(right, BorderLayout.EAST);

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

From source file:Main.java

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

    JPanel mainPanel = new JPanel();
    JPanel buttonsPanel = new JPanel();
    frame.add(mainPanel);
    frame.add(buttonsPanel, BorderLayout.SOUTH);

    String[] options = { "S", "G", "I", "T" };

    JComboBox comboBox = new JComboBox(options);
    comboBox.setRenderer(new MyComboBoxRenderer("COUNTRY"));
    comboBox.setSelectedIndex(-1);//from  ww w  .  j  a  v a 2 s  .c  o m
    mainPanel.add(comboBox);

    JButton clearSelectionButton = new JButton("Clear selection");
    clearSelectionButton.addActionListener(e -> {
        comboBox.setSelectedIndex(-1);
    });
    buttonsPanel.add(clearSelectionButton);

    frame.pack();
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new TestPane());
    frame.pack();// w w  w .j ava 2 s. c  o  m
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTextArea ta = new JTextArea(20, 20);
    ((AbstractDocument) ta.getDocument()).setDocumentFilter(new MyFilter());

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new JScrollPane(ta));
    frame.pack();/*from   ww  w  . j  a va  2 s .  c  o m*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new BlinkPane());
    frame.setSize(200, 200);/* ww w.j  a va 2s.  c o m*/
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final JTextPane tp = new JTextPane();
    JButton withFocus = new JButton("Select with focus");
    tp.addMouseListener(new MouseAdapter() {

        @Override/*  w  w  w  . j  a v  a  2  s . com*/
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
                tp.selectAll();
            }
        }

    });

    JFrame frame = new JFrame("Testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new JScrollPane(tp));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String avg[]) throws Exception {
    BufferedImage img = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));
    ImageIcon icon = new ImageIcon(img);
    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setSize(200, 300);//from w  w w. j a v  a 2  s  .com
    JLabel lbl = new JLabel();
    lbl.setIcon(icon);
    frame.add(lbl);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}