JFrame打开为空 [英] JFrame opening empty
问题描述
我正在尝试创建一个程序,它在JFrame
中没有显示任何内容,这很奇怪.该代码似乎合适,但Java似乎很混乱.到目前为止,这是我的代码:
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`");
}
}
它应该将图像imageLabel
加载到容器中,并在底部显示一些文本label
和label2
. ShowPng1()
和ShowPng2()
之间的区别是下一步"按钮,它位于ShowPng2()
中.
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()
.
推荐答案
1)您不是要向JFrame
本身添加组件.您忘记添加
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)不必调用panel.setVisible(true)
,没有必要..只需在main中调用i.setVisible(true)
.
2) Don't call panel.setVisible(true)
it's not necessary.. just call i.setVisible(true)
in main.
3)为确保您的代码在事件分发线程中运行,请使用SwingUtilities.invokeLater(..)
3)To ensure that your code is running in the event dispatch thread wrap it with SwingUtilities.invokeLater(..)
4)如果这是一项长期运行的任务,则应在后台线程中执行命令,否则将阻塞gui.在并发并发
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)遵循Java代码约定,方法名称以驼峰样式的小写字母开头.
5) Follow Java code conventions, method names starts with lower-case with a camel style.
6)也遵循@HovercraftFullOfEels的建议.
6) Follow @HovercraftFullOfEels advices too.
看看 Swing教程
这篇关于JFrame打开为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!