如何在西Jpanel(itempanel)下面创建一个简单的按钮列?

时间:2014-03-25 22:52:15

标签: java swing user-interface layout

我添加了一个JButton,但它与JPanel的大小相同。添加的任何其他JButtons都不会显示。 我尝试了很多布局管理器,但他们只是给我错误或将按钮放在非常不正确的位置。 任何帮助,将不胜感激。

public class FurnitureShop extends JFrame {

public static void main(String[] args) {
    new FurnitureShop().setVisible(true);
}

private FurnitureShop() {
    setSize(800, 600);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new BorderLayout());

    JPanel contentpanel  = new JPanel(new BorderLayout());
    contentpanel.setPreferredSize(new Dimension (800, 100));
    contentpanel.setBackground(Color.blue);

    JPanel footerpanel = new JPanel(new BorderLayout());
      footerpanel.setPreferredSize(new Dimension (800, 100));
      footerpanel.setBackground(Color.cyan);


    JPanel headerpanel = new JPanel(new BorderLayout());
      headerpanel.setPreferredSize(new Dimension (400, 200));
      headerpanel.setBackground(Color.black);


    JPanel itempanel = new JPanel(new BorderLayout());
      itempanel.setPreferredSize(new Dimension (400, 200));
      itempanel.setBackground(Color.green);


      add(contentpanel, BorderLayout.NORTH);
      add(footerpanel, BorderLayout.SOUTH);
      add(headerpanel, BorderLayout.EAST);
      add(itempanel, BorderLayout.WEST);

    JButton button = new JButton();
      button.setPreferredSize(new Dimension (20, 20));
      //button.setLocation(20,20);
      button.setVisible(true);

  itempanel.add(button);

1 个答案:

答案 0 :(得分:3)

我的第一个想法是使用GridBagLayout虽然这样可行,但我想知道是否有更简单的方法。

这使用JPanel作为主要容器BorderLayout(添加到框架的EAST位置)。在NORTH位置放置一个JPanelGridLayout配置为使用单个列

这称为复合布局。它允许您组合一系列布局管理器的功能以满足您的需求。单个布局管理器满足您的每个要求并且能够分离责任并不是很常见,这是一个非常强大的概念,可以让您专注于个性化需求

请查看Laying Out Components Within a Container了解详情。

Buttons

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ButtonColumns {

    public static void main(String[] args) {
        new ButtonColumns();
    }

    public ButtonColumns() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                try {
                    JLabel label = new JLabel(new ImageIcon(ImageIO.read(new File("\path\to\images"))));

                    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)));
                    }

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

                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException exp) {
                    exp.printStackTrace();
                }
            }
        });
    }

}