简体   繁体   English

从一个类到另一个使用JTextField

[英]using JTextField from one class to another

I am trying to use a JTextField from my class A in a class B. I have done some research but I cannot seem to find the answer. 我试图在类B中使用类A中的JTextField。我做了一些研究,但似乎找不到答案。

Here is how my class A looks like: 这是我的A类的样子:

public A extends JFrame implements ActionListener {
  public A(){
    chrono = new JTextField("" + sec); // déclarer final car une classe interne va acceder à ce composant 
    chrono.setHorizontalAlignment(SwingConstants.CENTER);
    chrono.setEditable(false);
    chrono.setFont(new Font("Tahoma", Font.BOLD, 54));
    chrono.setText("");
    chrono.setBounds(631, 11, 127, 72);
    contentPane.add(chrono);
    chrono.setColumns(10);
  }
}

And my class B looks like this: 我的B类看起来像这样:

public class Actions extends Fenetre{
   ActionListener tache_timer;

    tache_timer = new ActionListener(){
        public void actionPerformed(ActionEvent e){
            sec++;
            String textRes = res.getText();
            if(sec == 3 ){
                res.setText("FAIL");

            }

            if(sec == 6){
                /*On realise une pause de 1 sec */
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                EcouteClavier_actionPerformed(e);
                sec = 0;

            }
            chrono.setText("" + sec);// This JTextField is not working


        }
    };
}

The JTextField I am trying to use is called chrono. 我尝试使用的JTextField称为chrono。 How can I use chrono from Class A to Class B 如何从A类到B类使用chrono

You have (at least) two chocies... 您有(至少)两个选择...

You could... 你可以...

Pass a reference of the JTextField , either directly or indirectly via a reference of class A , to class B 通过类A的引用直接或间接将JTextField的引用传递给类B

This is generally not a good idea, as it starts to couple your code together in ways that make it difficult to change later as well as exposing elements of your code to uncontrollable change (once I have a reference to the text field, what's stopping me from removing it from it's parent container?) 通常这不是一个好主意,因为它开始以某种方式将您的代码耦合在一起,从而使以后的更改变得困难,并使代码的元素受到无法控制的更改(一旦我引用了文本字段,是什么阻止了我从它的父容器中删除它?)

You could... 你可以...

Use a Observer Pattern to allow B to notify interested parties that some state has changed. 使用观察者模式允许B通知感兴趣的一方某些状态已更改。 This way B doesn't care who's interested or what they might want to do about the change, making it far more flexible and much easier to use and change in the future. 这样, B不在乎谁对该更改感兴趣,或者他们可能想做什么,这使得B变得更加灵活,将来更容易使用和更改。

ActionListener is an example of an Observer Pattern , which notifies about some event which you need to take action on. ActionListenerObserver Pattern的示例,它通知您需要对其采取操作的某些事件。

You could reuse any of the many EventListener s available in the core API or make your own. 您可以重用核心API中提供的许多EventListener ,也可以自己使用。 You'd then have A implement this listener (directly or indirectly) and register interest of notification with B . 然后,您将让A实现此侦听器(直接或间接),并向B注册通知的兴趣。 When B 's state changes, it would trigger an event notification back to the interested parties... B的状态更改时,它将触发事件通知发回至感兴趣的各方...

You shouldn't ... 不应该 ...

Be tempted to use static !! 尝试使用static

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

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