gpt4 book ai didi

java - 当我尝试在框架上添加多个组件时,Swing 显示白色框

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

我正在开发 java swing 应用程序。我尝试在我的主 JFrame 上添加 2 个 JPanel。

我的主要 JPanel 类:

public class DrawingPanel extends JPanel {
//mouse variables here
//Point mPt = new Point(0,0);

public DrawingPanel() {
setBackground(COLOURBACK);
MyMouseListener ml = new MyMouseListener();
addMouseListener(ml);
}

public Dimension getPreferredSize() {
return new Dimension(width, height);
}
....
....
....
}
当我添加为主框架上的唯一组件时,

工作正常。但是当我尝试在框架上添加另一个 JPanel 时,我的 DrawingPanel 显示为小白框。

private void createAndShowGUI()
{
DrawingPanel panel = new DrawingPanel();


JFrame frame = new JFrame("Hex Testing 4");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
Container content = frame.getContentPane();
JPanel aggregationFrame = new JPanel(new GridBagLayout());
aggregationFrame.add(panel);
aggregationFrame.add(new JLabel("Enter username:"));
content.add(aggregationFrame);
//this.add(panel); -- cannot be done in a static context
//for hexes in the FLAT orientation, the height of a 10x10 grid is 1.1764 * the width. (from h / (s+t))
frame.setSize( (int)(SCRSIZE/1.23), SCRSIZE);
frame.setResizable(false);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}

看起来像这样

result

我尝试按照 JPanel appears as a small white box 中的建议将 getPreferredSize() 方法添加到 DrawingPanel问题但效果不佳。

你能帮我解决这个问题吗?

更新:

我已经更改了布局,现在我看到了 JPanel 的一半。我认为问题与 JPanel 和 JFrame 的固定大小有关。会调查一下。

update

最佳答案

所以,我采用了你的断章取义的代码,拼凑了一个可运行的变体并且......没有任何问题。这表明问题出在您未向我们展示的代码中的某个位置。不要发布不完整代码的“片段”,这会引发更多问题,而是发布 Minimal, Complete, and Verifiable example ,可以编译并运行,它演示了您遇到的问题

一个运行并工作的示例...

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
DrawingPanel panel = new DrawingPanel();

JFrame frame = new JFrame("Hex Testing 4");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = frame.getContentPane();
JPanel aggregationFrame = new JPanel(new GridBagLayout());
aggregationFrame.add(panel);
aggregationFrame.add(new JLabel("Enter username:"));
content.add(aggregationFrame);
//this.add(panel); -- cannot be done in a static context
//for hexes in the FLAT orientation, the height of a 10x10 grid is 1.1764 * the width. (from h / (s+t))
frame.pack();
//frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class DrawingPanel extends JPanel {
//mouse variables here
//Point mPt = new Point(0,0);

private int width = 200;
private int height = 200;

public DrawingPanel() {
setBackground(Color.BLACK);
// MyMouseListener ml = new MyMouseListener();
// addMouseListener(ml);
}

public Dimension getPreferredSize() {
return new Dimension(width, height);
}
}

}

关于java - 当我尝试在框架上添加多个组件时,Swing 显示白色框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53696506/

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