JPanel显示为一个小白框

时间:2016-03-23 22:31:01

标签: java swing

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

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);
    }
}

1 个答案:

答案 0 :(得分:1)

基本上,这是对Swing API如何工作的误解。

Swing(大量)依赖于布局管理API,该API用于决定组件应该有多大(以及应该放置它们的位置)

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

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

public class PaintPanel extends JPanel
{

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

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

请记住,布局管理员完全有权忽略这些价值观,因此您需要更好地了解这些管理人员的工作方式

有关详细信息,请参阅Laying Out Components Within a Container

相关问题