在另一个班级使用jpanel? (noob问题)

时间:2013-09-24 22:12:44

标签: java swing jpanel

我有课程:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;


public class MyTimer extends JFrame {
    static JButton quitButton;
    static JButton quit_2Button;
    public MyTimer() {

        initUI();

    }

    public static void random(){
        int x = (int)(Math.random() * 100 + 1);
        int y = (int)(Math.random() * 150 + 1);
        System.out.println(x);
        System.out.println(y);
        quitButton = new JButton("Press this to quit?");
        quitButton.setBounds(x, y, 200, 20);
    }
    public void initUI() {
        random();
        JPanel panel = new JPanel();

       getContentPane().add(panel);

       panel.setLayout(null);
       JButton quit_2Button = new JButton("Or maybe press this to quit!");
       quit_2Button.setBounds(50, 100, 200, 20);
       quit_2Button.setRolloverEnabled(true);
       quit_2Button.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent event) {
               System.exit(0);
          }
       });
       quitButton.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent event) {
               finder.main(null);
          }
       });


       panel.add(quitButton);
       panel.add(quit_2Button);
       setTitle("Java");
       setSize(300, 200);
       setLocationRelativeTo(null);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

}

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.SwingUtilities;
    import javax.swing.Timer;

    public class timer_run {
        ActionListener al = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel.remove(); //error here
            }
         };
        public static void main(String[] args) {


            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    MyTimer ex = new MyTimer();
                    ex.setVisible(true);
                    new timer_run();

                }

            });

        }
         public timer_run() {
                Timer timer = new Timer(1000, al);
                timer.start();


    }
}

我试图让它在激活actionlistener时调用jpanel.remove(quit_2Button)。问题是,无论何时我尝试这样做都会给我错误"面板无法解决"。我认为发生这种情况的原因是因为timer_run无法访问JPanel框架,但是通过各种试验和错误都无法解决这个问题。对于让timer_run看到JPanel框架,我有什么可能的做法吗?提前谢谢,请注意我是一个java菜鸟,如果可能的话,尽可能简单地回答一下?

1 个答案:

答案 0 :(得分:1)

建议:

  • 一个类不应该尝试直接操作另一个类的变量。
  • 而是让一个类调用另一个类的公共方法。
  • 要允许第二个类执行此操作,您需要将第一个类的对象的引用传递给第二个类的引用。我建议你通过构造函数参数来完成这个。

例如

// class name should begin with an upper case letter
public class TimerRun {
  private MyTimer myTimer;


  public TimerRun(MyTimer myTimer) {
    this.myTimer = myTimer;
  }

  //.... 
}

现在,TimerRun类可以通过调用myTimer.someMethod()

来调用myTimer持有的MyTimer对象上的公共方法