Java - JFrame,JPanel和Components不会显示

时间:2016-04-24 11:43:10

标签: java swing user-interface

我有这个包含GUI的活动。此活动包括输入长度和宽度,x和y(画布中形状的位置)以及选择形状,矩形或圆形的按钮。单击按钮后,它接受文本字段内的长度,宽度,x和y。

手动摆弄GUI代码后,即使设置了大小,JPanels和JFrame也不会显示,只是纯空白。

我希望将画布放在左侧,将标签,文本字段和按钮放在右侧。我究竟做错了什么?我仍然处于该计划的早期阶段,只是审美。谢谢。

public class Problem02{
    Problem02(){
        JFrame framer = new JFrame("Problem 02");
        framer.setLayout(new FlowLayout());
        framer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel LPanel = new  JPanel();
        JPanel RPanel = new JPanel();
        Canvas canvasses = new Canvas();Label XLabel = new Label("X");
        JLabel YLabel = new JLabel("Y");
        JLabel WidthLabel = new JLabel("Width");
        JLabel LengthLabel = new JLabel("Length");
        JButton RectangleButton = new JButton("Rectangle");
        JButton CircleButton = new JButton("Circle");
        JTextField XText = new JTextField("");
        JTextField YText = new JTextField("");
        JTextField WidthText = new JTextField("");
        JTextField LengthText = new JTextField("");

        framer.add(LPanel, BorderLayout.WEST);
        framer.add(RPanel, BorderLayout.EAST);
        LPanel.add(canvasses);
        RPanel.add(XLabel);
        RPanel.add(XText);
        RPanel.add(YLabel);
        RPanel.add(YText);
        RPanel.add(WidthLabel);
        RPanel.add(WidthText);
        RPanel.add(LengthLabel);
        RPanel.add(LengthText);
        RPanel.add(RectangleButton);
        RPanel.add(CircleButton);

        framer.setSize(500,500);
        framer.setTitle("Problem 02");
        framer.setVisible(true);
    }
    public static void main(String[] args) {
     new Problem02();

    }


    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void windowOpened(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void windowClosing(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void windowClosed(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void windowIconified(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void windowDeiconified(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void windowActivated(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void windowDeactivated(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

2 个答案:

答案 0 :(得分:1)

你正在使用JFrame PLUS扩展你正在使用JFrame变量(成帧器)...你将所有组件添加到变量中,但是你没有显示framer,你显示{{1} }。

不要扩展JFrame,将最后一行更改为

this

另外,PLEASE,坚持命名约定:变量的小写字母,你甚至欺骗StackOverflows文本突出显示解析器; - )

答案 1 :(得分:0)

你可以像往常一样从Jframe扩展问题02但是你不必使用对象框架而是你可以这样做     this.setLayout(new FlowLayout());     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

并且不要忘记这一点         this.getContentPane()添加(RPanel)。         this.getContentPane()添加(LPanel);