Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.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 不能';t在运行时根据用户输入参数绘制矩形_Java_Swing_User Interface - Fatal编程技术网

Java 不能';t在运行时根据用户输入参数绘制矩形

Java 不能';t在运行时根据用户输入参数绘制矩形,java,swing,user-interface,Java,Swing,User Interface,我试图从方法paint(Graphics g)中的JTextFields中获取4个整数参数。我正在将用户输入解析为整数,然后尝试将其解析为方法`g.drawRect(x1,y1,x2,y2),但当我运行应用程序时,我没有得到任何矩形。下面是代码: 点击按钮: btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {

我试图从方法
paint(Graphics g)
中的JTextFields中获取4个整数参数。我正在将用户输入解析为整数,然后尝试将其解析为方法`g.drawRect(x1,y1,x2,y2),但当我运行应用程序时,我没有得到任何矩形。下面是代码:

点击按钮:

btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            Figure();
        }
    });


 public void paint(Graphics g) {

     int x1 = Integer.parseInt(textField.getText().toString());
     int y1 = Integer.parseInt(textField_1.getText().toString());
     int x2 = Integer.parseInt(textField_2.getText().toString());
     int y2 = Integer.parseInt(textField_3.getText().toString());


        g.drawRect (x1, y1, x2, y2);  
      }

 private void Figure()
 {
     JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setBounds(30, 30, 300, 300);
        window.getContentPane().add(new Form());
        window.setVisible(true);
 }

谢谢你的建议

1)为了更快地获得更好的帮助,请发布or。2) 请学习常见的Java命名法(命名约定-例如
EachWordUpperCaseClass
firstWordLowerCaseMethod()
firstWordLowerCaseAttribute
,除非它是
大写常量
),并一致使用它。3) “我试图从JTextFields中获取4个整数参数”为了用户的利益,为他们提供4个
JSpinner
组件,使用4个微调器数字模型。好吧,你似乎是在用魔法编码(编写一些代码并希望它能用魔法工作……我应该知道,我经常这么做;P)。首先看一看和了解更多关于Swing中绘画如何实际工作的细节