对于我正在制作的游戏,我希望玩家可以选择两个(或更多)按钮,当点击按钮时,会出现一个新屏幕。
但是,截至目前,第一个按钮的文本在单击时仍然保留。我在侦听器中尝试了setVisible(false),后来又尝试了setText(""),但两者都只创建了错误。如果有人能给我任何其他建议,我会非常感激。我有以下整个游戏面板供参考,但只有B1Listener应该与问题相关。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.Timer;
public class Panel00 extends JPanel
{
private BufferedImage myImage;
private Graphics myBuffer;
public Timer timer;
public JButton button1;
public JButton button2;
public JLabel label1 = new JLabel("Good Choice!");
public JLabel label2 = new JLabel("You're Fired!!");
public int x = 10; //CountDown from 100
public int delay = 1000; //milliseconds
boolean drawWorld = false;
public Panel00()
{
myImage = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
myBuffer = myImage.getGraphics();
setLayout(null);
JButton button1 = new JButton();
button1.setSize(300, 200);
button1.setLocation(100,150);
button1.setFont(new Font("Serif", Font.BOLD, 18));
button1.setText("<html><center>"+"Until we are able to determine and understand this problem"+"<br>"+" and the dangerous threat it poses, our country cannot be the victims of horrendous attacks"+"<br>"+"by people that believe only in Jihad, and have no sense of reason or respect for human life"+"</center></html>");
button1.addActionListener(new B1Listener());
button1.setBorder(null);
button1.setOpaque(false);
button1.setContentAreaFilled(false);
button1.setBorderPainted(false);
add(button1);
JButton button2 = new JButton();
button2.setSize(300, 200);
button2.setLocation(600,150);
button2.setFont(new Font("Serif", Font.BOLD, 18));
button2.setText("<html><center>"+"If ISIS wants to fight, fine with us. "+"<br>"+"We have wanted that fight for a long time. There is no room in the world for ISIS any more."+"<br>"+"The Muslims or us, one of us will have to go."+"</center></html>");
button2.addActionListener(new B2Listener());
button2.setBorder(null);
button2.setOpaque(false);
button2.setContentAreaFilled(false);
button2.setBorderPainted(false);
add(button2);
ActionListener counter =
new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
repaint();
x--;
if (x == 0)
{
timer.stop();
}
}
};
timer = new Timer(delay, counter);
timer.start();
setFocusable(true);
}
public void paintComponent(Graphics g)
{
ImageIcon Nintendo = new ImageIcon("trumpT.jpg");
g.drawImage(Nintendo.getImage(), 0, 0, 1000, 1000, null);
ImageIcon N = new ImageIcon("trump speech.jpg");
g.setColor(Color.WHITE);
g.fillOval(90,100,320,320);
g.setColor(Color.WHITE);
g.fillOval(590,100,320,320);
g.setColor(Color.WHITE);
g.setFont(new Font("Serif",Font.BOLD, 50));
g.drawString(""+x,500,50);
if (drawWorld)
{
g.drawImage(N.getImage(), 0, 0, 1000, 1000, null);
}
}
private class B1Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
repaint();
drawWorld = true;
label1.setLocation(500,700);
label1.setSize(400, 400);
label1.setForeground(new Color(212, 175, 55));
label1.setFont(new Font("Serif", Font.BOLD, 40));
add(label1);
button1.setText("");
timer.stop();
}
}
private class B2Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
label2.setLocation(500,700);
label2.setSize(400, 400);
label2.setForeground(Color.RED);
label2.setFont(new Font("Serif", Font.BOLD, 40));
add(label2);
timer.stop();
}
}
}
答案 0 :(得分:0)
最好使用匿名内部类,比如
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
button1.setText("");
}
});
答案 1 :(得分:0)
您的Panel00构造函数创建一个隐藏实例变量button1的本地button1,该按钮保持为null。你用button2做同样的事情。
在构造函数中,更改
JButton button1 = new JButton();
...
JButton button2 = new JButton();
到
button1 = new JButton();
...
button2 = new JButton();