Java - JFrame,JPanel和Components将不会显示

时间:2023-01-27 23:49:26

I have this activity that includes GUI. This activity includes inputting the length and width, x and y (location of the shape in the canvas) and the button which chooses the shape, rectangle or circle. After clicking the button, it then accept the length, width, x and y inside the textfield.

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

After fiddling with the GUI code manually, the JPanels and JFrames won't show even after setting the size, just pure blank.

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

I was expecting to put the canvas on the left and the labels, textfields and buttons on the right. What am I doing wrong? I'm still at the early part of the program just aesthetic. Thank you.

我期待把画布放在左边,标签,文本字段和按钮放在右边。我究竟做错了什么?我仍处于该项目的早期阶段,只是审美。谢谢。

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 个解决方案

#1


1  

You're extending from JFrame PLUS you're using a JFrame variable (framer)... you add all the components to the variable, but you don't show framer, you show this.

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

Do not extend JFrame, an change the last lines to

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

    framer.setSize(500,500);
    framer.setTitle("Problem 02");
    framer.setVisible(true);

Plus, PLEASE, stick to naming conventions: lowercase letters for variables, you even trick *s text highlighting parser ;-)

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

#2


0  

you can leave Problem02 extended from Jframe like you did but then you don't have to use object frame and instead you can do that
this.setLayout(new FlowLayout()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

你可以像你一样从Jframe扩展Problem02,但是你不必使用对象框架而是你可以做到这一点.setLayout(new FlowLayout()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

and don't forget this this.getContentPane().add(RPanel); this.getContentPane().add(LPanel);

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

#1


1  

You're extending from JFrame PLUS you're using a JFrame variable (framer)... you add all the components to the variable, but you don't show framer, you show this.

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

Do not extend JFrame, an change the last lines to

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

    framer.setSize(500,500);
    framer.setTitle("Problem 02");
    framer.setVisible(true);

Plus, PLEASE, stick to naming conventions: lowercase letters for variables, you even trick *s text highlighting parser ;-)

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

#2


0  

you can leave Problem02 extended from Jframe like you did but then you don't have to use object frame and instead you can do that
this.setLayout(new FlowLayout()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

你可以像你一样从Jframe扩展Problem02,但是你不必使用对象框架而是你可以做到这一点.setLayout(new FlowLayout()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

and don't forget this this.getContentPane().add(RPanel); this.getContentPane().add(LPanel);

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