Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPanel显示为一个白色小框_Java_Swing - Fatal编程技术网

Java JPanel显示为一个白色小框

Java JPanel显示为一个白色小框,java,swing,Java,Swing,我正处于尝试创建Java 2d图形绘制程序的早期阶段。我使用的是流程布局,我使用的是三个面板。前两行是按钮、组合框等,第三行是一个大的空白白色面板,用于在上面进行绘制。前两个面板显示得很漂亮,但“绘制”面板在第二个按钮面板旁边显示为一个白色小框。任何帮助都将不胜感激 public class DrawingApp extends JFrame { private final topButtonPanel topPanel = new topButtonPanel(); priva

我正处于尝试创建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);
    }
}
公共类DrawingApp扩展JFrame
{
私有最终topButtonPanel topPanel=新topButtonPanel();
私有最终bottomButtonPanel bottomPanel=新的bottomButtonPanel();
private final PaintPanel PaintPanel=新PaintPanel();
公开提款app()
{
超级(“Java 2D图纸”);
setLayout(新的FlowLayout());
添加(托帕内尔);
添加(底部面板);
添加(油漆面板);
}
公共静态void main(字符串[]args)
{
DrawingApp框架=新DrawingApp();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。设置尺寸(750500);
frame.setVisible(true);
}
}
公共类topButtonPanel扩展了JPanel
{
私有最终字符串[]名称={“线”、“椭圆”、“矩形”};
私有最终JButton undo=新JButton(“undo”);
专用最终JButton clear=新JButton(“clear”);
专用最终JLabel形状=新JLabel(“形状:”);
专用最终JCOMBOX形状COMBOX=新JCOMBOX(名称);
专用最终JCheckBox filled=新JCheckBox(“filled”);
公共topButtonPanel()
{
超级();
setLayout(新的FlowLayout());
添加(撤消);
添加(清除);
添加(形状);
shapesComboBox.setMaximumRowCount(3);
添加(shapesComboBox);
增加(填写);
}
}
公共类bottomButtonPanel扩展了JPanel
{
专用最终JCheckBox usegrident=新JCheckBox(“使用梯度”);
私有最终JButton firstColor=新JButton(“第一颜色”);
私有最终JButton secondColor=新JButton(“第二种颜色”);
专用最终JLabel线宽标签=新JLabel(“线宽:”);
专用最终JLabel dashLengthLabel=新JLabel(“短划线长度”);
专用最终JTextField线宽字段=新的JTextField(2);
专用最终JTextField dashLengthField=新JTextField(2);
专用最终JCheckBox filled=新JCheckBox(“虚线”);
公共按钮面板()
{
超级();
setLayout(新的FlowLayout());
添加(使用梯度);
添加(第一种颜色);
添加(第二种颜色);
添加(线宽标签);
添加(线宽字段);
添加(dashLengthLabel);
添加(dashLengthField);
增加(填写);
}
} 
公共类PaintPanel扩展了JPanel
{
公共绘画小组()
{
超级();
挫折地面(颜色:白色);
设置大小(700400);
}
}

基本上,这是对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);
    }
}
请记住,布局经理完全有权忽略这些值,因此您需要更好地了解这些经理的工作方式


有关更多详细信息,请参见
公共类topButtonPanel扩展JPanel
请学习常用Java命名法(命名约定-例如
EachWordUpperCaseClass
firstWordLowerCaseMethod()
firstWordLowerCaseAttribute
,除非它是
大写常量
),并一致使用它。但是请注意,这里没有理由扩展
JPanel
,因为没有任何功能在改变-只需使用一个实例即可。