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 void createUI() {
    JPanel borderPanel = new JPanel(new BorderLayout());

    JLabel northLabel = new JLabel("Nawth");
    borderPanel.add(northLabel, BorderLayout.NORTH);

    JComboBox southCombo = new JComboBox();
    borderPanel.add(southCombo, BorderLayout.SOUTH);

    JPanel centerPanel = new JPanel();
    centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));

    JComboBox firstCombo = new JComboBox();
    centerPanel.add(firstCombo);/*  www.  j a  va 2 s.c o  m*/
    centerPanel.add(Box.createVerticalGlue());
    JPanel centerPanelConstrain = new JPanel(new GridBagLayout());
    centerPanelConstrain.add(centerPanel);
    borderPanel.add(centerPanelConstrain, BorderLayout.CENTER);

    getContentPane().add(borderPanel);
    pack();
}

From source file:Main.java

public Main() {
    JPanel jp = new JPanel();
    jp.setLayout(new BorderLayout());
    final JTabbedPane tb = new JTabbedPane();
    JButton btn = new JButton("push me !!!");
    btn.addActionListener(e -> {/*w ww.j a  v a  2s  . c o m*/
        tb.setEnabledAt(1, true);
        tb.setEnabledAt(2, true);
        tb.setEnabledAt(3, true);
        tb.setEnabledAt(4, true);
    });
    JPanel pnl = new JPanel();
    pnl.add(btn);
    tb.add("Tab1", pnl);
    tb.add("Tab2", new JTextArea(10, 20));
    tb.add("Tab3", new JTextArea(10, 20));
    tb.add("Tab4", new JTextArea(10, 20));
    tb.add("Tab5", new JTextArea(10, 20));
    jp.add(tb, BorderLayout.CENTER);
    tb.setEnabledAt(1, false);
    tb.setEnabledAt(2, false);
    tb.setEnabledAt(3, false);
    tb.setEnabledAt(4, false);
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.add(jp, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

private void makeGUI() {

    setLayout(new BorderLayout());

    JPanel jp = new JPanel();
    jp.setLayout(new GridLayout(20, 20));
    int b = 0;//from w  ww  . j a va 2  s .  co m
    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 20; j++) {
            jp.add(new JButton("Button " + b));
            ++b;
        }
    }

    int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
    int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
    JScrollPane jsp = new JScrollPane(jp, v, h);

    add(jsp, BorderLayout.CENTER);
}

From source file:Main.java

public Main() {
    JFrame frame = new JFrame();
    JPanel container = new JPanel();
    container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
    CustomPanel customPanel1 = new CustomPanel(Color.blue);
    CustomPanel customPanel2 = new CustomPanel(Color.red);
    CustomPanel customPanel3 = new CustomPanel(Color.green);
    container.add(customPanel1);/*from  ww  w  .ja v a2  s.com*/
    container.add(customPanel2);
    container.add(customPanel3);
    frame.getContentPane().add(container);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public Main() {
    JPanel sliderPanel = new JPanel();
    sliderPanel.setLayout(new GridLayout(0, 1));
    for (int i = 0; i < slider.length; i++) {
        labels[i] = new Label(colorNames[i] + 255);
        sliderPanel.add(labels[i]);//from w w  w  . j  ava2 s  .  c  om
        slider[i] = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 255);
        slider[i].setMinorTickSpacing(10);
        slider[i].setMajorTickSpacing(50);
        slider[i].setPaintTicks(true);
        slider[i].setPaintLabels(true);
        sliderPanel.add(slider[i]);
    }
    lb = new Label("Colour");
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.setModal(true);
    dialog.add(sliderPanel, BorderLayout.CENTER);
    dialog.add(lb, BorderLayout.SOUTH);
    dialog.pack();
    dialog.setLocation(200, 200);
    dialog.setTitle("Colour Dialog");
    dialog.setVisible(true);
}

From source file:Main.java

public Main() {
    final JPopupMenu contextMenu = new JPopupMenu("Edit");
    contextMenu.add(makeMenuItem("Save"));
    contextMenu.add(makeMenuItem("Save As"));
    contextMenu.add(makeMenuItem("Close"));

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    frame.add(panel);//from w w w . ja  va2 s. c o m
    panel.setComponentPopupMenu(contextMenu);

    textArea.setInheritsPopupMenu(true);
    panel.add(BorderLayout.CENTER, textArea);

    JTextField textField = new JTextField();
    textField.setInheritsPopupMenu(true);
    panel.add(BorderLayout.SOUTH, textField);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 200);
    frame.setVisible(true);
}

From source file:ContextMenu.java

public ContextMenu() {
    final JPopupMenu contextMenu = new JPopupMenu("Edit");
    contextMenu.add(makeMenuItem("Save"));
    contextMenu.add(makeMenuItem("Save As"));
    contextMenu.add(makeMenuItem("Close"));

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    frame.add(panel);/*  w  ww .  ja v a  2 s . c  o  m*/
    panel.setComponentPopupMenu(contextMenu);

    textArea.setInheritsPopupMenu(true);
    panel.add(BorderLayout.CENTER, textArea);

    JTextField textField = new JTextField();
    textField.setInheritsPopupMenu(true);
    panel.add(BorderLayout.SOUTH, textField);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 200);
    frame.setVisible(true);
}

From source file:Main.java

public Main() {
    JPanel parentPanel = new JPanel();
    parentPanel.setLayout(new BorderLayout(10, 10));

    JPanel childPanel1 = new JPanel();
    childPanel1.setBackground(Color.red);
    childPanel1.setPreferredSize(new Dimension(300, 40));

    JPanel childPanel2 = new JPanel();
    childPanel2.setBackground(Color.blue);
    childPanel2.setPreferredSize(new Dimension(800, 600));

    JButton myButton = new JButton("Add Component ");
    myButton.addActionListener(e -> {
        parentPanel.remove(childPanel1);
        parentPanel.add(childPanel2, BorderLayout.CENTER);
        parentPanel.revalidate();/* w  w  w  .j ava  2s  .c o  m*/
        parentPanel.repaint();
        pack();
    });
    setLocation(10, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    parentPanel.add(childPanel1, BorderLayout.CENTER);
    parentPanel.add(myButton, BorderLayout.SOUTH);
    add(parentPanel);
    pack();
    setVisible(true);
}

From source file:Main.java

public Main() {
    dialog.setLayout(new GridLayout());
    yes.addActionListener(this);
    no.addActionListener(this);
    JPanel btnPanel = new JPanel();
    btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    btnPanel.add(yes);/* ww  w . java  2  s. c o m*/
    btnPanel.add(no);
    dialog.add(message, "Center");
    dialog.add(btnPanel, "South");
    dialog.pack();
    dialog.setVisible(true);
    dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    new Thread(this).start();
}

From source file:Main.java

public Main() {
    super("JLayeredPane Demo");
    setSize(256, 256);//www . j  a v  a  2s  .  c o  m

    JPanel content = new JPanel();
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
    content.setOpaque(false);

    JLabel label1 = new JLabel("Username:");
    label1.setForeground(Color.white);
    content.add(label1);

    JTextField field = new JTextField(15);
    content.add(field);

    JLabel label2 = new JLabel("Password:");
    label2.setForeground(Color.white);
    content.add(label2);

    JPasswordField fieldPass = new JPasswordField(15);
    content.add(fieldPass);

    setLayout(new FlowLayout());
    add(content);
    ((JPanel) getContentPane()).setOpaque(false);

    ImageIcon earth = new ImageIcon("largeJava2sLogo.png");
    JLabel backlabel = new JLabel(earth);
    getLayeredPane().add(backlabel, new Integer(Integer.MIN_VALUE));
    backlabel.setBounds(0, 0, earth.getIconWidth(), earth.getIconHeight());
    super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}