Reputation: 15
Pretty new to JAVA so having a lot of troubles.
I want to get the values of vdata(string),vshift(interger),vgroup(interger) (all of which are storing the data from JTextField on event of clicking button1)
and store it for passing it to another class for computation.
and I want to use button 2 for closing the GUI.
also I would like to show the computed data in the GUI(should I use JtextArea for that?, the text displayed must be immutable )
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();
}
}
Upvotes: 2
Views: 151
Reputation: 1747
to get the values from your inputs and do a calculation with them after button1 is clicked:
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 ;
}
});
to close the Gui after button2 is clicked :
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
System.exit(0);
}
});
to show the computed Data on your Gui I suggest using Labels :
//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);
Upvotes: 3