gpt4 book ai didi

java - JPanel 和扩展 JPanel 的自定义类无法正确显示

转载 作者:行者123 更新时间:2023-12-02 04:48:25 25 4
gpt4 key购买 nike

当我从主 GUI 类中提取大量代码并将它们放入较小的可管理类中时,我遇到了一个小错误。

因此,我使用一个非常简单的 GUI 进行了另一次测试,发现即使唯一的区别是代码位于不同的位置,也会发生相同的错误。

第一个 JPanel 正确显示并填充整个布局。扩展 JPanel 的第二个 TestPanel 无法正确显示,并且填充了布局的一小部分。

The working JPanel containing JScrollPane with a JTextArea in it's viewport The non working JPanel containing JScrollPane with a JTextArea in it's viewport

两个类都具有完全相同的 GridBagLayout,并且两个 JScrollPanes 都具有相同的 GridBagConstraints。

这是我的主类,其中包含一个 JTabbedPane,其中包含两个 JPanel,其中包含一个 JScrollPane,每个 JPanel 的视口(viewport)中都有一个 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;
}
}

最佳答案

notWorkingExtendingJPanel.setLayout(gbl_panel_1);

我猜问题是在将组件添加到面板后无法设置布局。

上述语句和所有网格包布局逻辑应位于您创建组件并将组件添加到面板的 TestJPanel 类中。

关于java - JPanel 和扩展 JPanel 的自定义类无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29477242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com