我正在使用三个班级。第一个 (JFrame) 调用 Splash 类 (JPanel)。一段时间后,我希望将面板删除并替换为 Menu 类中的另一个面板。
我的主类目前是这样的(init方法被同一个类中的main方法调用)...
public void init() throws InterruptedException{
setLayout(new BorderLayout());
Menu menu = new Menu(this);
Splash splash = new Splash(this);
this.getContentPane().add(splash, BorderLayout.CENTER);
validate();
setVisible(true);
Thread.sleep(1000);
this.removeAll();
invalidate();
this.getContentPane().add(menu, BorderLayout.CENTER);
revalidate();
setVisible(true);
}
Splash 类看起来像这样......
public Splash(Survive survive) throws InterruptedException{
Color c1 = new Color(255, 255, 255);
this.setBackground(c1);
JLabel jl1 = new JLabel();
jl1.setIcon(new ImageIcon("res/splash.png"));
this.add(jl1);
}
菜单类如下...
public Menu(Survive survive){
ImageIcon i1 = new ImageIcon("res/title.png");
this.setLayout(new FlowLayout(FlowLayout.CENTER));
this.setLayout(new GridBagLayout());
Color c1 = new Color(96, 96, 96);
this.setBackground(c1);
JButton b0 = new JButton(i1);
b0.setPreferredSize(new Dimension(800, 250));
b0.setRolloverIcon(i1);
b0.setOpaque(false);
b0.setBorderPainted(false);
gbc.gridx = 0;
gbc.gridy = -2;
this.add(b0, gbc);
JButton b1 = new JButton("Singleplayer");
b1.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
gbc.gridx = 0;
gbc.gridy = 1;
this.add(b1, gbc);
JButton b2 = new JButton("Options");
b2.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
gbc.gridx = 0;
gbc.gridy = 2;
this.add(b2, gbc);
JButton b3 = new JButton("Credits");
b3.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
gbc.gridx = 0;
gbc.gridy = 3;
this.add(b3, gbc);
JButton b4 = new JButton("Options");
b4.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
gbc.gridx = 0;
gbc.gridy = 4;
this.add(b4, gbc);
}
这一切的目的是使用swing组件制作一个闪屏。我意识到有更简单的方法可以做到这一点,但我发现这种方法适合我的需要。
任何建议将不胜感激!