gpt4 book ai didi

java - JPanel 转换,我的代码有什么问题?

转载 作者:太空宇宙 更新时间:2023-11-04 06:59:39 25 4
gpt4 key购买 nike

我是java新手,每天都在学习新东西。今天我偶然发现了一个我无法修复的错误。所以我有一个内部有 JPanel 的 JFrame,现在我想在单击“开始游戏 JLabel”时删除 Jpanel,并使其转换到我的游戏 JPanel(现在我使用测试 JPanel)

JFrame 类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainMenu extends JFrame {

JPanel panel;
JFrame frame;
JButton playlabel;

public void mainmenu() {

frame = new JFrame();
panel = new JPanel();
playlabel = new JButton ("Nieuw Spel");

//frame
frame.setSize(new Dimension(800, 600));
frame.getContentPane().setBackground(new Color(14,36,69));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(frame.getMinimumSize());
frame.setVisible(true);

//panel
Dimension expectedDimension = new Dimension(690, 540);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setPreferredSize(expectedDimension);
panel.setMaximumSize(expectedDimension);
panel.setMinimumSize(expectedDimension);
panel.setBackground(new Color(14, 36, 69));
panel.add(playlabel);
playlabel.setAlignmentX(JComponent.CENTER_ALIGNMENT);

//playlabel
playlabel.setFont(new Font("Old English Text MT", Font.BOLD, 40));
playlabel.setBounds(250, 350, 50, 20);
playlabel.setForeground(new Color(217,144,39));
playlabel.setBackground(new Color(14,36,69));
playlabel.setBorderPainted(false);
playlabel.setFocusPainted(false);
playlabel.addActionListener(new PlayListener());
}
private class PlayListener extends JFrame implements ActionListener {
public void actionPerformed(ActionEvent e) {
JPanel panelgame = Game.Game();
this.remove(panel);
this.add(panelgame);
this.revalidate();
}
}
}

游戏类:

package labyrinthproject.View;
import java.awt.Color;
import javax.swing.JPanel;

public class Game {
public static JPanel Game(){
JPanel panel = new JPanel();
panel.setSize(690, 540);
panel.setBackground(new Color(255,36,69));
return panel;
}
}

如果有人可以向我解释为什么这不起作用,我将不胜感激!非常感谢!

真诚的,一名 Java 初学者。

最佳答案

您的代码中有很多问题

  • 在事件分派(dispatch)线程上创建 GUI
  • 不要扩展 JFrame(那里有三(三!)个 JFrame!)
  • 遵循命名约定
  • 不要过度使用静态方法
  • 仅存储您真正需要代表类状态的实例变量
  • 请勿使用手动 setSizesetBounds 调用。使用 LayoutManager 代替
  • frame.setVisible(true) 的调用应该是框架完全组装后的最后调用
  • 考虑使用 CardLayout 在面板之间切换 ( http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html )

稍微清理了一下,但确切的结构取决于您最终想要实现的目标:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MainMenu extends JPanel
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}

private static void createAndShowGUI()
{
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel mainMenu = new MainMenu();
mainFrame.getContentPane().add(mainMenu);

mainFrame.pack();
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}

MainMenu()
{
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

Dimension expectedDimension = new Dimension(690, 540);
setPreferredSize(expectedDimension);
setBackground(new Color(14, 36, 69));

JButton newGameButton = new JButton ("Nieuw Spel");
newGameButton.setAlignmentX(JComponent.CENTER_ALIGNMENT);
newGameButton.setFont(new Font("Old English Text MT", Font.BOLD, 40));
newGameButton.setForeground(new Color(217,144,39));
newGameButton.setBackground(new Color(14,36,69));
newGameButton.setBorderPainted(false);
newGameButton.setFocusPainted(false);
newGameButton.addActionListener(new PlayListener());
add(newGameButton);
}

private class PlayListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
removeAll();
GamePanel gamePanel = new GamePanel();
add(gamePanel);
revalidate();
}
}
}

class GamePanel extends JPanel
{
GamePanel()
{
setBackground(new Color(255,36,69));
}
}

关于java - JPanel 转换,我的代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22283681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com