我正在尝试为一个简单的调度程序创建一个小型主菜单,但是我发现按钮之间没有空隙。它们紧贴在一起,我想让每个按钮之间有一些间隔。
我已经尝试使用weightx、weighty命令,但似乎没有任何变化。 我想要在GUI边缘和按钮边缘之间以及每个按钮之间留出一些空白空间。
这里是一个截图
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Scheduler {
JButton VDay, VWeek, Task, Exit;
JFrame wframe, dframe, tframe;
JLabel head;
public void CreateFrame() {
JFrame frame = new JFrame("Main Menu");
ButtonListener btnlst = new ButtonListener();
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
c.weighty = .25;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.BOTH;
head = new JLabel("The Plain Scheduler");
panel.add(head, c);
c.weightx = 1;
c.weighty = .25;
c.gridx = 0;
c.gridy = 1;
c.gridheight = 2;
c.gridwidth = 3;
VDay = new JButton("View Day");
panel.add(VDay, c);
c.weightx = 1;
c.weighty = .25;
c.gridx = 0;
c.gridy = 3;
c.gridheight = 2;
c.gridwidth = 3;
VWeek = new JButton("View Week");
panel.add(VWeek,c);
c.weightx = 1;
c.weighty = .25;
c.gridx = 0;
c.gridy = 5;
c.gridheight = 2;
c.gridwidth = 3;
Task = new JButton("Assign/Edit Tasks");
panel.add(Task, c);
c.weightx = 1;
c.weighty = .25;
c.gridx = 0;
c.gridy = 7;
c.gridheight = 1;
c.gridwidth = 2;
Exit = new JButton("Exit");
panel.add(Exit, c);
VDay.addActionListener(btnlst);
VWeek.addActionListener(btnlst);
Task.addActionListener(btnlst);
Exit.addActionListener(btnlst);
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Scheduler scheduler = new Scheduler();
scheduler.CreateFrame();
}
}
GridBagConstraints#insets
。 - MadProgrammer