简体   繁体   English

Java NetBeans:如何在NetBeans中向JFrame发送不同的值

[英]Java netbeans : how to send a different value to JFrame in netbeans

so I have a frame 1 and frame 2 in the frame 1 by having 4 and has 1 JButton JTextField at 4 JTextField a user to input an integer value .. after the user input, the user presses a JButton and JFrame will feature 2 .. 因此,我在第1帧中有第1帧,在第2帧中有第2帧,并且在4 JTextField处有1个JButton JTextField用户输入整数值..在用户输入后,用户按下JButton,JFrame将具有2 ..

and in the second frame I have 1 JTextArea which will print out a value that a user input 在第二帧中,我有1个JTextArea,它将打印出用户输入的值

so how to send values ​​from frame 1 to frame 2? 那么如何将值从第1帧发送到第2帧?

inside JFrame 2, Create attributes for user inputs in Jframe 1 and create getters and setters for them. 在JFrame 2中,为Jframe 1中的用户输入创建属性,并为其创建getter和setter。

Inside the method for clicking the jbutton, you should do following, 在单击jbutton的方法内部,您应该执行以下操作:

  1. Create an object of JFrame2 创建一个JFrame2对象
  2. Pass user input values to the Jframe 将用户输入值传递到Jframe

Now take user inputs from textfields and set them inside Jframe2 using setter methods you created. 现在,从文本字段中获取用户输入,并使用创建的setter方法在Jframe2中进行设置。

If you dont understand this well, please post your code. 如果您对此不太了解,请发布您的代码。 It'll help others to make good answers! 它会帮助其他人做出好的答案!

In your JFrame1, 在您的JFrame1中,

To Handle the button action (Note that variable name of the button is jButton1) 处理按钮动作(请注意,按钮的变量名称为jButton1)

     jButton1.addActionListener(new java.awt.event.ActionListener() { //This part can be written inside the constructor.
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
     });

        public void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

           coba ar = new coba();
           double X0 = Double.parseDouble (x0.getText ());
           double X1 = Double.parseDouble (x1.getText ());
           double Y0 = Double.parseDouble (y0.getText ());
           double Y1 = Double.parseDouble (y1.getText ());

           int no = 1; 
           ar.X0 = X0; 
           ar.X1 = X1;
           ar.Y0 = Y0; 
           ar.Y1 = Y1;

           ar.showValues();
      }
  });
  1. In your coba.java 在你的coba.java中

      void showValues() { jTextArea1.setText(""+X0+" "+X1+" "+Y0+" "+Y1); } 

Hope this would help you ! 希望这对您有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM