框架什么都不显示,也不显示任何错误
Frame shows nothing, neither does it show any error
所以在解决了我的第一个错误后,由于 Hovercraft 的帮助,我终于开始编码并为我的实际项目准备好东西。但是,当我尝试添加标签时,它现在什么也没显示,我的意思是,如果我删除标签并添加任何其他内容(例如按钮等),它会显示按钮,但在我添加 jlabel 的那一刻对于代码,无论标签的 属性 是什么,它都会给我一个完整的空白屏幕。项目代码如下:
主机:
public class Parking_Mania {
public static void main(String []args)throws Exception
{
new GameFrame("Paking Mania");
}
}
游戏框架:
public class GameFrame extends JFrame{
File info=new File("information.txt");
public GameFrame(String name) throws IOException
{
if(!info.exists())
{
info.createNewFile();
}
this.setTitle(name);
this.setSize(640,510);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
Frames frame=new Frames();
this.add(frame);
frame.panel.show(frame, "opening");
}
}
处理改变不同面板的面板 Class:
public class Frames extends JPanel{
CardLayout panel= new CardLayout();
public Frames()
{
this.setLayout(panel);
Opening op=new Opening(this);
//nxtframe nf= new nxtframe(this);
this.add(op, "opening");
//this.add(nf, "nt");
}
}
最后应该在框架上显示的面板:
public class Opening extends JPanel{
private Frames f;
private JLabel bg=new JLabel();
//private JLabel helpframe=new JLabel();
private JButton play=new JButton();
/*private JButton help=new JButton();
private JButton helpclose=new JButton();
private ImageIcon background;*/
public Opening(final Frames f){
this.f=f;
this.setBackground(Color.BLACK);
//this.add(bg);
this.add(play);
//bg.setIcon(new ImageIcon(getClass().getResource("/bg.jpg")));
}
}
应在框架可见之前将组件添加到框架中。
从 Swing tutorial 中的工作示例开始。本教程涵盖了 Swing 的所有基础知识。
我会特别从 How to Use CardLayout
上的教程开始,以便了解和修改第一个演示代码。
本教程将向您展示如何更好地构建代码,这样您就不会一直扩展面板,而只是将组件添加到面板。
所以在解决了我的第一个错误后,由于 Hovercraft 的帮助,我终于开始编码并为我的实际项目准备好东西。但是,当我尝试添加标签时,它现在什么也没显示,我的意思是,如果我删除标签并添加任何其他内容(例如按钮等),它会显示按钮,但在我添加 jlabel 的那一刻对于代码,无论标签的 属性 是什么,它都会给我一个完整的空白屏幕。项目代码如下:
主机:
public class Parking_Mania {
public static void main(String []args)throws Exception
{
new GameFrame("Paking Mania");
}
}
游戏框架:
public class GameFrame extends JFrame{
File info=new File("information.txt");
public GameFrame(String name) throws IOException
{
if(!info.exists())
{
info.createNewFile();
}
this.setTitle(name);
this.setSize(640,510);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
Frames frame=new Frames();
this.add(frame);
frame.panel.show(frame, "opening");
}
}
处理改变不同面板的面板 Class:
public class Frames extends JPanel{
CardLayout panel= new CardLayout();
public Frames()
{
this.setLayout(panel);
Opening op=new Opening(this);
//nxtframe nf= new nxtframe(this);
this.add(op, "opening");
//this.add(nf, "nt");
}
}
最后应该在框架上显示的面板:
public class Opening extends JPanel{
private Frames f;
private JLabel bg=new JLabel();
//private JLabel helpframe=new JLabel();
private JButton play=new JButton();
/*private JButton help=new JButton();
private JButton helpclose=new JButton();
private ImageIcon background;*/
public Opening(final Frames f){
this.f=f;
this.setBackground(Color.BLACK);
//this.add(bg);
this.add(play);
//bg.setIcon(new ImageIcon(getClass().getResource("/bg.jpg")));
}
}
应在框架可见之前将组件添加到框架中。
从 Swing tutorial 中的工作示例开始。本教程涵盖了 Swing 的所有基础知识。
我会特别从 How to Use CardLayout
上的教程开始,以便了解和修改第一个演示代码。
本教程将向您展示如何更好地构建代码,这样您就不会一直扩展面板,而只是将组件添加到面板。