大家好我有问题。如果有人可以帮助它会很棒。我正在使用border和gridlayout,我正在尝试拆分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)
{
}
}
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);
}
}
答案 0 :(得分:4)
最新修改:
我认为,JPanel
或LINE_START
上的WEST
必须与GridLayout
一致,而CENTER
JPanel可以与{{1}一起使用容纳这些图像:-)。当你接受这个答案时,我正在研究它,当@AndrewThompson添加了关于GridLayout
thingy的评论时,但似乎把JPanel放在GridLayout上将允许相同大小的JButton。以下是我在代码中使用GridLayout
的解释:
GridLayout
输出:
以下是我在代码中使用import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import javax.swing.*;
public class RunFurniture
{
/**
* @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.pack();
application.setLocationByPlatform(true);
application.setVisible(true);
}
}
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 GridLayout(0, 1));
//westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.PAGE_AXIS));
westPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
for(int i=0; i<commandButtons.length; i++)
{
westPanel.add(commandButtons[i]);
commandButtons[i].addActionListener(this);
}
// westPanel.setSize(westDimension);
this.add(westPanel, BorderLayout.LINE_START);
// 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)
{
}
}
的解释:
BoxLayout
这是输出:
答案 1 :(得分:4)
这是我在评论中提到的一个例子(还有一些其他的调整)。
package test;
import java.awt.*;
import javax.swing.*;
public class PanelFurniture extends JPanel
{
private static final long serialVersionUID = 4231608548183463223L;
JButton center, east;
// leading/trailing spaces in these labels will be ignored.
JButton[] commandButtons = {
new JButton("Add Chair"),
new JButton("Add Table"),
new JButton("Add Desk "),
new JButton("Clear All "),
new JButton("Total Price"),
new JButton("Summary "),
new JButton("Save"),
new JButton("Load")
};
JPanel centerPanel, eastPanel;
PanelFurniture()
{
this.setLayout(new BorderLayout());
JToolBar toolBar = new JToolBar();
for(int i=0; i<commandButtons.length; i++)
{
if (i==3 || i==6) {
toolBar.addSeparator();
}
toolBar.add(commandButtons[i]);
}
this.add(toolBar, BorderLayout.NORTH);
// 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 static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame application = new JFrame();
PanelFurniture panel = new PanelFurniture();
application.add(panel);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.pack();
application.setLocationByPlatform(true);
application.setVisible(true);
}
});
}
}
此GUI基于图像。
package test;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class PanelFurniture extends JPanel
{
private static final long serialVersionUID = 4231608548183463223L;
JButton center, east;
// leading/trailing spaces in these labels will be ignored.
JButton[] commandButtons = {
new JButton("Add Chair"),
new JButton("Add Table"),
new JButton("Add Desk "),
new JButton("Clear All "),
new JButton("Total Price"),
new JButton("Summary "),
new JButton("Save"),
new JButton("Load")
};
JPanel centerPanel, westPanel, westPanelConstrain, eastPanel;
PanelFurniture()
{
super(new BorderLayout(2,2));
this.setBorder(new EmptyBorder(4,4,4,4));
westPanel = new JPanel(new GridLayout(0,1,4,4));
for(int i=0; i<commandButtons.length; i++)
{
westPanel.add(commandButtons[i]);
}
westPanelConstrain = new JPanel(new BorderLayout());
westPanelConstrain.add(westPanel, BorderLayout.NORTH);
this.add(westPanelConstrain, 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 static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame application = new JFrame();
PanelFurniture panel = new PanelFurniture();
application.add(panel);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.pack();
application.setLocationByPlatform(true);
application.setVisible(true);
}
});
}
}
答案 2 :(得分:4)
另外一些建议:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/** @see http://stackoverflow.com/q/9793194/230513 */
public class FurnitureTest {
private static final class FurniturePanel
extends JPanel implements ActionListener {
private static final int N = 3;
private static final Icon icon =
UIManager.getIcon("OptionPane.informationIcon");
private JPanel westPanel = new JPanel();
private JPanel centerPanel = new JPanel();
private 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")
};
FurniturePanel() {
this.setLayout(new GridLayout());
westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.Y_AXIS));
for (JButton b : commandButtons) {
b.setAlignmentX(JButton.CENTER_ALIGNMENT);
westPanel.add(b);
b.addActionListener(this);
}
this.add(westPanel, BorderLayout.WEST);
centerPanel.setLayout(new GridLayout(N, N, N, N));
for (int i = 0; i < N * N; i++) {
centerPanel.add(new JLabel(icon));
}
this.add(centerPanel, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame application = new JFrame();
FurniturePanel panel = new FurniturePanel();
application.add(panel);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.pack();
application.setLocationByPlatform(true);
application.setVisible(true);
}
});
}
}