JFrame打开为空

Vit*_*nko 1 java swing jframe

我正在尝试创建一个程序,但它没有显示任何JFrame奇怪的内容。代码似乎很合适,但 java 似乎很困惑什么的。到目前为止,这是我的代码:

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`");      
}
}
Run Code Online (Sandbox Code Playgroud)

应该将图像加载imageLabel到容器内,表现出一定的文字label,并label2在底部。ShowPng1()和之间的区别ShowPng2()是 Next 按钮,它位于ShowPng2().

Hov*_*els 5

  1. 您不向 JFrame 添加任何内容。
  2. 您永远不会将 JFrame 设置为可见。
  3. 您将 Swing 事件线程与一个长时间运行的进程捆绑在一起。

  • 您需要向 JFrame 本身添加组件。
  • 添加组件后,您需要将其设置为可见。
  • 您需要在后台线程中运行长时间运行的进程。

  • 您需要阅读 Swing 教程。查看标签,单击信息链接,然后查看其中包含的资源。