Example usage for javax.swing JPanel setLayout

List of usage examples for javax.swing JPanel setLayout

Introduction

In this page you can find the example usage for javax.swing JPanel 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[]) {
    JPanel p = new JPanel();

    p.setLayout(new GridLayout(2, 1));
    JList<String> lista = new JList<>(new String[] { "1", "2", "3", "4" });
    p.add(new JScrollPane(lista));
    JComboBox<String> combo = new JComboBox<>();
    for (int i = 0; i < 100; i++) {
        combo.addItem(Integer.toString(i));
        p.add(combo);//w ww  . ja  v a  2s .  c  o m
    }

    JFrame f = new JFrame();
    f.getContentPane().add(p, BorderLayout.CENTER);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(200, 200);
    f.setVisible(true);
}

From source file:Main.java

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

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(3, 0));

    for (int i = 1; i <= 9; i++) {
        buttonPanel.add(new JButton("Button  " + i));
    }/* ww  w  . j  av  a 2  s  .  c  om*/

    contentPane.add(buttonPanel, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
}

From source file:MyLabel.java

public static void main(String[] args) {
    String lyrics = "<html>Line<br>line<br>line</html>";

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout(10, 10));

    JLabel label = new JLabel(lyrics);
    label.setFont(new Font("Georgia", Font.PLAIN, 14));
    label.setForeground(new Color(50, 50, 25));

    panel.add(label, BorderLayout.CENTER);
    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    JFrame f = new JFrame();
    f.add(panel);/*from   ww  w .  ja  v a2  s .  c o  m*/
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame("This is BOX LAYOUT");
    JPanel panel1 = new JPanel();
    panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
    panel1.setBackground(Color.yellow);

    JPanel panel2 = new JPanel();
    panel2.setLayout(new FlowLayout());
    panel2.setBackground(Color.ORANGE);

    for (int i = 0; i < 5; i++)
        panel1.add(new JCheckBox("CheckBox " + (i + 1)));

    f.add(panel1, BorderLayout.WEST);
    f.add(panel2, BorderLayout.CENTER);
    f.setVisible(true);/*from  ww w  . j  a  va 2  s. c om*/
    f.setSize(300, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:BoxLayoutDemo.java

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

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
    p.add(createComponent("Component 1"));
    p.add(Box.createVerticalGlue());
    p.add(createComponent("Component 2"));
    p.add(createComponent("Component 3"));
    p.add(createComponent("Component 4"));
    frame.setContentPane(p);/*from   w  ww  .  j a v a 2 s .co m*/

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

From source file:SimpleGridBag.java

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

    p.setLayout(new GridBagLayout());
    p.add(new JButton("Java"));
    p.add(new JButton("Source"));
    p.add(new JButton("and"));
    p.add(new JButton("Support."));

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);//  ww  w .  j  av  a2s . co  m
        }
    };
    f.addWindowListener(wndCloser);

    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
}

From source file:Main.java

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

    final JTextField textField = new JTextField();

    JScrollBar scrollBar = new JScrollBar(JScrollBar.HORIZONTAL);

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

    BoundedRangeModel brm = textField.getHorizontalVisibility();
    scrollBar.setModel(brm);//  w  ww .  j  av  a  2 s. c  o  m
    panel.add(textField);
    panel.add(scrollBar);

    frame.add(panel, BorderLayout.NORTH);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame dialog = new JFrame();
    dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    dialog.setResizable(true);/*from   w ww.java 2 s.c om*/

    JPanel guiHolder = new JPanel();
    guiHolder.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.anchor = GridBagConstraints.PAGE_START;
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    guiHolder.add(new JLabel("my test"), gbc);

    dialog.add(guiHolder);
    dialog.setSize(new Dimension(320, 240));
    dialog.setVisible(true);
}

From source file:RigidArea.java

public static void main(String[] args) {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.setBorder(new EmptyBorder(new Insets(40, 60, 40, 60)));

    panel.add(new JButton("Button"));
    panel.add(Box.createRigidArea(new Dimension(0, 5)));
    panel.add(new JButton("Button"));
    panel.add(Box.createRigidArea(new Dimension(0, 5)));
    panel.add(new JButton("Button"));
    panel.add(Box.createRigidArea(new Dimension(0, 5)));
    panel.add(new JButton("Button"));

    JFrame f = new JFrame();
    f.add(panel);//from  w  w  w.j a  va  2  s.  co  m
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

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

    int x = 5;/*www  .j  a  v a  2 s . c  o m*/
    int y = 5;
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(x, y));
    for (int i = 0; i < x * y; i++) {
        JButton button = new JButton(String.valueOf(i));
        button.setPreferredSize(new Dimension(100, 100));
        panel.add(button);
    }
    JPanel container = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
    container.add(panel);
    JScrollPane scrollPane = new JScrollPane(container);
    f.getContentPane().add(scrollPane);

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