[英]How to transition from JFrame to another JFrame after a button click?
我正在制作游戲的GUI,並且有一個使用JFrame
創建的初始屏幕。 我有一個按鈕,它表示播放,然后我想在按下播放時發生什么,我想切換到另一個JFrame
,它將包含不同的內容。 但是,我不希望窗口關閉並打開另一窗口,而是希望它只從一個框架切換到另一框架。
我沒有GUI的經驗,如果您有任何幫助的信息,將不勝感激。
這是一個例子:
import javax.swing.JFrame;
import javax.swing.JButton;
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setBounds(10, 10, 500, 200);
JButton b1 = new JButton("b1");
b1.addActionListener((c) -> {
buttonPressed(f);
});
f.setContentPane(b1);
f.setVisible(true);
}
private static void buttonPressed(JFrame f) {
JButton b2 = new JButton("b2");
f.setContentPane(b2);
f.revalidate();
}
按下b1時,框架的內容窗格將替換為新按鈕。 需要revalidate()
調用才能使UI在更改后刷新。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.