无法在jframe

时间:2015-12-06 02:17:45

标签: java swing netbeans methods jframe

我正在使用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);

}

我不知道我的错误在哪里?任何人都可以搞清楚。提前致谢

1 个答案:

答案 0 :(得分:3)

我只是猜测你的问题,但也许是因为你在创建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的绘画方法中绘制,因为这会产生令人讨厌和意想不到的副作用,特别是因为你没有调用超级的绘画方法。