如何存储 JTextField 中的值并传递给另一个 class 进行计算
how to store the values from JTextField and pass to another class for computation
JAVA 还很陌生,遇到了很多麻烦。
我想获取的值
vdata(字符串)、vshift(整数)、vgroup(整数)
(所有这些都在单击按钮 1 时存储来自 JTextField 的数据)
并存储它以将其传递给另一个class进行计算。
并且我想使用按钮 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();
}
}
从您的输入中获取值并在单击 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);
JAVA 还很陌生,遇到了很多麻烦。
我想获取的值 vdata(字符串)、vshift(整数)、vgroup(整数) (所有这些都在单击按钮 1 时存储来自 JTextField 的数据)
并存储它以将其传递给另一个class进行计算。
并且我想使用按钮 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();
}
}
从您的输入中获取值并在单击 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);