我完成了简单的形状游戏。具有3个JPanel的代码包含Start,Game和游戏结束。在代码中一切都很完美但游戏结束我必须包括计算得分的鼠标点击的游戏得分。我不能包括那个得分。这是JPanel的最后一个面板的得分页面。我加入了JButtons,例如{try again}和{exit}。当我按下再次尝试按钮时,我看不到我的第一个Jpanel完美包含开始按钮。我该如何解决? 这是沉重的代码我希望它不是一团糟
public class myshapestry implements ActionListener {
JFrame frame= new JFrame();
MyPanel panel=new MyPanel();
Container l ;
private int ballXSpeed=150;
private int ballSpeed=90;
private int ballX=150;
private int ballY=200;
Color c=Color.BLACK;
JLabel statusbar = new JLabel("This game is developed by Asuskobay");
Startthegame panelstart= new Startthegame();//this is start JPanel
chooselevel panellevel= new chooselevel();//this is difficulties JPanel
thegameover gameover=new thegameover();//End of page
private int mouseclicks;//mouseclicks score
private int mouse;
public myshapestry() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.add(panel);
frame.add(panellevel);
frame.add(gameover);
frame.add(panelstart);
frame.add(statusbar, BorderLayout.SOUTH);
frame.setVisible(true);
frame.setSize(getPreferredSize());
frame.setTitle("Game of Shapes");
HandlerClass handler=new HandlerClass();
panel.addMouseListener(handler);
panel.addMouseMotionListener(handler);
Timer timer=new Timer(100,panel) ;
timer.start();
}
public Dimension getPreferredSize() {
return new Dimension(900,730);
}
private class HandlerClass implements MouseMotionListener, MouseListener {
public void mouseClicked(MouseEvent e){
mouseclicks++;
statusbar.setText(String.format("MouseLoc ( %d, %d ) \t \t Mouse Click \t", e.getX(), e.getY()) + mouseclicks);
mouseclicks=mouse;
}
@Override
public void mousePressed(MouseEvent event) {
if(event.getX() <= (ballX+90) && event.getX() >= (ballX) && event.getY() <= (ballY+90) && event.getY() >= (ballY))
c=Color.RED;
} }
public class MyPanel extends JPanel implements ActionListener {
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(c);
g.fillOval(ballX, ballY, 90 , 90);
}
public void actionPerformed(ActionEvent e) { //balls movement
ballX = ballX + ballXSpeed;
ballY = ballY + ballSpeed;
if(ballY>=730-185){
ballSpeed = -ballSpeed;
}
if(ballX>=900-180){
ballXSpeed = -ballXSpeed;
}
if(ballY<=0+60){
ballSpeed= -ballSpeed;
}
if (ballX<=0+60){
ballXSpeed= -ballXSpeed;
}
repaint();
if(c==Color.RED){
frame.remove(panel);
frame.add(gameover);
frame.validate();
}}}
public class chooselevel extends JPanel implements ActionListener {
private JButton easy;
private JButton normal;
private JButton hard;
public chooselevel(){
setLayout(null);
easy=new JButton("Easy");
normal=new JButton("Normal");
hard=new JButton("Hard");
easy.setBounds(380,330,90,20);
normal.setBounds(380,370,90,20);
hard.setBounds(380,410,90,20);
add(hard);
add(easy);
add(normal);
hard.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent l){
String level=l.getActionCommand();
if(level.equals("Hard")){
ballXSpeed=190;
ballSpeed=184;
frame.remove(panellevel);
frame.add(panel);
frame.validate();
}} });
normal.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent l){
String level=l.getActionCommand();
if(level.equals("Normal")){
ballXSpeed=157;
ballSpeed=123;
frame.remove(panellevel);
frame.add(panel);
frame.validate();
}}});
easy.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent l){
String level=l.getActionCommand();
if(level.equals("Easy")){
ballXSpeed=85;
ballSpeed=74;
frame.remove(panellevel);
frame.add(panel);
frame.validate();
}} });
}
public Dimension getPreferredSize() {
return new Dimension(900,730);
} }
public class Startthegame extends JPanel implements ActionListener {
private JButton Start;
private JButton Exit;
public Startthegame() {
setLayout(null);
Start = new JButton("Start");
Exit = new JButton("Exit");
Start.setBounds(380,330,70,20);
Exit.setBounds(380,370,70,20);
add(Start);
add(Exit);
Exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent i){
String action= i.getActionCommand();
if(action.equals("Exit")) {
System.exit(0);
}}
});
Start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent i){
String action= i.getActionCommand();
if(action.equals("Start")) {
frame.remove(panelstart);
frame.add(panellevel);
frame.validate();
repaint();
} }});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(900, 730);
} }
public class thegameover extends JPanel {
private JLabel gameisover;
private JLabel yourscore;
private JButton tryagain;
private JButton Exit;
thegameover(){
setLayout(null);
gameisover= new JLabel("GAME OVER");
yourscore=new JLabel();
add(gameisover);
add(yourscore);
gameisover.setBounds(170,200,600,70);
yourscore.setBounds(380,380,120,70);
gameisover.setFont(new Font(null, Font.PLAIN, 90));
yourscore.setFont(new Font(null, Font.PLAIN, 20));
yourscore.setText ("Your Score " + mouseclicks);
tryagain=new JButton("Try Again");
Exit=new JButton("Exit");
add(tryagain);
add(Exit);
tryagain.setBounds(280,550,100,20);
Exit.setBounds(490,550,100,20);
tryagain.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ac){
String tryit=ac.getActionCommand();
if(tryit.equals("Try Again")){
frame.remove(gameover);
frame.add(panelstart);
frame.validate();
repaint();
}
}
});
}
public Dimension getPreferredSize(){
return new Dimension(900,73);
}}
public static void main (String args[]){
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
myshapestry tr=new myshapestry();
}
});
} }
编辑后:
我已经创建了新的Jpanel来收集其中的所有Jpanels以在Frame中添加状态栏,但是我发现错误出错了
public class myshapestry implements ActionListener {
collectionpanel collectpanel= new collectionpanel();
public myshapestry() {
frame.add(collectpanel);
public class collectionpanel extends JPanel {
collectionpanel(){
cardlayout= new CardLayout();
this.setLayout(cardlayout);
this.add(panel, "game");
this.add(panellevel,"level");
this.add(gameover, "over");
this.add(panelstart, "start");
cardlayout.show(collectpanel, "game");
cardlayout.show(collectpanel, "level");
cardlayout.show(collectpanel, "start");
cardlayout.show(collectpanel, "over");
} }
错误;
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: wrong parent for CardLayout
at java.awt.CardLayout.checkLayout(Unknown Source)
at java.awt.CardLayout.show(Unknown Source)
at example.myshapestry$collectionpanel.<init>(myshapestry.java:100)
at example.myshapestry.<init>(myshapestry.java:58)
at example.myshapestry$1.run(myshapestry.java:393)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:2)
存在一系列问题,所有这些问题共同导致混乱......
Timer
,这意味着当您尝试将游戏显示在&#34;屏幕或尝试重新启动,&#34;游戏面板&#34;仍在尝试更新屏幕...... ,而不是...
Timer
。当你发现游戏是&#34; over&#34;时,在你显示&#34; over&#34;屏幕,停止Timer
CardLayout
。有关详细信息,请参阅How to Use CardLayout 例如......
创建CardLayout
实例字段,并将Timer
实例字段设为
public class MyShapes {//implements ActionListener {
//...
private CardLayout cardLayout;
private Timer timer = new Timer(100, panel);
将CardLayout
应用于frame
并为每个视图命名...
public MyShapes() {
//...
cardLayout = new CardLayout();
frame.setLayout(cardLayout);
frame.add(panel, "game");
frame.add(panellevel, "level");
frame.add(gameover, "over");
frame.add(panelstart, "start");
frame.add(statusbar, BorderLayout.SOUTH);
cardLayout.show(frame.getContentPane(), "start");
//...
}
当用户选择&#34;开始&#34;时,使用CardLayout
Start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent i) {
String action = i.getActionCommand();
if (action.equals("Start")) {
//frame.remove(panelstart);
//frame.add(panellevel);
//frame.validate();
cardLayout.show(frame.getContentPane(), "level");
}
}
});
当您开始游戏时,请启动Timer
并使用CardLayout
切换视图...
hard.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent l) {
String level = l.getActionCommand();
if (level.equals("Hard")) {
ballXSpeed = 190;
ballSpeed = 184;
//frame.remove(panellevel);
//frame.add(panel);
//frame.validate();
timer.start();
cardLayout.show(frame.getContentPane(), "game");
}
}
});
当你发现&#34;游戏结束时#34;条件,停止Timer
并使用CardLayout
切换视图...
if (c == Color.RED) {
//frame.remove(panel);
//frame.add(gameover);
//frame.validate();
timer.stop();
cardLayout.show(frame.getContentPane(), "over");
}
现在,评分问题......
创建thegameover
面板时,您使用yourscore.setText("Your Score " + mouseclicks);
,但创建视图时mouseclicks
为0
,您需要更新此值...
向类thegameover
添加一个方法,称为setScore
,并相应地更新标签...
public void setScore(int score) {
yourscore.setText("Your Score " + score);
}
当您检测到游戏超过条件时,请更新分数...
timer.stop();
gameover.setScore(mouseclicks);
cardLayout.show(frame.getContentPane(), "over");
您可能希望阅读Code Conventions for the Java TM Programming Language,这样可以让人们更轻松地阅读您的代码并让您阅读其他代码
时也值得