【问题标题】:Unable to run methods in a jframe无法在 jframe 中运行方法
【发布时间】:2015-12-06 02:17:45
【问题描述】:

我正在使用 netbeans 构建一个 Java 应用程序。我创建了一个 jbutton,并希望通过使用它来运行一个单独的 jframe。问题在于点击 jbutton 时 jframe 即将到来,但方法未执行。

jframe处的代码是:

public class DroneJFrame extends javax.swing.JFrame {

private int x;
private int y;
private String text;
private int a;
private int b;
private String text1;
private int c;
private int d;
private String text2;

public DroneJFrame() {
        initComponents();

        x= 10;
        y=150;
        a=10;
        b=150;
        c=1000;
        d=150;
        text="Drone";
        text1="Factory";
        text2="Hospital";
        setSize(1070,300);

        this.getContentPane().add(jPanel1);
}

@Override
public void paint(Graphics g) {
    g.setColor(Color.white);
    g.fillRect(0, 0, 1070, 300);
    g.setColor(Color.BLACK);
    g.drawString(text, x, y);
    System.out.println(x + "" + y);

    g.drawString(text1, a, b);

    g.drawString(text2, c, d);

}

public void factory() throws InterruptedException{
    while(true){
        while(a<=getWidth()){
                b=getHeight()/2;
                repaint();
                Thread.sleep(1000000);
            }
        }
    }

public void Hospital() throws InterruptedException{
    while(true){
        while(c<=getWidth()){
                d=getHeight()/2;
                repaint();
                Thread.sleep(1000000);
            }
    }
}

public void start() throws InterruptedException{
    while(true){
        while(x <= getWidth()-50){
                x++;
                y = getHeight()/2;
                repaint();
                Thread.sleep(50);
        }
        JOptionPane.showMessageDialog(null, "Drone Has Been Reached To Hospital");
        while(x >= 0){
                x--;
                y = getHeight()/2;
                repaint();
                Thread.sleep(50);
            }
        break;
        }

    }

public static void main(String args[]) throws InterruptedException {
            DroneJFrame drone = new DroneJFrame();
            drone.start();
            drone.factory();
            drone.Hospital();

            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(DroneJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(DroneJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(DroneJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(DroneJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }                
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    }               
            });
}                  
private javax.swing.JPanel jPanel1;                 

}

jbutton处的代码是:

private void droneActionPerformed(java.awt.event.ActionEvent evt) {                                      
    DroneJFrame drone = new DroneJFrame();
    drone.setVisible(true);
    drone.setLocation(180,200);
    drone.setDefaultCloseOperation(DroneJFrame.HIDE_ON_CLOSE);

}

我不明白我的错误在哪里?任何人都可以弄清楚。提前致谢

【问题讨论】:

  • 如果您可以创建并发布minimal reproducible example(请阅读链接)、此处发布的代码(不在链接中)、我们可以直接复制并粘贴到我们的 IDE 中并自行测试的代码,那总是最好的.这不是完整的代码转储,而是可以直接向我们展示您的问题的最小可编译和可运行程序。
  • 另外,一些所谓的“侧面”问题,与您手头的问题无关但仍然很重要的问题:您可能真的不想显示多个 JFrame。请参阅The Use of Multiple JFrames, Good/Bad Practice? 了解原因以及可以使用的替代方案。此外,您永远不想像您正在做的那样直接在 JFrame 的paint 方法中进行绘制,因为这可能会产生令人讨厌和意想不到的副作用,尤其是因为您从未调用过 super 的 paint 方法。编辑:正如@AndrewThompson 上面所说的那样!
  • 不要从JFrame 扩展,它会将您锁定在一个用例中。不要覆盖顶级容器的paint,如JFrame,还有更多关于如何实际绘制顶级容器的内容,然后再处理。在这两种情况下,都以JPanel 开头,如果您需要进行自定义绘画,请覆盖paintComponent
  • 顺便说一句-从您的个人资料中,我注意到在之前的问题中,只有一个没有答案,但其余的都没有accepted answer!这是为什么?如果 SO 对我来说效果很差,以至于我从来没有得到正确的答案,我会寻找另一个可能的答案来源..
  • 你的while (true) 循环肯定会出现问题,请查看Concurrency in Swing 了解原因

标签: java swing netbeans methods jframe


【解决方案1】:

我只是猜测您的问题,但可能是因为您在创建 GUI 后没有调用重要的辅助方法:

// you call this:
DroneJFrame drone = new DroneJFrame();

// but I don't see you calling these methods in your action listener:
drone.start();
drone.factory();
drone.Hospital();

此外,start 和 Hospital 方法具有长时间运行的 while 循环,这些循环将冻结 GUI 的事件线程,从而冻结整个 GUI。它们应该在后台线程中运行,而不是在 Swing 事件线程中。对于这个“游戏循环”,您最好使用 Swing Timer。

话虽如此,如果我还没有提到一些所谓的“侧面”问题,这些问题与您手头的问题无关但仍然很重要,那我就失职了:您可能真的不想展示超过一个 JFrame。请参阅The Use of Multiple JFrames, Good/Bad Practice? 了解原因以及可以使用的替代方案。此外,您永远不想像您正在做的那样直接在 JFrame 的paint 方法中进行绘制,因为这可能会产生令人讨厌和意想不到的副作用,尤其是因为您从未调用过 super 的 paint 方法。

【讨论】:

  • 而且...现在 EDT 已被阻止:P
  • @MadProgrammer:在我看到你的帖子之前进行编辑。请阅读编辑。
  • @MadProgrammer:非常感谢!
  • drone.start();无人机.工厂();无人机.医院();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多