【问题标题】:How can I print the text I typed in my first class to my second class?如何将我在第一堂课中输入的文本打印到第二堂课?
【发布时间】:2017-04-03 19:40:43
【问题描述】:

有人请帮助我吗?如何将我在文本字段中输入的文本打印到另一个类中?事情与获取和设置文本有关吗?这是我的第一堂课

public class Name extends JFrame {

JFrame frame = new JFrame ();
JPanel panel1 = new JPanel();
JLabel yourname = new JLabel("Name");
JTextField text = new JTextField(30);


public Name(){

    add(yourname);
    add(text);

    setLayout(new FlowLayout());
    setVisible(true);
    setSize(900,600);

}


public static void main(String[] args) {

    Name go = new Name();
   go.setVisible(true);
   go.setDefaultCloseOperation(EXIT_ON_CLOSE);
   go.setLocationRelativeTo(null);
}}

这是我的第二节课

public class Output extends JFrame {
JFrame frame1 = new JFrame();

Output(){

    add(yourname); 


}

public static void main(String args[]){

    Output receipt = new Output ();
    receipt.setVisible(true);


}}

【问题讨论】:

    标签: java swing class actionlistener jtextfield


    【解决方案1】:

    您需要收听JTextField上的更改。

    类似:

    public class ListenerForTextField extends JFrame {
      JTextField text = new JTextField("Some Value");
    
      public ListenerForTextField () {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        text.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            //SomeOtherClass a = new SomeOtherClass();
            //a.useText(text.getText());
          }
        });
    }
    

    【讨论】:

    • @Charity Rama - 如果此答案或任何其他答案解决了您的问题,请将其标记为已接受。欢迎来到 Stack Overflow!
    猜你喜欢
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多