【发布时间】:2012-03-21 04:05:41
【问题描述】:
大家好,我有问题。如果有人可以提供帮助,那就太好了。我正在使用边框和网格布局,我正在尝试拆分 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);
}
}
【问题讨论】:
-
WEST 中的那些按钮可能更适合放在
JToolBar中的NORTH中。 OTOH 我不完全清楚你希望这个 GUI 如何出现,或者在调整大小时它应该如何重新分配空间。你能添加一些 ASCII 艺术或图画来更好地解释吗? -
..另外,我猜
RunRurniture应该是RunFurniture。 ;) -
你说的很对类名
-
我正在尝试做一个简单的设计
-
我认为,
LINE_START或WEST上的JPanel必须与BoxLayout和PAGE_AXIS一起使用,而CENTERJPanel 可以与GridLayout一起使用容纳这些图片:-)
标签: java swing border-layout