[英]How do I make a JButton go from one GUI to the other?
我應該制作一個打開迷宮的代碼。 迷宮代碼工作正常,但我需要一個首先加載的“啟動畫面”,然后打開一個菜單,詢問“你想玩嗎?” 有和沒有選項。 我堅持的部分是制作“是”和“否”按鈕。 當按下“是”時,它應該打開在另一個類中輸入的迷宮 GUI。 有人可以幫我寫一段代碼,里面有一個按鈕可以打開第二堂課嗎?
我在 Java 中使用 Eclipse IDE。
編輯:我把啟動畫面的代碼放在上面,但我不確定我做的是否正確。 我是 Java 的新手,非常感謝您的幫助!
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@SuppressWarnings("serial")
public class SplashScreen extends JFrame implements ActionListener {
Image img = Toolkit.getDefaultToolkit().getImage("images/splash.png");
ImageIcon imgicon = new ImageIcon(img);
public SplashScreen() {
try {
setSize(633, 300);
setLocationRelativeTo(null);
show();
Thread.sleep(5000);
dispose();
int input = JOptionPane.showConfirmDialog(null, "Welcome to the Maze Game! Would you like to play?", "Select an Option...",
JOptionPane.YES_NO_CANCEL_OPTION);
// 0=yes, 1=no, 2=cancel
System.out.println(input);
} catch (Exception exception) {
javax.swing.JOptionPane.showMessageDialog((java.awt.Component) null, "Error" + exception.getMessage(),
"Error:", javax.swing.JOptionPane.YES_NO_OPTION);
}
}
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
}
public static void main(String[] args) {
SplashScreen sp = new SplashScreen();
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
這里有幾件事:我認為您寧願將迷宮添加到 JPanel,然后讓 JPanel 占據整個 JFrame。 這樣你就可以只替換 JPanel - 不需要有多個 JFrames。 還要確保使用事件調度線程運行 GUI,以避免不需要的 GUI 行為(如下所示)。
請參閱下面的示例,我創建了一個簡單的程序,它將根據您從 JOptionPane 中選擇的選項顯示一個(綠色)或兩個(藍色)JPanel:
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* This program has two JPanels - one green and one blue.
* The JOptionPane controls which JPanel is displayed.
*/
public class Test {
private JFrame jf;
private JPanel one;
private JPanel two;
/**
* ALWAYS start a Java GUI from the event-dispatching thread!
* @param args
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test();
}
});
}
/**
* An instance of the program is constructed.
*/
public Test() {
jf = new JFrame("Test");
addComponents(jf.getContentPane());
showOptions();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
jf.pack();
}
/**
* Add components to the pane of our JFrame.
* You probably want to set the layout manager.
* @param pane
*/
public void addComponents(Container pane) {
one = new JPanel();
one.setPreferredSize(new Dimension(100, 100));
one.setBackground(Color.GREEN);
two = new JPanel();
two.setPreferredSize(new Dimension(100, 100));
two.setBackground(Color.BLUE);
// We've added the GREEN JPanel - that's it
pane.add(one);
}
/**
* Show the option pane.
*/
public void showOptions() {
int input = JOptionPane.showConfirmDialog(null, "Show the blue JPanel?", "Select an Option...", JOptionPane.YES_NO_CANCEL_OPTION);
if (input == JOptionPane.YES_OPTION) {
jf.getContentPane().remove(one); // Removing our default green JPanel
jf.getContentPane().add(two); // Adding our shiny new blue JPanel
}
}
}
所以我有一個舊的 Java 項目,我只是看了一下以幫助您。 所以你需要將 2 個 GUI 相互連接。 在你按下是之后,你關閉的那一個我猜會出現另一個。 所以在 GUI 中,當我右鍵單擊按鈕轉到事件、操作並選擇執行的操作時,它會將您重定向到源。 我只有這段代碼:
Array a = new Array();
a.setVisible(true);
a.setDefaultCloseOperation(Array.DISPOSE_ON_CLOSE);
我希望它有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.