簡體   English   中英

我如何將變量從一個jframe傳遞到另一個jframe

[英]how do i pass variable from one jframe to another jframe

我目前在我的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);
}



}

RunOrWalk3中添加一個方法setText(String)來更改TextField中顯示的內容,並在RunOrWalk2btnOK ActionListener中添加一些方法,以調用此方法。

例:

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[])方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM