【发布时间】:2015-10-15 13:36:37
【问题描述】:
所以我正在制作一个游戏,在有人获胜后,我想要一个运行显示“x 已获胜”或“y 已获胜”的方法。现在我的问题是,如果我制作方法来绘制包含胜利消息的字符串,那么我的 IDE 不允许我这样做,因为我显然无法运行以 Graphics g 作为参数的方法。
还有其他方法可以在 JFrame 屏幕上显示消息吗?
这是我的代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;`
//nothing wrong here :P
public class Demonstration extends JFrame {
private JButton b1;
private JButton b2;
public Demonstration () {
//My constructor
JPanel panel = new JPanel();
panel.setLayout(null);
add(panel);
handler h = new handler();
b1 = new JButton("yes");
b2 = new JButton("no");
//A sub - class to handle events. Created below
b1.setBounds(34, 34, 34, 34);
//random
panel.add(b1);
b1.addActionListener(h);
b2.setBounds(56, 56, 56, 56);
// random again
panel.add(b2);
b2.addActionListener(h);
setTitle("Demonstration");
setBackground(Color.BLACK);
setDefaultCloseOperation(3);
setSize(720, 720);
setVisible(true);
}
}
那是我的小组。
public static void main (String[] args) {
new Demonstration();
}
假设我稍后创建了处理程序类,我希望它运行一段代码,该代码有一个 if / else 按钮被单击 (.getSource()),我想使用 Graphics 重新设计界面并打印出胜利信息,我该怎么做?
【问题讨论】:
标签: java swing methods graphics jpanel