简体   繁体   English

将变量从JFrame传递到另一个

[英]Passing a varable from a JFrame to another

I want to pass a variable from a JFrame to another when I'm pressing the btnCalculeaza button. 我想在按下btnCalculeaza按钮时将变量从JFrame传递到另一个变量。

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 : 第二个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 我想将nota1变量传递给第二个JFrame (称为Anul2 ),然后将其转换为JLabel

Two ways, 两种方式

  1. Pass a reference of the first JFrame to the second JFrame Constructor. 将第一个JFrame的引用传递给第二个JFrame构造函数。
  2. Use getFrames() method along with getDeclaredFields() to get the nota1 variable value. 将getFrames()方法与getDeclaredFields()一起使用以获取nota1变量值。

Your real problem is that your mixing up responsibilities in your code. 您真正的问题是您在代码中混淆了职责

You should fully separate your data processing form your UI stuff. 您应该将数据处理与UI内容完全分开。

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() . 然后,该类可能有一个方法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. 现在:在动作侦听器中,创建该类的对象,然后可以将该对象传递给新的JFRame。

Decouple the processing of your data from showing it to the user! 将数据处理与显示给用户脱钩!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM