如何将变量从一个jframe传递到另一个jframe

时间:2017-01-24 13:44:24

标签: java swing

我目前在java应用程序GUI(Eclipse)上遇到一些麻烦,在jframe之间传递值。我的程序是让用户输入他们行进的距离(以公里为单位)到文本框中。根据距离(公里),无论用户输入什么,都可以获得捐赠。 1Km = $ 0.10,所以5km = $ 0.50,我该如何实现?一旦用户在runorwalk2框架上键入5km并单击确定,runorwalk3框架将显示金额$ 0.50。我该如何实现呢?下面是我目前的2帧代码:

public class RunOrWalk2 extends JFrame {

private JPanel contentPane;
private JTextField textField1;
private JTextField textField;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                RunOrWalk2 frame = new RunOrWalk2();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public RunOrWalk2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblNewLabel = new JLabel("Run/Walk");
    lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
    lblNewLabel.setBounds(172, 11, 81, 14);
    contentPane.add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("Distance Travelled");
    lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblNewLabel_1.setBounds(154, 36, 117, 14);
    contentPane.add(lblNewLabel_1);

    textField1 = new JTextField();
    textField1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    textField1.setBounds(154, 61, 115, 40);
    contentPane.add(textField1);
    textField1.setColumns(10);

    JLabel lblNewLabel_2 = new JLabel("Km");
    lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 13));
    lblNewLabel_2.setBounds(279, 74, 46, 14);
    contentPane.add(lblNewLabel_2);

    JLabel lblMaxDonationLimit = new JLabel("Max donation limit for the week");
    lblMaxDonationLimit.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblMaxDonationLimit.setBounds(130, 123, 195, 17);
    contentPane.add(lblMaxDonationLimit);

    textField = new JTextField();
    textField.setFont(new Font("Tahoma", Font.PLAIN, 16));
    textField.setText("        $10");
    textField.setBounds(154, 150, 117, 48);
    contentPane.add(textField);
    textField.setColumns(10);

    JButton btnNewButton = new JButton("Back");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            RunOrWalk frame = new RunOrWalk();
            frame.setVisible(true);
            dispose();
        }
    });
    btnNewButton.setBounds(73, 215, 89, 35);
    contentPane.add(btnNewButton);

    JButton btnOk = new JButton("OK");
    btnOk.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            RunOrWalk3 frame = new RunOrWalk3();
            frame.setVisible(true);
            dispose();
        }
    });
    btnOk.setBounds(273, 215, 89, 35);
    contentPane.add(btnOk);
}

}



public class RunOrWalk3 extends JFrame {

private JPanel contentPane;
private JTextField textField4;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                RunOrWalk3 frame = new RunOrWalk3();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public RunOrWalk3() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblNewLabel = new JLabel("Run/Walk");
    lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
    lblNewLabel.setBounds(173, 11, 76, 14);
    contentPane.add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("Total amount donation earned today");
    lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
    lblNewLabel_1.setBounds(91, 36, 241, 14);
    contentPane.add(lblNewLabel_1);

    textField4 = new JTextField();
    textField4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    textField4.setBounds(117, 59, 184, 92);
    contentPane.add(textField4);
    textField4.setColumns(10);

    JButton btnNewButton = new JButton("OK");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            MainMenu window = new MainMenu();
            window.frame.setVisible(true);
            dispose();
        }
    });
    btnNewButton.setBounds(160, 191, 89, 37);
    contentPane.add(btnNewButton);
}



}

1 个答案:

答案 0 :(得分:1)

setText(String)添加一个方法RunOrWalk3,用于更改TextField中显示的内容,并向btnOK ActionListener中的RunOrWalk2添加内容,以便调用此方法。

示例:

public void actionPerformed(ActionEvent arg0) {
  RunOrWalk frame = new RunOrWalk();
  double km = Double.parseDouble(textField1.getText().replace("km",""));
  //read what is in textField1, remove the "km" and transform into a double.
  km = km * 0.1;
  frame.setText(km + " $");
  frame.setVisible(true);
  dispose();
}
顺便说一句:给你的文本域更好的名字。 正如XtremeBaumer所说,你只需要一个main(String[])方法。