简体   繁体   English

Java JTextField从一个类输出到另一个类?

[英]Java JTextField output from one class to another?

this is a very simple program that I need help with. 这是一个非常简单的程序,我需要帮助。 I'm trying to show the input entered in a JTextField inside of a actionlistener but when I compile the program I get an errors saying "error: cannot find symbol" pointing at this line --String input = field.getText();-- at the second class . 我试图显示在动作侦听器内的JTextField中输入的输入,但是当我编译程序时,出现错误,指出“错误:找不到符号”指向此行--String input = field.getText();- -在第二节课。 I guess its because it doesn't recognize the JTextField name from the first class but how do I get the second class to recognize it? 我猜是因为它无法识别第一类的JTextField名称,但是我如何获得第二类来识别它呢? Please help im trying to learn on my own,and yes im a noob sorry and thank you. 请帮助我尝试自行学习,是的,对不起,谢谢。

ps. ps。 All it has to do is show the input in a system.out.println in the second class. 它要做的就是在第二个类的system.out.println中显示输入。

    import java.awt.* ;
    import java.awt.event.* ;
    import java.sql.* ;
    import java.util.* ;
    import javax.swing.* ;
    import javax.swing.border.* ;
    import javax.swing.event.* ;
    import javax.swing.table.* ;

    class Test
    {
        public static void main(String[] args)
       {

         Test gui = new Test() ;

       }

        public Test()
       {
       JPanel panel1 = new JPanel(new BorderLayout(10,10));  
       JTextField field = new JTextField(22);
       field.addActionListener(new FieldInputAction()) ;     

       panel1.add(field ,BorderLayout.NORTH);

       JFrame f = new JFrame();
       f.setTitle("TEST") ;
       f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);  
       f.setSize(1000, 700) ;
       f.setVisible(true) ;  
       f.setContentPane(panel1) ;

       }

       class FieldInputAction implements ActionListener
       {

          public void actionPerformed(ActionEvent e)
          {  
             String input = field.getText();
             System.out.println(input);
          }


       }





    }

JTextField is declared private. JTextField被声明为私有。 Rewrite class Test so it implements its own ActionListener and then move actionPerformed into class Test and your object will be visible inside class Test scope. 重写Test类,以便它实现自己的ActionListener,然后将actionPerformed移入Test类,您的对象将在Test类的范围内可见。

The reason is that field is out of scope when you call it. 原因是您调用该field超出范围。 Since it's declared inside of the curly braces (that is, the "block") for Test() , it's therefore only accessible within Test() . 由于它是在Test()的花括号(即“块”)中声明的,因此只能在Test()访问。

The simplest way to fix this is just to make field an instance variable. 解决此问题的最简单方法是使field成为实例变量。 So: 所以:

class Test
{
    private JTextField field; //Add this line

    public static void main(String[] args)
    {
    //etc.
    field = new JTextField(22); //Note that we no longer declare it, only initialize it.
    //etc.

I would do the following: 我将执行以下操作:

class Test
{

    private JTextField field; //declare interface components as private
    private JPanel panel1; 
    private JFrame f;
    private String input; //this is the input variable


    public static void main(String[] args)
   {

     Test gui = new Test() ;

   }

    public Test()
   {
   panel1 = new JPanel(new BorderLayout(10,10));  
   field = new JTextField(22);
   field.addActionListener(new FieldInputAction()) ;     

   panel1.add(field ,BorderLayout.NORTH);

   f = new JFrame();
   f.setTitle("TEST") ;
   f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);  
   f.setSize(1000, 700) ;
   f.setVisible(true) ;  
   f.setContentPane(panel1) ;

   }

   class FieldInputAction implements ActionListener
   {

      public void actionPerformed(ActionEvent e)
      {  
             input = field.getText(); 
//if this doesn't work encapsulate the fields. and use the method getfield() with getText()

             System.out.println(input);
          }


   }

}

EDIT: If you want to use the input variable in another class just encapsulate the field and use: system.out.println(""+Text.getInput()) 编辑:如果要在另一个类中使用输入变量,只需封装字段并使用:system.out.println(“” + Text.getInput())

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

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