Vitalij Kornijenko
Vitalij Kornijenko

Reputation: 559

JFrame opening empty

I'm trying to create a program and it's not showing anything in JFrame which is rather odd. The code seems to fit but java appears to be confused or something. Here's my code so far:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class Img extends JFrame{

/**
 * 
 */
private static final long serialVersionUID = -6362332275268668673L;
static JFrame panel = new JFrame();
private JButton next= new JButton("Next");

public Img(String a, String b){
    ShowPng1(a,b);
}

public void ShowPng1(String a, String b) {
    ImageIcon theImage = new ImageIcon("Icon_Entry_21.png");
    panel.setSize(300, 300);
    panel.setResizable(false);

    JLabel label = new JLabel(a);
    JLabel label2 = null;
    if(!b.isEmpty()){
        label2 = new JLabel("NOTE: " + b);
    }

    JLabel imageLabel = new JLabel(theImage);
    imageLabel.setOpaque(true);

    JPanel p1 = new JPanel(new GridLayout(3, 1));
    p1.add(imageLabel);
    p1.add(label);
    if(label2 != null)p1.add(label2);

    panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel.setVisible(true);
}

public void ShowPng2(String a, String b) {
    ImageIcon theImage = new ImageIcon("Icon_Entry_21.png");
    panel.setSize(300, 300);
    panel.setResizable(false);

    JLabel label = new JLabel(a);
    JLabel label2 = null;
    if(!b.isEmpty()){
        label2 = new JLabel("NOTE: " + b);
    }

    JLabel imageLabel = new JLabel(theImage);
    imageLabel.setOpaque(true);

    JPanel p1 = new JPanel(new GridLayout(3, 1));
    p1.add(imageLabel);
    p1.add(label);
    if(label2 != null)p1.add(label2);
    p1.add(next);

    panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel.setVisible(true);

    try {
        Runtime.getRuntime().exec("cmd /c start mailrugames://play/0.3001");
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, "Error launching client.","Error", JOptionPane.ERROR_MESSAGE);
        System.exit(-1);
    }

}

public void actionPerformed(ActionEvent e) {
    try {
        ShowPng1("Applying patch NOW.","");
        Process p1 = Runtime.getRuntime().exec("cmd /c start Start.bat");
        p1.waitFor();
        JOptionPane.showMessageDialog(null, "Done!","Note", JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);
    } catch (IOException e1) {
        e1.printStackTrace();
        System.exit(-1);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
        System.exit(-1);
    }
}

public static void main(String[] args) throws IOException, InterruptedException {

    Img i = new Img("Preparing client for a patch","");
    Process p1 = Runtime.getRuntime().exec("cmd /c start Clean.bat");
    p1.waitFor();
    Img.panel.dispose();

    i.ShowPng2("Launching client.","Make sure the client is fully patched before closing it and clicking `Next`");      
}
}

It should load an Image imageLabel into a container and show some text label and label2 on the bottom. The difference between ShowPng1() and ShowPng2() is the Next button, it's located in ShowPng2().

Upvotes: 1

Views: 2687

Answers (2)

nachokk
nachokk

Reputation: 14413

1) You are not adding components to the JFrame itself. You forget to add

public class Img extends JFrame{
  .
  .
  public void ShowPng1(String a, String b) {       
       //your code here, don't call panel.setVisible(true) here is not necesary
       this.add(panel);
  }

}

2) Don't call panel.setVisible(true) it's not necessary.. just call i.setVisible(true) in main.

3)To ensure that your code is running in the event dispatch thread wrap it with SwingUtilities.invokeLater(..)

4)You should execute your command in a background thread if it's a long running task cause if not you will block your gui. Read more in Concurrency in swing

5) Follow Java code conventions, method names starts with lower-case with a camel style.

6) Follow @HovercraftFullOfEels advices too.

Take a look to the Swing Tutorial

Upvotes: 4

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285415

  1. You add nothing to the JFrame.
  2. You never set the JFrame to visible.
  3. You tie up the Swing event thread with a long-running process.

  • You need to add components to the JFrame itself.
  • You need to set it visible after components have been added.
  • You need to run your long-running process in a background thread.

  • You need to go through the Swing tutorials. Check out the tag, click on the info link, and check out the resources that it contains.

Upvotes: 5

Related Questions