Example usage for java.awt Container setLayout

List of usage examples for java.awt Container setLayout

Introduction

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

Prototype

public void setLayout(LayoutManager mgr) 

Source Link

Document

Sets the layout manager for this container.

Usage

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(0, 1));

    // Define Start Button
    JButton startButton = new JButton("Start");
    ActionListener startActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component parent = (Component) actionEvent.getSource();
            monitor = new ProgressMonitor(parent, "Loading Progress", "Getting Started...", 0, 200);
            progress = 0;/*ww  w. ja  va  2 s.c o m*/
        }
    };
    startButton.addActionListener(startActionListener);
    contentPane.add(startButton);

    // Define Manual Increase Button
    // Pressing this button increases progress by 5
    JButton increaseButton = new JButton("Manual Increase");
    ActionListener increaseActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            if (monitor == null)
                return;
            if (monitor.isCanceled()) {
                System.out.println("Monitor canceled");
            } else {
                progress += 5;
                monitor.setProgress(progress);
                monitor.setNote("Loaded " + progress + " files");
            }
        }
    };
    increaseButton.addActionListener(increaseActionListener);
    contentPane.add(increaseButton);

    // Define Automatic Increase Button
    // Start Timer to increase progress by 3 every 250 ms
    JButton autoIncreaseButton = new JButton("Automatic Increase");
    ActionListener autoIncreaseActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            if (monitor != null) {
                if (timer == null) {
                    timer = new Timer(250, new ProgressMonitorHandler());
                }
                timer.start();
            }
        }
    };
    autoIncreaseButton.addActionListener(autoIncreaseActionListener);
    contentPane.add(autoIncreaseButton);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:URLMonitorPanel.java

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame("URL Monitor");
    Container c = frame.getContentPane();
    c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
    Timer t = new Timer();
    String[] u = new String[] { "http://www.java2s.com", "http://www.java2s.com" };

    for (int i = 0; i < u.length; i++) {
        c.add(new URLMonitorPanel(u[i], t));
    }/*from   w  w  w  . j a  v a 2 s .c om*/
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent evt) {
            System.exit(0);
        }
    });
    frame.pack();
    frame.show();
}

From source file:SampleProgress.java

public static void main(String args[]) {

    JFrame frame = new JFrame("ProgressMonitor Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(0, 1));

    // Define Start Button
    JButton startButton = new JButton("Start");
    ActionListener startActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component parent = (Component) actionEvent.getSource();
            monitor = new ProgressMonitor(parent, "Loading Progress", "Getting Started...", 0, 200);
            progress = 0;//  w  w  w.  j  a  v a 2s. c o m
        }
    };
    startButton.addActionListener(startActionListener);
    contentPane.add(startButton);

    // Define Manual Increase Button
    // Pressing this button increases progress by 5
    JButton increaseButton = new JButton("Manual Increase");
    ActionListener increaseActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            if (monitor == null)
                return;
            if (monitor.isCanceled()) {
                System.out.println("Monitor canceled");
            } else {
                progress += 5;
                monitor.setProgress(progress);
                monitor.setNote("Loaded " + progress + " files");
            }
        }
    };
    increaseButton.addActionListener(increaseActionListener);
    contentPane.add(increaseButton);

    // Define Automatic Increase Button
    // Start Timer to increase progress by 3 every 250 ms
    JButton autoIncreaseButton = new JButton("Automatic Increase");
    ActionListener autoIncreaseActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            if (monitor != null) {
                if (timer == null) {
                    timer = new Timer(250, new ProgressMonitorHandler());
                }
                timer.start();
            }
        }
    };
    autoIncreaseButton.addActionListener(autoIncreaseActionListener);
    contentPane.add(autoIncreaseButton);

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

From source file:InvokeExample.java

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

    JPanel p = new JPanel();
    p.add(good);/*from   ww w .j  a  v a2  s.c om*/
    p.add(bad);
    p.add(bad2);

    Container c = f.getContentPane();
    c.setLayout(new BorderLayout());
    c.add(p, BorderLayout.CENTER);
    c.add(resultLabel, BorderLayout.SOUTH);

    good.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            resultLabel.setText("Working . . .");
            setEnabled(false);
            Thread worker = new Thread() {
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException ex) {
                    }

                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            resultLabel.setText("Ready");
                            setEnabled(true);
                        }
                    });
                }
            };
            worker.start();
        }
    });

    bad.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            resultLabel.setText("Working . . .");
            setEnabled(false);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
            }
            resultLabel.setText("Ready");
            setEnabled(true);
        }
    });

    bad2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            resultLabel.setText("Working . . . ");
            setEnabled(false);
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep(5000); // Dispatch thread is starving!
                    } catch (InterruptedException ex) {
                    }

                    resultLabel.setText("Ready");
                    setEnabled(true);
                }
            });
        }
    });

    f.setSize(300, 100);
    f.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);//www  .j av  a  2  s .c o m
    contentPane.add(b2);

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

From source file:SpringDemo3.java

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

    // Set up the content pane.
    Container contentPane = frame.getContentPane();
    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(layout);

    // Create and add the components.
    JLabel label = new JLabel("Label: ");
    JTextField textField = new JTextField("Text field", 15);
    contentPane.add(label);/*from   ww w.ja  v a 2s .com*/
    contentPane.add(textField);

    // Adjust constraints for the label so it's at (5,5).
    layout.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, contentPane);
    layout.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, contentPane);

    // Adjust constraints for the text field so it's at
    // (<label's right edge> + 5, 5).
    layout.putConstraint(SpringLayout.WEST, textField, 5, SpringLayout.EAST, label);
    layout.putConstraint(SpringLayout.NORTH, textField, 5, SpringLayout.NORTH, contentPane);

    // Adjust constraints for the content pane: Its right
    // edge should be 5 pixels beyond the text field's right
    // edge, and its bottom edge should be 5 pixels beyond
    // the bottom edge of the tallest component (which we'll
    // assume is textField).
    layout.putConstraint(SpringLayout.EAST, contentPane, 5, SpringLayout.EAST, textField);
    layout.putConstraint(SpringLayout.SOUTH, contentPane, 5, SpringLayout.SOUTH, textField);

    // Display the window.
    frame.pack();
    frame.setVisible(true);
}

From source file:BoxSample.java

public static void main(String args[]) {
    JButton button;/*from   w  ww  .j  ava  2  s  .co  m*/
    Vector buttons = new Vector();
    Dimension dim;
    JFrame frame = new JFrame("Box Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
    JPanel topLeft = new JPanel();
    topLeft.setLayout(new BoxLayout(topLeft, BoxLayout.X_AXIS));
    topLeft.add(button = new JButton("One"));
    buttons.add(button);
    changeBoth(button);
    topLeft.add(button = new JButton("Two"));
    buttons.add(button);
    changeBoth(button);
    changeWidth(topLeft);
    JPanel bottomLeft = new JPanel();
    bottomLeft.setLayout(new BoxLayout(bottomLeft, BoxLayout.X_AXIS));
    bottomLeft.add(button = new JButton("Six"));
    buttons.add(button);
    changeBoth(button);
    bottomLeft.add(button = new JButton("Seven"));
    buttons.add(button);
    changeBoth(button);
    changeWidth(bottomLeft);
    JPanel left = new JPanel();
    left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
    left.add(topLeft);
    left.add(button = new JButton("Four"));
    buttons.add(button);
    changeBoth(button);
    left.add(bottomLeft);
    changeBoth(left);

    JPanel right = new JPanel();
    right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
    right.add(button = new JButton("Three"));
    buttons.add(button);
    changeWidth(button);
    right.add(button = new JButton("Five"));
    buttons.add(button);
    changeBoth(button);
    changeBoth(right);
    contentPane.add(left);
    contentPane.add(right);
    tweak(buttons);
    frame.pack();
    frame.setVisible(true);
}

From source file:SpringDemo4.java

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

    // Set up the content pane.
    Container contentPane = frame.getContentPane();
    SpringLayout layout = new SpringLayout();
    contentPane.setLayout(layout);

    // Create and add the components.
    JLabel label = new JLabel("Label: ");
    JTextField textField = new JTextField("Text field", 15);
    contentPane.add(label);/* w  w  w  . ja v  a  2s. co m*/
    contentPane.add(textField);

    // Adjust constraints for the label so it's at (5,5).
    SpringLayout.Constraints labelCons = layout.getConstraints(label);
    labelCons.setX(Spring.constant(5));
    labelCons.setY(Spring.constant(5));

    // Adjust constraints for the text field so it's at
    // (<label's right edge> + 5, 5).
    SpringLayout.Constraints textFieldCons = layout.getConstraints(textField);
    textFieldCons.setX(Spring.sum(Spring.constant(5), labelCons.getConstraint(SpringLayout.EAST)));
    textFieldCons.setY(Spring.constant(5));

    // Adjust constraints for the content pane.
    setContainerSize(contentPane, 5);

    // Display the window.
    frame.pack();
    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 to a 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("Button Second");

    // Create Constraints objects for b1 and b2
    SpringLayout.Constraints b1c = new SpringLayout.Constraints();
    SpringLayout.Constraints b2c = new SpringLayout.Constraints();

    // Create a Spring object for y value for b1 and b2
    Spring yPadding = Spring.constant(20);

    // Set (10, 20) for (x, y) for b1
    b1c.setX(Spring.constant(10));
    b1c.setY(yPadding);/*w w  w.j ava 2 s .co  m*/

    // Set (150, 20) for (x, y) for b2
    b2c.setX(Spring.constant(150));
    b2c.setY(yPadding);

    // Use the Constraints object while adding b1 and b2
    contentPane.add(b1, b1c);
    contentPane.add(b2, b2c);

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

From source file:BorderSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Sample Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border bevelBorder = new BevelBorder(BevelBorder.RAISED, Color.red, Color.red.darker(), Color.pink,
            Color.pink.brighter());
    Border emptyBorder = new EmptyBorder(5, 10, 5, 10);
    Border etchedBorder = new EtchedBorder(EtchedBorder.RAISED, Color.red, Color.pink);
    Border lineBorder = new LineBorder(Color.red, 5);
    Icon diamondIcon = new DiamondIcon(Color.red);
    Border matteBorder = new MatteBorder(5, 10, 5, 10, diamondIcon);
    Border softBevelBorder = new SoftBevelBorder(BevelBorder.RAISED, Color.red, Color.red.darker(), Color.pink,
            Color.pink.brighter());
    Font font = new Font("Serif", Font.ITALIC, 12);
    Border titledBorder = new TitledBorder(lineBorder, "Hello", TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM,
            font, Color.red);/*from   w  w w  .ja  v a  2 s  . c  o  m*/
    Border compoundBorder = new CompoundBorder(lineBorder, matteBorder);

    JLabel bevelLabel = new JLabel("Bevel");
    bevelLabel.setBorder(bevelBorder);
    bevelLabel.setHorizontalAlignment(JLabel.CENTER);
    JLabel emptyLabel = new JLabel("Empty");
    emptyLabel.setBorder(emptyBorder);
    emptyLabel.setHorizontalAlignment(JLabel.CENTER);
    JLabel etchedLabel = new JLabel("Etched");
    etchedLabel.setBorder(etchedBorder);
    etchedLabel.setHorizontalAlignment(JLabel.CENTER);
    JLabel lineLabel = new JLabel("Line");
    lineLabel.setBorder(lineBorder);
    lineLabel.setHorizontalAlignment(JLabel.CENTER);
    JLabel matteLabel = new JLabel("Matte");
    matteLabel.setBorder(matteBorder);
    matteLabel.setHorizontalAlignment(JLabel.CENTER);
    JLabel softBevelLabel = new JLabel("Soft Bevel");
    softBevelLabel.setBorder(softBevelBorder);
    softBevelLabel.setHorizontalAlignment(JLabel.CENTER);
    JLabel titledLabel = new JLabel("Titled");
    titledLabel.setBorder(titledBorder);
    titledLabel.setHorizontalAlignment(JLabel.CENTER);
    JLabel compoundLabel = new JLabel("Compound");
    compoundLabel.setBorder(compoundBorder);
    compoundLabel.setHorizontalAlignment(JLabel.CENTER);

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(2, 4, 5, 5));
    contentPane.add(bevelLabel);
    contentPane.add(emptyLabel);
    contentPane.add(etchedLabel);
    contentPane.add(lineLabel);
    contentPane.add(matteLabel);
    contentPane.add(softBevelLabel);
    contentPane.add(titledLabel);
    contentPane.add(compoundLabel);
    frame.setSize(400, 200);
    frame.setVisible(true);
}