我正在与三个班级合作。第一个(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);
}
所有这一切的目的是使用摇摆组件制作闪屏。我意识到有更简单的方法可以做到这一点,但我发现这种方法可以满足我的需求。
非常感谢任何建议!
答案 0 :(得分:2)
使用一次性javax.swing.Timer
来触发事件,使用CardLayout
来保持/交换组件。