【发布时间】:2021-06-04 16:51:56
【问题描述】:
这是我要复制的图片
这就是我所拥有的(尚未添加图标图像)
我似乎找不到解决方案,盯着它看了很长时间。
我正在尝试复制下图,将GridLayout 用于按钮,其余部分则使用Java Swing 自行计算。此外,我已将按钮添加到 JPanel 中,现在我正在尝试在面板和窗格之间添加间距。
这就是我所拥有的,我该怎么做?
super(title);
this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.PAGE_AXIS));
Container pane = this.getContentPane();
JButton b1 = new JButton();
b1.setBackground(Color.white);
JButton b2 = new JButton();
b2.setBackground(Color.white);
JButton b3 = new JButton();
b3.setBackground(Color.white);
JButton b4 = new JButton();
b4.setBackground(Color.white);
JButton b5 = new JButton();
b5.setBackground(Color.white);
JButton b6 = new JButton();
b6.setBackground(Color.white);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,3,10,10));
panel.setBackground(Color.black);
panel.add(b1);
panel.add(b2);
panel.add(b3);
panel.add(b4);
panel.add(b5);
panel.add(b6);
pane.add(panel);
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
【问题讨论】:
-
可能是
panel.setBorder(new EmptyBorder(..))。我说“可能”,因为这与我愿意给出一个缺少minimal reproducible example 的问题一样多的关注。顺便说一句this.setSize(500,500);最好是this.pack();
标签: java swing jframe jpanel awt