簡體   English   中英

打開新 JFrame 的按鈕

[英]Button that opens a new JFrame

我正在編寫一個程序,我應該有一個游戲的標題屏幕,您可以在其中單擊一個“播放”按鈕,打開一個不同的 window 並同時關閉另一個。 我正在嘗試做的是使用 ActionListener 作為按鈕,使當前的 window 不可見,同時使不同的 window 可見。 我很難讓它工作並且遇到很多語法和邏輯錯誤。 我很確定有更好的方法來做到這一點,所以任何指針都將不勝感激。 我正在使用 swing。 免責聲明:初學者 java 級程序員大聲笑:p

我的第一個 class 包括這個(不包括我的進口):


    public static void main(String[] args) {

        //Creating the Frame
        MyFrame menuFrame = new MyFrame();

    // (here i have code for the frame in my actual program)
    }
    static class MyFrame extends JFrame {

        MyFrame () {

            this.setDefaultCloseOperation
                    (JFrame.EXIT_ON_CLOSE);

            this.getContentPane().add(new JLabel(new ImageIcon("logo.png")));
            this.pack();
            this.setVisible(true);

            new EightOff.returnHomeListener (this);

        }
    }
}

我的另一個 class 打開了另一個框架,包括以下按鈕和動作偵聽器,我試圖從GameMenu引用我的框架:

public class EightOff
{
private static JButton returnHome = new JButton("Return to Game Menu");

public static class returnHomeListener implements ActionListener {
        public returnHomeListener(GameMenu.MyFrame myFrame) {

        }

        @Override
        public void actionPerformed(ActionEvent e)
            {
                returnHomeListener (JFrame visibleFrame) {
                visibleFrame.toSetVisible (true);
            }
        }
    }
}

你應該先看看如何使用按鈕、復選框和單選按鈕以及如何編寫動作監聽器

ActionListener應該注冊到按鈕上,所以當某些動作發生時可以通知 if ,例如...

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JButton button = new JButton("Click me");
                button.addActionListener(new TestActionListener());

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(button);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "I'd prfefer if you did't do that");            
        }

    }
}

為了對 object 進行更改,您首先需要對該 object 的引用。 這是非常基本的,Java 101 種東西,您應該查看將信息傳遞給方法或構造函數以獲取更多詳細信息。

您還應該閱讀JFrameJavaDocs ,以更好地了解 class 提供的屬性和功能。

看看如何制作框架也不會受到傷害。

我建議您查看如何使用CardLayout而不是嘗試隱藏/顯示 windows,您通常會有更好的用戶體驗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM