[英]Components not appearing on my formPanel
我有一个formPanel,它扩展了JPanel,并且直接位于扩展JFrame的MainFrame上。
// this is the constructor of the mainframe
public MainFrame() {
super("Invoice Generator");
toolBar=new ToolBar();
formPanel=new FormPanel();
setLayout(new BorderLayout());
add(toolBar, BorderLayout.NORTH);
add(formPanel, BorderLayout.CENTER);
// this is where i add the custom components
setBackground(new Color(170, 175, 255));
setVisible(true);
layout();
setSize(new Dimension(500, 250));
}
public void layout() {
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
/////////////first row/////////////////////////////
gc.gridx=0;
gc.gridy=0;
gc.weightx=1;
gc.weighty=1;
gc.insets=new Insets(2, 2, 2, 2);
gc.anchor=GridBagConstraints.NONE;
add(new JButton("Booked by"), gc);
gc.gridx=1;
gc.gridy=0;
gc.weightx=1;
gc.weighty=1;
gc.insets=new Insets(2, 2, 2, 2);
gc.anchor=GridBagConstraints.NONE;
add(text1, gc);
}
我的组件没有出现在formPanel上,为什么?
您的layout
方法似乎在您的JFrame
而不是您的formPanel
就像是...
public void layout() {
formPanel.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
/////////////first row/////////////////////////////
gc.gridx=0;
gc.gridy=0;
gc.weightx=1;
gc.weighty=1;
gc.insets=new Insets(2, 2, 2, 2);
gc.anchor=GridBagConstraints.NONE;
formPanel.add(new JButton("Booked by"), gc);
gc.gridx=1;
gc.gridy=0;
gc.weightx=1;
gc.weighty=1;
gc.insets=new Insets(2, 2, 2, 2);
gc.anchor=GridBagConstraints.NONE;
formPanel.add(text1, gc);
}
可能会更好。
我会努力地创建一个自JPanel
扩展的自定义类,该类封装了这些字段,然后将其简单地添加到框架中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.