大家好,我有一个问题。如果有人可以提供帮助,那就太好了。我正在使用边框和网格布局,我正在尝试拆分 GUI,但它没有发生,因为我希望按钮成为整体的一小部分,比如说 1/5,但目前超过 GUI 的一半。我也尝试将按钮放在维度上,但我不确定这是否是一个好习惯。我有两个类,一个是 RunFurniture,其中是框架的主要方法,另一个方法是带有 GUI 的 PanelFurniture。我正在使用 eclipse 和程序正在编译并运行。我希望我给了一个很好的解释。这是代码。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class PanelFurniture extends JPanel implements ActionListener
{
JButton center, east;
JButton[] commandButtons = {
new JButton(" Add Chair"),
new JButton(" Add Table"),
new JButton(" Add Desk "),
new JButton(" Clear All "),
new JButton("Total Price"),
new JButton(" Save "),
new JButton(" Load "),
new JButton("Summary ")
};
JPanel centerPanel, westPanel, eastPanel;
PanelFurniture()
{
this.setLayout(new BorderLayout());
westPanel = new JPanel();
westPanel.setLayout(new FlowLayout());
for(int i=0; i<commandButtons.length; i++)
{
westPanel.add(commandButtons[i]);
commandButtons[i].addActionListener(this);
}
// westPanel.setSize(westDimension);
this.add(westPanel, BorderLayout.WEST);
// start the middle panel
centerPanel = new JPanel(new GridLayout(1,2));
center = new JButton("center");
centerPanel.add(center);
east = new JButton("east");
centerPanel.add(east);
this.add(centerPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent ae)
{
}
}
RunRurniture
import java.awt.*;
import javax.swing.*;
public class RunRurniture
{
/**
* @param args
*/
public static void main(String[] args)
{
JFrame application = new JFrame();
PanelFurniture panel = new PanelFurniture();
application.add(panel);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setSize(300,150);
application.setLocationByPlatform(true);
application.setVisible(true);
}
}