Example usage for java.awt Container add

List of usage examples for java.awt Container add

Introduction

In this page you can find the example usage for java.awt Container add.

Prototype

public Component add(Component comp) 

Source Link

Document

Appends the specified component to the end of this container.

Usage

From source file:MnemonicSample.java

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

    Container content = frame.getContentPane();
    content.setLayout(new GridLayout(1, 0));

    JButton button1 = new JButton("Warning");
    button1.setMnemonic(KeyEvent.VK_W);
    content.add(button1);

    JButton button2 = new JButton("Warning");
    button2.setMnemonic(KeyEvent.VK_H);
    content.add(button2);/*ww w .  jav  a  2  s  .  c  om*/

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:FileTree.java

/** Main: make a Frame, add a FileTree */
public static void main(String[] av) {

    JFrame frame = new JFrame("FileTree");
    frame.setForeground(Color.black);
    frame.setBackground(Color.lightGray);
    Container cp = frame.getContentPane();

    if (av.length == 0) {
        cp.add(new FileTree(new File(".")));
    } else {/*from  w  w  w. j a  v a2s  .co  m*/
        cp.setLayout(new BoxLayout(cp, BoxLayout.X_AXIS));
        for (int i = 0; i < av.length; i++)
            cp.add(new FileTree(new File(av[i])));
    }

    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:LabelJarSample.java

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

    Container content = frame.getContentPane();
    content.setLayout(new GridLayout(2, 2));

    JLabel label1 = new JLabel("Text Label");
    content.add(label1);

    Image warnImage = ImageLoader.getImage(LabelJarSample.class, "Warn.gif");
    Icon warnIcon = new ImageIcon(warnImage);
    JLabel label2 = new JLabel(warnIcon);
    content.add(label2);// ww w.  j  a  v  a 2 s.c  o m

    JLabel label3 = new JLabel("Warning", warnIcon, JLabel.CENTER);
    content.add(label3);

    String htmlLabel = "<html><sup>HTML</sup> <sub><em>Label</em></sub><br>"
            + "<font color=\"#FF0080\"><u>Multi-line</u></font>";
    JLabel label4 = new JLabel(htmlLabel);
    content.add(label4);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:IconCheckBoxSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Iconizing CheckBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Icon checked = new DiamondIcon(Color.black, true);
    Icon unchecked = new DiamondIcon(Color.black, false);
    JCheckBox aCheckBox1 = new JCheckBox("Pizza", unchecked);
    aCheckBox1.setSelectedIcon(checked);
    JCheckBox aCheckBox2 = new JCheckBox("Calzone");
    aCheckBox2.setIcon(unchecked);/*w w w  .ja v  a  2  s . c  o m*/
    aCheckBox2.setSelectedIcon(checked);
    Icon checkBoxIcon = new CheckBoxIcon();
    JCheckBox aCheckBox3 = new JCheckBox("Anchovies", checkBoxIcon);
    JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust", checked);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(0, 1));
    contentPane.add(aCheckBox1);
    contentPane.add(aCheckBox2);
    contentPane.add(aCheckBox3);
    contentPane.add(aCheckBox4);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:FocusCycleSample.java

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

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(3, 3));
    for (int i = 0; i < 8; i++) {
        JButton button = new JButton("" + i);
        contentPane.add(button);
    }/*www.j a  v a 2 s  .com*/

    JPanel panel = new FocusCycleConstrainedJPanel();
    panel.setLayout(new GridLayout(1, 3));
    for (int i = 0; i < 3; i++) {
        JButton button = new JButton("" + (i + 3));
        panel.add(button);
    }
    contentPane.add(panel);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:FlowLayoutSettingGaps.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Flow Layout");
    aWindow.setBounds(30, 30, 300, 300); // Size
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout flow = new FlowLayout(); // Create a layout manager
    flow.setHgap(35); // Set the horizontal gap
    Container content = aWindow.getContentPane(); // Get the content pane
    content.setLayout(flow); // Set the container layout mgr
    // Now add six button components
    for (int i = 1; i <= 6; i++) {
        content.add(new JButton("Press " + i)); // Add a Button to content pane
    }//from  w w w  .j a v  a 2s  . c  o  m
    aWindow.setVisible(true); // Display the window
}

From source file:Main.java

public static void main(String[] args) {

    JFrame frame = new JFrame("JFrame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JButton counterButton = new JButton("Clicked #0");
    JButton closeButton = new JButton("Close");
    frame.setLayout(new FlowLayout());
    contentPane.add(closeButton);
    contentPane.add(counterButton);/*from ww w. ja va2 s. c om*/

    counterButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            counterButton.setText("Clicked #" + counter++);
        }
    });

    closeButton.addActionListener(e -> System.exit(0));

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

From source file:SpinnerDemo.java

public static void main(String[] args) {
    JFrame jf = new JFrame("It Spins");
    Container cp = jf.getContentPane();
    cp.setLayout(new GridLayout(0, 1));

    // Create a JSpinner using one of the pre-defined SpinnerModels
    JSpinner dates = new JSpinner(new SpinnerDateModel());
    cp.add(dates);

    // Create a JSPinner using a SpinnerListModel. 
    String[] data = { "One", "Two", "Three" };
    JSpinner js = new JSpinner(new SpinnerListModel(data));
    cp.add(js);//from w w w  .ja  v a2 s.  co m

    jf.setSize(100, 80);
    jf.setVisible(true);
}

From source file:AlignmentSample.java

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

From source file:SpringFormTest.java

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

    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(layout);/*from  ww w.j av  a 2  s. com*/

    Component left = new JLabel("Left");
    Component right = new JTextField(15);

    contentPane.add(left);
    contentPane.add(right);

    layout.putConstraint(SpringLayout.WEST, left, 10, SpringLayout.WEST, contentPane);
    layout.putConstraint(SpringLayout.NORTH, left, 25, SpringLayout.NORTH, contentPane);
    layout.putConstraint(SpringLayout.NORTH, right, 25, SpringLayout.NORTH, contentPane);
    layout.putConstraint(SpringLayout.WEST, right, 20, SpringLayout.EAST, left);

    frame.setSize(300, 100);
    frame.show();
}