-1

I am building a Java application using netbeans. I have created a jbutton and and want to run a separate jframe by using it. The problem is on the click of jbutton the jframe is coming but the methods are not executing.

The code at jframe is:

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;                 

}

The code at jbutton is:

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);

}

I am not getting where is my mistake? Can anyone figure it out. Thanks in advance

Uday Verma
  • 99
  • 9
  • 1
    Always best if you could create and post a [mcve] (please read the link), code posted here (not in a link), code we could copy and paste directly into our IDE and test ourselves. This is not a complete code dump, but rather the smallest compilable and runnable program possible that directly demonstrates your problem for us. – Hovercraft Full Of Eels Dec 06 '15 at 02:19
  • 1
    Also, some so called "side" issues, issues unrelated directly to your question at hand but nevertheless important: you probably don't really want to display more than one JFrame. Please see [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636) to see why, and to see alternatives that could be used. Also you never want to draw directly in a JFrame's paint method as you're doing as this can have nasty and unexpected side effects, especially since you don't ever call the super's paint method. Edit: As @AndrewThompson well states above! – Hovercraft Full Of Eels Dec 06 '15 at 02:21
  • 1
    Don't extend from `JFrame`, it locks you into a single use case. Don't override `paint` of top level container, like `JFrame`, there is more going into how a top level container is actually painted then you want to deal with. In both case, start with a `JPanel` and if you need to do custom painting, override `paintComponent` – MadProgrammer Dec 06 '15 at 02:24
  • 1
    You're `while (true)` loops are going to course no end of issues, have a look at [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for reasons why – MadProgrammer Dec 06 '15 at 02:26

1 Answers1

3

I'm just guessing about your problem, but perhaps it's because you don't call important accessory methods after creating your 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();

Also, that start and Hospital methods have long-running while loops, loops that will freeze the GUI's event thread and thus the entire GUI. They should be run in background threads and not on the Swing event thread. Better still would be for you to use a Swing Timer for this "game loop".

Having said this, I'd be remiss if I didn't also mention some so called "side" issues, issues unrelated directly to your question at hand but nevertheless important: you probably don't really want to display more than one JFrame. Please see The Use of Multiple JFrames, Good/Bad Practice? to see why, and to see alternatives that could be used. Also you never want to draw directly in a JFrame's paint method as you're doing as this can have nasty and unexpected side effects, especially since you don't ever call the super's paint method.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373