当我从主GUI类中提取大量代码并将它们放入较小的可管理类中时,我遇到了一个小错误。
所以我用一个非常简单的GUI做了另一个测试,发现那里发生了同样的错误,即使唯一的区别是代码位于不同的位置。
第一个JPanel正确显示并填充整个布局。 扩展JPanel的第二个TestPanel无法正确显示并填充布局的一小部分。
两个类都具有完全相同的GridBagLayout,并且两个JScrollPanes都具有相同的GridBagConstraints。
这是我的主类,其中包含两个JPanels的JTabbedPane,其中包含一个JScrollPane,每个都有一个JTextArea的视口。
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JScrollPane;
import java.awt.GridBagConstraints;
import javax.swing.JTextArea;
public class TestMainGUI
{
private JFrame frame;
private JTabbedPane tabbedPane;
private JPanel workingDefaultPanel;
private JScrollPane workingDefaultScrollPane;
private JTextArea workingDefaultJTextArea;
private TestJPanel notWorkingExtendingJPanel;
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
try
{
TestMainGUI window = new TestMainGUI();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public TestMainGUI()
{
initialize();
}
private void initialize()
{
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//******************************************************************//
tabbedPane = new JTabbedPane(SwingConstants.TOP);
frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
//******************************************************************//
workingDefaultPanel = new JPanel();
tabbedPane.addTab("Working Panel", null, workingDefaultPanel, null);
GridBagLayout gbl_WorkingDefaultPanel = new GridBagLayout();
gbl_WorkingDefaultPanel.columnWidths = new int[]{0, 0};
gbl_WorkingDefaultPanel.rowHeights = new int[]{0, 0};
gbl_WorkingDefaultPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_WorkingDefaultPanel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
workingDefaultPanel.setLayout(gbl_WorkingDefaultPanel);
//******************************************************************//
workingDefaultScrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 0;
workingDefaultPanel.add(workingDefaultScrollPane, gbc_scrollPane);
//******************************************************************//
workingDefaultJTextArea = new JTextArea();
workingDefaultScrollPane.setViewportView(workingDefaultJTextArea);
//******************************************************************//
notWorkingExtendingJPanel = new TestJPanel();
tabbedPane.addTab("Non Working Panel", null, notWorkingExtendingJPanel, null);
GridBagLayout gbl_panel_1 = new GridBagLayout();
gbl_panel_1.columnWidths = new int[]{0, 0};
gbl_panel_1.rowHeights = new int[]{0, 0};
gbl_panel_1.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_panel_1.rowWeights = new double[]{1.0, Double.MIN_VALUE};
notWorkingExtendingJPanel.setLayout(gbl_panel_1);
}
}
这是我的TestJPanel
import java.awt.GridBagConstraints;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TestJPanel extends JPanel
{
private static final long serialVersionUID = 5041143050449865233L;
private JScrollPane notWorkingJScrollPane;
private JTextArea notWorkingJTextArea;
public TestJPanel()
{
super();
notWorkingJScrollPane = new JScrollPane();
GridBagConstraints gbc_notWorkingScrollPane = new GridBagConstraints();
gbc_notWorkingScrollPane.fill = GridBagConstraints.BOTH;
gbc_notWorkingScrollPane.gridx = 0;
gbc_notWorkingScrollPane.gridy = 0;
this.add(notWorkingJScrollPane, gbc_notWorkingScrollPane);
//******************************************************************//
notWorkingJTextArea = new JTextArea();
notWorkingJScrollPane.setViewportView(notWorkingJTextArea);
}
protected JScrollPane getNotWorkingJScrollPane()
{
return notWorkingJScrollPane;
}
protected JTextArea getNotWorkingJTextArea()
{
return notWorkingJTextArea;
}
}
答案 0 :(得分:2)
notWorkingExtendingJPanel.setLayout(gbl_panel_1);
我猜测问题是,在将组件添加到面板后,您无法设置布局。
上述语句和所有网格包布局逻辑应该位于您创建并将组件添加到面板的TestJPanel类中。