如何通过单击Java中的按钮将值JFrame传递给JFrame?

时间:2015-04-26 08:07:02

标签: swing jframe

我需要通过JFrame按钮将值从JFrame传递到另一个click。我是编程新手。我尝试了一切。我不知道如何做到这一点。

这是我的代码 -

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.FlowLayout;

public class Demo{
     private final JLabel showInput;
     private final JTextField inputField;

     public Demo(){
          JFrame inputFrame = new JFrame("Input");
          JFrame outputFrame = new JFrame("Output");

          inputField = new JTextField(15);
          JButton button = new JButton("Click here");

               }
          });

          showInput = new JLabel("Type something in the box and press the button");

          inputFrame.getContentPane().setLayout(new FlowLayout());
          inputFrame.getContentPane().add(inputField);
          inputFrame.getContentPane().add(button);
          inputFrame.pack();
          inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          outputFrame.getContentPane().setLayout(new FlowLayout());
          outputFrame.getContentPane().add(showInput);
          outputFrame.pack();
          outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          inputFrame.setVisible(true);
          outputFrame.setVisible(true);
     }

     public static void main(String[] args){
          Demo example = new Demo();
     }
}

1 个答案:

答案 0 :(得分:1)

试试这个。

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Demo{
     private final JLabel showInput;
     private final JTextField inputField;

     public Demo(){
          JFrame inputFrame = new JFrame("Input");
          JFrame outputFrame = new JFrame("Output");

          inputField = new JTextField(15);
          JButton button = new JButton("Click here");
          button.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
                    String textFieldText = inputField.getText();
                    showInput.setText(textFieldText);
               }
          });

          showInput = new JLabel("Type something in the box and press the button");

          inputFrame.getContentPane().setLayout(new FlowLayout());
          inputFrame.getContentPane().add(inputField);
          inputFrame.getContentPane().add(button);
          inputFrame.pack();
          inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          outputFrame.getContentPane().setLayout(new FlowLayout());
          outputFrame.getContentPane().add(showInput);
          outputFrame.pack();
          outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          inputFrame.setVisible(true);
          outputFrame.setVisible(true);
     }

     public static void main(String[] args){
          Demo example = new Demo();
     }
}

我添加这些代码以获得您需要的功能。

/*

button.addActionListener(new ActionListener(){
     // When the button is clicked, actionPerformed is called
     public void actionPerformed(ActionEvent e){
          String textFieldText = inputField.getText(); // Get the text
          showInput.setText(textFieldText); // Set the text on the JLabel
     }
});

*/