java面板无法打开?
我正在做一个吃豆人游戏。计划是有一个显示欢迎页面的GUI和一个写着“播放”的JButton用户按下“播放”按钮后,将打开一个新面板,该面板将显示包含说明的图像。在“说明”面板上,将有一个JButton,显示“继续”这将使用户进入一个GUI,其中有四个JButton,分别为“Easy”、“Medium”、“Hard”和“QUIT”这些按钮的actionPerformed方法已经编码,并且工作正常(新的JFrame将打开,每个级别都有相应的面板,QUIT将退出程序)
我创建它的方式是创建一个名为GUIPanel的类,当GUIPanel对象被实例化时,所有面板都将打开
在我完成了带有级别的GUI之后,我决定添加欢迎面板和说明面板。到目前为止,我已经为欢迎面板编写了代码。我在关卡按钮的代码上方添加了它
我的问题是,无论何时运行它,都会创建一个框架,但没有面板。请帮帮我!!这个项目明天到期
//*****************************
// GUI : Panel
//
// Updated : May 4th, 2017
//
//*****************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
public class GUIPanel extends JPanel
{
private Graphics g;
private BufferedImage buffer;
public GUIPanel()
{
try{
//welcome panel
JPanel welcome = new JPanel();
ImageIcon title = new ImageIcon("welcome.png");
buffer = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
g = buffer.getGraphics();
g.drawImage(title.getImage(), 400, 400, 200, 200, null);
//add button and listener and add onto welcome (JPanel)
JButton play = new JButton("Play");
play.addActionListener(new PlayListener());
welcome.add(play);
}
catch(Exception e){
}
}
private class PlayListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
JFrame playFrame = new JFrame();
//creating the panel
JPanel panel = new JPanel();
add(panel);
/*creating the Easy button which will allow the
user to play the EASY LEVEL of Karelman */
JButton easyButton = new JButton("Easy");
easyButton.setBackground(Color.BLUE);
easyButton.setForeground(Color.WHITE);
easyButton.addActionListener(new EasyListener());
panel.add(easyButton);
/*creating the Medium button which will allow the
user to play the MEDIUM LEVEL of Karelman */
JButton mediumButton = new JButton("Medium");
mediumButton.setBackground(Color.BLUE);
mediumButton.setForeground(Color.WHITE);
mediumButton.addActionListener(new MediumListener());
panel.add(mediumButton);
/*creating the random button which will allow the
user to play the HARD LEVEL of Karelman*/
JButton hardButton = new JButton("Hard");
hardButton.setBackground(Color.BLUE);
hardButton.setForeground(Color.WHITE);
hardButton.addActionListener(new HardListener());
hardButton.setBounds(40, 100, 100, 60);
panel.add(hardButton);
/*creating the quit button for exiting the program*/
JButton quitButton = new JButton("QUIT");
quitButton.setBackground(Color.BLUE);
quitButton.setForeground(Color.WHITE);
quitButton.addActionListener(new QuitListener());
quitButton.setBounds(150, 300, 25, 25);
panel.add(quitButton);
}
catch(Exception i){
}
}
}
/*
EasyListener is attached to the Easy Button
It will open the Easy Level by creating a
new frame which will use Easy Ghosts.
*/
private class EasyListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
JFrame frame = new JFrame("PLAY KARELMAN -- EASY");
frame.setSize(708, 738);
frame.setLocation(350, 0);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//create panel for Easy Level within frame
EasyPanel easy = new EasyPanel();
frame.setContentPane(easy);
}
catch(IOException i){
}
}
}
/*
MediumListener is attached to the Medium Button
It will open the Medium Level by creating a
new frame which will use Medium Ghosts.
*/
private class MediumListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
JFrame frame = new JFrame("PLAY KARELMAN -- MEDIUM");
frame.setSize(708, 738);
frame.setLocation(350, 0);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//create panel for Medium Level within frame
MediumPanel medium = new MediumPanel();
frame.setContentPane(medium);
}
catch(IOException i){
}
}
}
/*
HardListener is attached to the Hard Button
It will open the Hard Level by creating a
new frame which will use Hard Ghosts.
*/
private class HardListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
JFrame frame = new JFrame("PLAY KARELMAN -- HARD");
frame.setSize(708, 738);
frame.setLocation(350, 0);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//create panel for Hard Level within frame
HardPanel hard = new HardPanel();
frame.setContentPane(hard);
}
catch(IOException i){
}
}
}
/*
QuitListener is attached to the Quit Button
It will make a system call to exit the program.
*/
private class QuitListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
}
共 (0) 个答案