图形元素不会出现在JFrame(Java)中

时间:2013-12-23 10:36:40

标签: java swing user-interface jframe

我正在制作一个程序。我创建了一个JFrame,里面有一堆面板,按钮,标签和文本字段。出于某种原因,JFrame会出现,但内部没有任何内容。这是代码:

import javax.swing.*;
import java.awt.*;
import java.awt.Event.*;

public class GUI extends JFrame {

    JButton rect,oval,tri,free,addPoint;
    JLabel xLabel,yLabel;
    JTextField xTextField,yTextField;
    JPanel leftPanel,rightPanel,optionsPanel,pointsPanel;

    public GUI(){
        initUI();
    }

    private void initUI(){

        setLayout(new GridLayout(1,2,5,5));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Graphics Generator");
        setSize(500,500);
        setVisible(true);

        add(leftPanel);
        add(rightPanel);

        leftPanel.setLayout(new GridLayout(2,1,5,5));
        leftPanel.add(optionsPanel);

        optionsPanel.setLayout(new GridLayout(1,4,2,2));

        rect = new JButton("Rectangle");
        oval = new JButton("Oval");
        tri = new JButton("Triangle");
        free = new JButton("Free Shape");

        optionsPanel.add(rect);
        optionsPanel.add(oval);
        optionsPanel.add(tri);
        optionsPanel.add(free);

        leftPanel.add(pointsPanel);

        pointsPanel.setLayout(new GridLayout(1,5,2,2));

        pointsPanel.add(xLabel);
        pointsPanel.add(xTextField);
        pointsPanel.add(yLabel);
        pointsPanel.add(yTextField);
        pointsPanel.add(addPoint);

    }

    public static void main(String[] args) {

        GUI gui = new GUI();

    }

}

3 个答案:

答案 0 :(得分:5)

  • 您的JComponents未初始化,

  • 您已将JComponents添加到已显示JFrame

  • 您必须将代码行setVisible(true);移至构造函数的末尾,

  • 应在Initial Thread

  • 上创建Swing GUI

答案 1 :(得分:1)

您的面板,文本字段或标签均未初始化。我得到了NullPointerException

以下代码将使您的程序运行。但你真的需要研究一些LayoutManagers

private void initUI(){
    leftPanel = new JPanel();
    rightPanel = new JPanel();
    optionsPanel = new JPanel();
    pointsPanel = new JPanel();

    yLabel = new JLabel();
    xLabel = new JLabel();

    xTextField = new JTextField();
    yTextField = new JTextField();

    add(leftPanel);
    add(rightPanel);

    leftPanel.setLayout(new GridLayout(2,1,5,5));
    leftPanel.add(optionsPanel);

    optionsPanel.setLayout(new GridLayout(1,4,2,2));

    rect = new JButton("Rectangle");
    oval = new JButton("Oval");
    tri = new JButton("Triangle");
    free = new JButton("Free Shape");
    addPoint = new JButton("Add Point");

    optionsPanel.add(rect);
    optionsPanel.add(oval);
    optionsPanel.add(tri);
    optionsPanel.add(free);

    leftPanel.add(pointsPanel);

    pointsPanel.setLayout(new GridLayout(1,5,2,2));

    pointsPanel.add(xLabel);
    pointsPanel.add(xTextField);
    pointsPanel.add(yLabel);
    pointsPanel.add(yTextField);
    pointsPanel.add(addPoint);

    setLayout(new GridLayout(1,2,5,5));
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setTitle("Graphics Generator");
    setSize(500,500);
    setVisible(true);

}

答案 2 :(得分:1)

添加完组件后,您应该调用setVisible(true)。在调用revalidate();

之前,它们不会呈现

在尝试使用组件之前,还需要初始化组件。

示例:

leftPanel = new JPanel();
rightPanel = new JPanel();
add(leftPanel);
add(rightPanel);

重复其他组件。