JPanel 显示为一个小白框 [英] JPanel appears as a small white box

查看:29
本文介绍了JPanel 显示为一个小白框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正处于尝试创建 Java 2d 图形绘制程序的早期阶段.我正在使用流布局,我正在使用三个面板.前两个是按钮、组合框等的行,第三个是一个大的、空白的、白色的面板,用于绘画.前两个面板显示精美,但油漆面板显示为第二个按钮面板旁边的小白框.任何帮助将不胜感激.

I am in the early stages of trying to create a Java 2d graphics paint program. I'm using a flow layout, and I'm using three panels. The first two are rows of buttons, combo boxes, etc. and the third is meant to be a large, blank, white panel that will be used to paint on. The first two panels show up beautifully, but the paint panel appears as a small white box next to the second button panel. Any help would be appreciated.

public class DrawingApp extends JFrame
{
    private final topButtonPanel topPanel = new topButtonPanel();
    private final bottomButtonPanel bottomPanel = new bottomButtonPanel();
    private final PaintPanel paintPanel = new PaintPanel();

    public DrawingApp()
    {
        super("Java 2D Drawings");
        setLayout(new FlowLayout());
        add(topPanel);
        add(bottomPanel);
        add(paintPanel);
    }

    public static void main(String[] args) 
    {
        DrawingApp frame = new DrawingApp();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(750,500);
        frame.setVisible(true); 
    }
}

public class topButtonPanel extends JPanel
{

    private final String[] names = {"Line", "Oval", "Rectangle"};

    private final JButton undo = new JButton("Undo");
    private final JButton clear = new JButton("Clear");
    private final JLabel shape = new JLabel("Shape:");
    private final JComboBox<String> shapesComboBox = new JComboBox(names);
    private final JCheckBox filled = new JCheckBox("Filled");

    public topButtonPanel()
    {
        super();
        setLayout(new FlowLayout());
        add(undo);
        add(clear);
        add(shape);
        shapesComboBox.setMaximumRowCount(3);
        add(shapesComboBox);
        add(filled);
    }
}


public class bottomButtonPanel extends JPanel
{

    private final JCheckBox useGradient = new JCheckBox("Use Gradient");
    private final JButton firstColor = new JButton("1st Color");
    private final JButton secondColor = new JButton("2nd Color");
    private final JLabel lineWidthLabel = new JLabel("Line Width:");
    private final JLabel dashLengthLabel = new JLabel("Dash Length:");
    private final JTextField lineWidthField = new JTextField(2);
    private final JTextField dashLengthField = new JTextField(2);
    private final JCheckBox filled = new JCheckBox("Dashed");

    public bottomButtonPanel()
    {
        super();
        setLayout(new FlowLayout());
        add(useGradient);
        add(firstColor);
        add(secondColor);
        add(lineWidthLabel);
        add(lineWidthField);
        add(dashLengthLabel);
        add(dashLengthField);
        add(filled);
    }
} 

public class PaintPanel extends JPanel
{

    public PaintPanel()
    {
        super();
        setBackground(Color.WHITE);
        setSize(700,400);
    }
}

推荐答案

基本上,这是对 Swing API 工作方式的误解.

Basically, it's a misunderstanding of how the Swing API works.

Swing(严重)依赖于布局管理 API,该 API 用于决定组件应该有多大(以及应该放置在哪里)

Swing relies (heavily) on the layout management API which is used to make decisions about how large components should be (and where they should be placed)

使用 setSize 是没有意义的,因为布局管理器会自行决定它认为组件的大小应该是什么,并会相应地进行调整.

Using setSize is pointless, as the layout manager will make it's own decisions about what it thinks the size of your component should be and will adjust it accordingly.

例如,您可以使用getPreferred/Minimum/MaximumSize向布局管理器提出您希望组件有多大的建议

You can make suggestions to the layout manager about how large you'd like the component to be using getPreferred/Minimum/MaximumSize, for example

public class PaintPanel extends JPanel
{

    public PaintPanel()
    {
        super();
        setBackground(Color.WHITE);
    }

    public Dimension getPreferredSize() {
        return new Dimension(700, 400);
    }
}

请记住,布局管理器完全有权忽略这些值,因此您需要更好地了解这些管理器的工作原理

Just remember, layout managers are well within their right to ignore these values, so you need to have a better understanding of how these managers work

有关详细信息,请参阅在容器内布置组件

这篇关于JPanel 显示为一个小白框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆