伙计我在这里有两节课。当我使用来自我的其他类的jbutton调用类游戏的主要方法时,只会出现带有白色屏幕的帧。这是代码。谢谢您的帮助。第一节
@SuppressWarnings("serial")
public class Game extends JPanel {
Ball ball = new Ball(this);
Racquet racquet = new Racquet(this);
int score = 0;
int speed = 1;
private int getScore() {
return score;
}
private int getSpeed(){
return speed;
}
public Game() {
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
racquet.keyReleased(e);
}
@Override
public void keyPressed(KeyEvent e) {
racquet.keyPressed(e);
}
});
setFocusable(true);
}
public void move() {
ball.move();
racquet.move();
}
@Override
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d);
racquet.paint(g2d);
g2d.setColor(Color.black);
g2d.setFont(new Font("Showcard Gothic", Font.BOLD, 20));
g2d.drawString(String.valueOf("Your Score:" + getScore()), 340, 30);
g2d.drawString(String.valueOf("Game Speed:" + getSpeed()), 340, 220);
g2d.drawLine(300, 400, 300, -50);
}
public void gameOver() {
JOptionPane.showMessageDialog(this, "your score is: " + getScore(),
"Game Over", JOptionPane.YES_NO_OPTION);
System.exit(ABORT);
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Mini Tennis");
Game game = new Game();
frame.add(game);
frame.setSize(550, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
while (true) {
game.move();
game.repaint();
Thread.sleep(10);
}
}
}
第二课
public class Main extends JFrame implements ActionListener{
JPanel mainPanel;
JLabel title;
Color bgColor = new Color(51,137,237);
JButton startBtn, regBtn, viewBtn, exitBtn;
public Main(){
setSize(550, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
mainPanel();
setVisible(true);
}
void mainPanel(){
mainPanel = new JPanel();
add(mainPanel);
mainPanel.setBackground(bgColor);
mainPanel.setLayout(null);
title = new JLabel("Ball Catcher");
mainPanel.add(title);
title.setBounds(130,1,500,200);
title.setForeground(Color.white);
title.setFont(new Font("Showcard Gothic", Font.BOLD, 35));
startBtn = new JButton("START");
mainPanel.add(startBtn);
startBtn.setBounds(200,150,130,40);
startBtn.addActionListener(this);
startBtn.setForeground(Color.WHITE);
startBtn.setFont(new Font("Showcard Gothic", Font.BOLD, 20));
startBtn.setBackground(bgColor);
regBtn = new JButton("REGISTER");
mainPanel.add(regBtn);
regBtn.setBounds(200,200,130,40);
regBtn.setForeground(Color.WHITE);
regBtn.addActionListener(this);
regBtn.setFont(new Font("Showcard Gothic", Font.BOLD, 15));
regBtn.setBackground(bgColor);
viewBtn = new JButton("VIEW SCORE");
mainPanel.add(viewBtn);
viewBtn.setBounds(200,250,130,40);
viewBtn.setForeground(Color.WHITE);
viewBtn.setFont(new Font("Showcard Gothic", Font.BOLD, 15));
viewBtn.setBackground(bgColor);
exitBtn = new JButton("EXIT");
mainPanel.add( exitBtn);
exitBtn.setBounds(200,300,130,40);
exitBtn.setForeground(Color.WHITE);
exitBtn.setFont(new Font("Showcard Gothic", Font.BOLD, 15));
exitBtn.setBackground(bgColor);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == startBtn){
String[] args={};
try {
Game.main(args);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if(e.getSource() == regBtn){
new Register();
dispose();
}
}
public static void main(String[] args){
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception ex)
{
}
new Main();
}
}
答案 0 :(得分:2)
你的代码存在很多问题,太多不能解决所有问题,但它包括:
while (true)
... 关于你的主要问题 - 不要调用另一个类的主要方法,因为这样做会丢掉所有的OOP。而是创建一个实例并调用实例非静态方法。因为它阻止了Swing事件线程,你的代码因为while (true)
而冻结了。而不是那样,使用Swing Timer。但最重要的是,抛出这些代码并重新开始。
改善建议:
while (true)
。paintComponent(...)
方法,并确保在其中调用 相同的 超级方法。