【问题标题】:how to store the values from JTextField and pass to another class for computation如何存储来自 JTextField 的值并传递给另一个类进行计算
【发布时间】:2020-09-30 18:29:33
【问题描述】:

对 JAVA 很陌生,所以遇到了很多麻烦。

我想获取 vdata(字符串),vshift(整数),vgroup(整数) (所有这些都在单击button1时存储来自JTextField的数据)

并存储它以将其传递给另一个类进行计算。

我想使用按钮 2 来关闭 GUI。

我还想在 GUI 中显示计算数据(我应该使用 JtextArea 吗?显示的文本必须是不可变的)

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;



public class GUI {
 private JFrame frame;
 private JPanel panel,buttonPanel;
 private JLabel label,blank1,blank2;
 private JTextArea textArea;
 //private JOptionPane input;
 private JButton button1,button2;
 private JTextField data,shift,group;
 private String vdata,EGdata;
 private int vshift,vgroup;


 public GUI(){
     frame= new JFrame("Crypto");

     data=new JTextField("Enter the Data to be Encrypted");
     shift=new JTextField("Enter the shift value");
     group=new JTextField("Enter the grouping value");

     data.addFocusListener(new FocusAdapter() {
         public void focusGained(FocusEvent e) {
             JTextField source = (JTextField)e.getComponent();
             source.setText("");
             source.removeFocusListener(this);
         }
     });



     shift.addFocusListener(new FocusAdapter() {
         public void focusGained(FocusEvent e) {
             JTextField source = (JTextField)e.getComponent();
             source.setText("");
             source.removeFocusListener(this);
         }
     });

     group.addFocusListener(new FocusAdapter() {
         public void focusGained(FocusEvent e) {
             JTextField source = (JTextField)e.getComponent();
             source.setText("");
             source.removeFocusListener(this);
         }
     });

     button1=new JButton("Generate");
     button2=new JButton("Cancel");
     button1.setBounds(0,0,50,30);
     button2.setBounds(0,0,50,30);


     button1.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent ae){
             vdata = data.getText();
             vshift=Integer.parseInt(shift.getText());
             vgroup=Integer.parseInt(group.getText());


         }
     });


     blank2=new JLabel("");
     blank1=new JLabel("");
     label=new JLabel("The Encrypted Data is:");

     textArea=new JTextArea(EGdata);

     panel=new JPanel();
     panel.setBorder(BorderFactory.createEmptyBorder(30,20,10,20));
     panel.setLayout(new GridLayout(10,1));
     panel.setBackground(Color.GRAY);

     panel.add(blank2);
     panel.add(data);
     panel.add(shift);
     panel.add(group);


     panel.add(blank1);
     panel.add(label);

     panel.add(textArea);

     panel.add(button1);
     panel.add(button2);


     frame.add(panel, BorderLayout.CENTER);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



     frame.pack();
     frame.setVisible(true);





 }
 public static void main(String[] args){
     new GUI();


 }


}

【问题讨论】:

    标签: java swing class return-value jtextfield


    【解决方案1】:

    从您的输入中获取值并在单击 button1 后对其进行计算:

        button1.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent ae){
             vdata = data.getText();
             vshift=Integer.parseInt(shift.getText());
             vgroup=Integer.parseInt(group.getText());
             /*call your methode to calculate this is an example of 
             calculating vshift + vgroup */
             int res = compute (vshift,vgroup) ;
             //give the label the result after convertingthe result to string
             result.setText(String.valueOf(res));
         }
    
         private int compute(int vshift, int vgroup) {
             //compute what you want in here
             int addition ;
             addition = vshift+vgroup ;
             return addition ;
         }
     });
    

    在 button2 被点击后关闭 Gui:

    button2.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent ae){
           System.exit(0);
         }
     });
    

    为了在你的 Gui 上显示计算的数据,我建议使用标签:

    //change this line : textArea=new JTextArea(EGdata);
    //by the following :
    result=new JLabel();
    result.setText(EGdata);
    //add label to Gui :
     panel.add(result);
     // and remove textarea from you Gui
      // remove this line --> panel.add(textArea);
    

    【讨论】:

    • 谢谢。这很有帮助。
    猜你喜欢
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 2014-02-24
    相关资源
    最近更新 更多