對於我正在製作的遊戲,我試圖用按鈕開始遊戲的簡單標題屏幕。我聽說卡片佈局最適合這種情況,但現在我因爲某種原因實施了它,因此我的輸入被忽略了。以下是該問題的簡化版本,並感謝您的幫助。CardLayout輸入問題Java
MainFrame.java
//package panels.examples;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainFrame extends JFrame implements ActionListener
{
JPanel headerPanel;
JPanel bodyPanel;
JPanel panel1,panel2;
JButton button1,button2;
Container con;
CardLayout clayout;
public MainFrame()
{
//con=getContentPane();
clayout=new CardLayout();
headerPanel=new JPanel();
bodyPanel=new JPanel(clayout);
button1=new JButton("button1");
button2=new JButton("button2");
//add three buttons to headerPanel
headerPanel.add(button1);
headerPanel.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
panel1=new JPanel();
panel1.add(new JLabel("Panel1"));
panel1.setBackground(Color.pink);
panel2=new PrizePanel();
panel2.add(new JLabel("Panel2"));
panel2.setBackground(Color.gray);
//add above three panels to bodyPanel
bodyPanel.add(panel1,"one");
bodyPanel.add(panel2,"two");
setLayout(new BorderLayout());
setSize(800,400);
add(headerPanel,BorderLayout.NORTH);
add(bodyPanel,BorderLayout.CENTER);
// headerPanel.setBounds(0,0,600,100);
bodyPanel.setBounds(0,100, 600, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String args[])
{
new MainFrame();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button1)
{
clayout.show(bodyPanel, "one");
}
else if(e.getSource()==button2)
{
clayout.show(bodyPanel, "two");
}
}
}
PrizePanel.java
//INCLUDE
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.IOException;
//CLASS
public class PrizePanel extends JPanel {
//DECLARATION
private static final int FRAMEX = 800;
private static final int FRAMEY = 400;
private static final Color BACKGROUND = new Color(204, 204, 204);
private BufferedImage myImage;
private Graphics myBuffer;
private Player pd;
private Timer t;
int points = 0;
//CONSTRUCTOR
public PrizePanel()
{
myImage = new BufferedImage(FRAMEX, FRAMEY, BufferedImage.TYPE_INT_RGB);
myBuffer = myImage.getGraphics();
myBuffer.setColor(BACKGROUND);
myBuffer.fillRect(0, 0, FRAMEX,FRAMEY);
int xPos = (int)(Math.random()*(FRAMEX-100) + 50);
int yPos = (int)(Math.random()*(FRAMEY-100)+ 50);
pd = new Player(150,150,50);
t = new Timer((17), new Listener());
t.start();
addKeyListener(new KeyHandler());
setFocusable(true);
}
//PAINT
public void paintComponent(Graphics g)
{
g.drawImage(myImage, 0, 0, getWidth(), getHeight(), null);
}
//LISTENER
private class Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
myBuffer.setColor(BACKGROUND);
myBuffer.fillRect(0,0,FRAMEX,FRAMEY);
pd.move();
pd.draw(myBuffer);
myBuffer.setColor(Color.BLACK);
repaint();
}
}
//MOVE
private class KeyHandler extends KeyAdapter
{
//Ground
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_LEFT){
pd.setdx(-2);
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
pd.setdx(2);
}
if(e.getKeyCode() == KeyEvent.VK_UP){
pd.setY(pd.getY()-1);
pd.setdy(-5.5);
}
}
}
}
嘿感謝,沒想到得到的迴應如此之快,這是現在所有工作。 – user3050810
希望它適合您;) – MadProgrammer