I want to pass a variable from a JFrame to another when I'm pressing the btnCalculeaza
button.
Here is my code:
JButton btnCalculeaza = new JButton("Calculeaza");
btnCalculeaza.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int algad = Integer.parseInt(textField.getText());
int analiza = Integer.parseInt(textField_1.getText());
int be = Integer.parseInt(textField_2.getText());
int depcom = Integer.parseInt(textField_3.getText());
int engleza1 = Integer.parseInt(textField_4.getText());
int engleza2= Integer.parseInt(textField_5.getText());
int fizica= Integer.parseInt(textField_6.getText());
int grafica= Integer.parseInt(textField_7.getText());
int informatica= Integer.parseInt(textField_8.getText());
int matematici= Integer.parseInt(textField_9.getText());
int programare1= Integer.parseInt(textField_10.getText());
int programare2= Integer.parseInt(textField_11.getText());
int tpsm= Integer.parseInt(textField_12.getText());
double nota1;
nota1=(7*algad+analiza*6.0+7*be+3*depcom+2*engleza1+2*engleza2+fizica*7+grafica*3+informatica*4+matematici*6+programare1*5+programare2*4+tpsm*4)/60;
System.out.println(nota1);
new Anul2(nota1).setVisible(true);
Anul2 anul2 =new Anul2();
anul2.setVisible(true);
frame.dispose();
}
});
And the second JFrame
:
public class Anul2 extends JFrame {
private double nota1;
public Anul2(double nota1) {
this.nota1 = nota1;
}
private static final long serialVersionUID = 1L;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Anul2 frame = new Anul2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Anul2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 527, 407);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblMediaGeneralaPana = new JLabel("Media generala pana acum:");
lblMediaGeneralaPana.setBounds(12, 331, 160, 16);
contentPane.add(lblMediaGeneralaPana);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setText(Double.toString(nota1));
lblNewLabel.setBounds(171, 331, 56, 16);
contentPane.add(lblNewLabel);
}
}
I would like to pass the nota1
variable to the second JFrame
(called Anul2
) and then I want to convert it to a JLabel
Two ways,
Your real problem is that your mixing up responsibilities in your code.
You should fully separate your data processing form your UI stuff.
Meaning: you have a local variable
nota1=(7*algad+analiza*6.0+7*be+3*depcom+2*engleza1+2*engleza2+fizica*7+grafica*3+informatica*4+matematici*6+programare1*5+programare2*4+tpsm*4)/60;
and its extremely complicated computation within an action listener. Such things don't belong there.
Instead, you should have a distinct class that only holds all the parameters required for that computation. And then, that class has probably a method computeNota()
.
Now: in your action listener, you create an object of that class, and then you could pass on that object to new JFRame for example.
Decouple the processing of your data from showing it to the user!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.