来自子类的 JPanel 未正确添加到 driver JFrame
JPanel from subclass not adding to driver JFrame correctly
我在尝试将我在子 class 中创建的 JPanel 添加到我在 driver class 中创建的 JFrame 时遇到了一些问题。我不确定为什么,但是面板没有显示它应该如何显示,我可以让它工作的唯一方法是在 subclass 和 driver class 中定义 JFrame 但后来我结束了有两个不同的 JFrames,一个是我想要的东西,一个是空白的。有任何想法吗?我已经添加了来自 classes 的代码以及它应该是什么样子和它看起来是什么样子的图像。
我的driverclass;
public class GUIDriver extends JFrame {
public static void main(String[] args) {
GUIDesign newOrder = new GUIDesign();
//frame
JFrame myframe = new JFrame("ROFA: Royal Furniture Ordering System");
myframe.setLayout(new BorderLayout());
myframe.setVisible(true);
myframe.setSize(900,600);
myframe.setResizable(false);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myframe.add(newOrder);
}
和我的 GUI class;
public class GUIDesign extends JPanel implements ActionListener {
GUIDesign(){
JButton chair = new JButton("Add Chair");
JButton desk = new JButton("Add Desk");
JButton table = new JButton("Add Table");
JButton clear = new JButton("Clear All");
JButton save = new JButton("Save file");
JButton load = new JButton("Load file");
GridBagLayout layout = new GridBagLayout();
JPanel panelLeft = new JPanel(layout);
panelLeft.setLayout(layout);
panelLeft.setBorder(BorderFactory.createEtchedBorder(Color.lightGray, Color.black));
this.add(panelLeft, BorderLayout.WEST);
GridBagConstraints c = new GridBagConstraints(0, 0, 0, 1, 1.0, 1.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(
0,0,0,0), 0, 0);
c.fill = GridBagConstraints.BOTH;
panelLeft.add(save, c);
c.gridx = 0;
c.gridy = 1;
panelLeft.add(load, c);
c.gridx = 0;
c.gridy = 2;
panelLeft.add(chair, c);
c.gridx = 0;
c.gridy = 3;
panelLeft.add(table, c);
c.gridx = 0;
c.gridy = 4;
panelLeft.add(desk, c);
c.gridx = 0;
c.gridy = 5;
panelLeft.add(clear, c);
}
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated
methods, choose Tools | Templates.
}
这就是它的样子:
这就是我的结局:
谢谢。
默认情况下,JPanel 使用 FlowLayout
。 FlowLayout
将以首选大小显示组件,默认为居中对齐,可以更改为向左或向右对齐。
this.add(panelLeft, BorderLayout.WEST);
您不能在添加组件时只对 FlowLayout 使用 BorderLayout 约束。
代码应该是:
this.setLayout( new BorderLayout() );
this.add(panelLeft, BorderLayout.LINE_START); // new recommendation instead of using "WEST".
现在应该将面板与面板的左侧对齐。
此外,请注意,使用 GridLayout
比使用 GridBagLayout
更容易。
将按钮添加到面板的代码很简单:
JPanel panelLeft = new JPanel( new GridLayout(0, 1) );
panelleft.add(save);
panelLeft.add(load);
不需要任何限制。
我在尝试将我在子 class 中创建的 JPanel 添加到我在 driver class 中创建的 JFrame 时遇到了一些问题。我不确定为什么,但是面板没有显示它应该如何显示,我可以让它工作的唯一方法是在 subclass 和 driver class 中定义 JFrame 但后来我结束了有两个不同的 JFrames,一个是我想要的东西,一个是空白的。有任何想法吗?我已经添加了来自 classes 的代码以及它应该是什么样子和它看起来是什么样子的图像。
我的driverclass;
public class GUIDriver extends JFrame {
public static void main(String[] args) {
GUIDesign newOrder = new GUIDesign();
//frame
JFrame myframe = new JFrame("ROFA: Royal Furniture Ordering System");
myframe.setLayout(new BorderLayout());
myframe.setVisible(true);
myframe.setSize(900,600);
myframe.setResizable(false);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myframe.add(newOrder);
}
和我的 GUI class;
public class GUIDesign extends JPanel implements ActionListener {
GUIDesign(){
JButton chair = new JButton("Add Chair");
JButton desk = new JButton("Add Desk");
JButton table = new JButton("Add Table");
JButton clear = new JButton("Clear All");
JButton save = new JButton("Save file");
JButton load = new JButton("Load file");
GridBagLayout layout = new GridBagLayout();
JPanel panelLeft = new JPanel(layout);
panelLeft.setLayout(layout);
panelLeft.setBorder(BorderFactory.createEtchedBorder(Color.lightGray, Color.black));
this.add(panelLeft, BorderLayout.WEST);
GridBagConstraints c = new GridBagConstraints(0, 0, 0, 1, 1.0, 1.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(
0,0,0,0), 0, 0);
c.fill = GridBagConstraints.BOTH;
panelLeft.add(save, c);
c.gridx = 0;
c.gridy = 1;
panelLeft.add(load, c);
c.gridx = 0;
c.gridy = 2;
panelLeft.add(chair, c);
c.gridx = 0;
c.gridy = 3;
panelLeft.add(table, c);
c.gridx = 0;
c.gridy = 4;
panelLeft.add(desk, c);
c.gridx = 0;
c.gridy = 5;
panelLeft.add(clear, c);
}
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated
methods, choose Tools | Templates.
}
这就是它的样子:
这就是我的结局:
谢谢。
默认情况下,JPanel 使用 FlowLayout
。 FlowLayout
将以首选大小显示组件,默认为居中对齐,可以更改为向左或向右对齐。
this.add(panelLeft, BorderLayout.WEST);
您不能在添加组件时只对 FlowLayout 使用 BorderLayout 约束。
代码应该是:
this.setLayout( new BorderLayout() );
this.add(panelLeft, BorderLayout.LINE_START); // new recommendation instead of using "WEST".
现在应该将面板与面板的左侧对齐。
此外,请注意,使用 GridLayout
比使用 GridBagLayout
更容易。
将按钮添加到面板的代码很简单:
JPanel panelLeft = new JPanel( new GridLayout(0, 1) );
panelleft.add(save);
panelLeft.add(load);
不需要任何限制。