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();
}
}