如何在java中使用按钮

问题描述 投票:0回答:0

我是编程的新手,最近六个月刚刚开始学习,并且断断续续。我的问题是如何让我的按钮将我带到下一个面板以从我的主面板显示我的游戏面板。我有一个开关盒,它应该检查我设置为在单击时启动的按钮的值。但出于某种原因,我不明白我做错了什么。

我迷路了,只需要一些帮助来真正了解我正在犯的错误以及如何改进。这些类中的每一个都是不同的文件。

// Just to start game and nothing else
public class Starter
{
    public static void main(String[] args)
    {
        Window window = new Window();
        window.createWindow();
    }
}

// Has values that can be used for all classes
public class Common
{
    int width, height;
    Font titleFont;
    Font normalFont;
    Color white;
    Color black;
    
    public Common()
    {
        width = 800;
        height = 600;
        titleFont = new Font("Time New Roman", Font.PLAIN, 90);
        normalFont = new Font("Time New Roman", Font.PLAIN, 26);
        white = Color.white;
        black = Color.black;
    }
}

//Class to create just Frame and add to frame
public class Window
{
    JFrame window;
    String title;
    MainPanel mainPanel;
    Common values;
    
    public Window()
    {
        window = new JFrame();
        title = "Game";
        mainPanel = new MainPanel();
        values = new Common();
    }
    
    public void createWindow()
    {
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setTitle(title);
        window.setSize(values.width, values.height);
        window.setLayout(null);
        window.setLocationRelativeTo(null);
        
        window.add(mainPanel.mainPanel());
        mainPanel.createMainPanel();
        
        window.setVisible(true);    
    }
}

//Main Menu Panel
public class MainPanel
{
    JPanel mainPanel;
    Common values;
    JButton startButton;
    JLabel titleLabel;
    Manager manager;
    
    public MainPanel()
    {
        mainPanel = new JPanel();
        values = new Common();
        startButton = new JButton();
        titleLabel = new JLabel();
        manager = new Manager();
    }
    
    public void createMainPanel()
    {
        mainPanel.setBounds(0, 0, values.width, values.height);
        mainPanel.setBackground(Color.black);
        mainPanel.setLayout(null);
        
        startButton.setText("Start");
        startButton.setBounds(350, 400, 100, 50);
        startButton.setFocusable(false);
        startButton.setBackground(values.black);
        startButton.setForeground(values.white);
        startButton.setFont(values.normalFont);
        startButton.setFocusPainted(false);
        startButton.addActionListener(manager);
        startButton.setActionCommand("Start");
        
        titleLabel.setText("Solo Level");
        titleLabel.setBounds(200, 100, 600, 100);
        titleLabel.setFont(values.titleFont);
        titleLabel.setForeground(values.white);
        titleLabel.setBackground(values.black);
        
        //Add everything to Main Panel
        mainPanel.add(titleLabel);
        mainPanel.add(startButton);
    }
    
    public JPanel mainPanel()
    {
        return mainPanel;
    }
}

// Class to control buttons and change panels
public class Manager implements ActionListener
{
    String choice;
    MainPanel mp;
    GamePanel gp;

    public void actionPerformed(ActionEvent e)
    {
        choice = e.getActionCommand();
        
        switch(choice)
        {
            case "Start" :
                changeMainPanel();
                break;
            case "next" : 
                break;
            case "left" : 
                break;
            case "right" :
                break;
        } 
    }
    
    public void changeMainPanel()
    {
        mp.mainPanel.setVisible(false);
        gp.gamePanel.setVisible(true);
    }
}

// Game Panel not working
public class GamePanel
{
    JPanel gamePanel;
    Common values;
    JLabel test;
    
    public GamePanel()
    {
        gamePanel = new JPanel();
        values = new Common();
        test = new JLabel();
    }
    
    public void createGamePanel()
    {
        gamePanel.setBounds(0, 0, values.width, values.height);
        test.setText("This is a test");
        test.setBounds(100, 100, 250, 250);
        gamePanel.add(test);
    }
}
java jframe jpanel jbutton
© www.soinside.com 2019 - 2024. All rights reserved.