我是编程的新手,正在寻找有关如何在下面的代码中向我的lblScenario(我制作的标签)显示我的问题的建议。我使用的技术不起作用任何帮助将不胜感激:}
import java.awt.BorderLayout;
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 MoralGameGUI
{
private JLabel lblScenario;
private JFrame frame;
private JPanel panel;
int score;
public MoralGameGUI()
{
frame=new JFrame();
frame.setTitle("Moral Game");
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnYes = new JButton("Yes");
frame.getContentPane().add(btnYes, BorderLayout.WEST);
JButton btnNo = new JButton("No");
frame.getContentPane().add(btnNo, BorderLayout.EAST);
JButton btnGetScenario = new JButton("Get Scenario ");
btnGetScenario.addActionListener(new ScenarioHandler());
frame.getContentPane().add(btnGetScenario, BorderLayout.NORTH);
JLabel lblScenario = new JLabel("Scenario");
frame.getContentPane().add(lblScenario, BorderLayout.CENTER);
JLabel lblAnswer = new JLabel("answer");
frame.getContentPane().add(lblAnswer, BorderLayout.SOUTH);
panel= new JPanel();
panel.setLayout(null);
frame.setVisible(true);
}
class ScenarioHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int index = (int)(Math.random()*20);
if (index == 2){
lblScenario.setText("nd save her?");
}
else if (index ==3){
lblScenario.setText("A magic evil gene asks you if you would give up all your fingers and toes to help raise funds for a ugandan princess");
}
else if (index ==4){
lblScenario.setText("A woman with a rubbish dress on ask you for yours do you give it to her ");
}
else if (index ==5){
lblScenario.setText("A man with no shoes asks for your hand in marriage what do you say");
}
else if (index ==6){
lblScenario.setText("your stuck in a derilict prison and you find a loaf of bread in the darkness do you eat it ");
}
else if (index ==7){
lblScenario.setText("The jam has a thick layer of mould over it do you eat it?");
}
else if (index ==8){
lblScenario.setText("15 giant racoons try to stral your humous what do you shout out them");
}
else if (index ==9){
lblScenario.setText("your wedding dress has been lost do you buy some sweet reeboks instead");
}
else if (index ==10){
lblScenario.setText("ten lizards attack do you fight back");
}
else if (index ==11){
lblScenario.setText("there is no bread left in the kitchen do you cry?");
}
else if (index ==12){
lblScenario.setText("do you sell your soul for a delicious pack of oreos");
}
else if (index ==13){
lblScenario.setText("Have you got a mans voice");
}
else if (index ==14){
lblScenario.setText("have you got the heart of a lion");
}
else if (index ==15){
lblScenario.setText("no eyebrowns dosent hold you back do you agree?");
}
else if (index ==16){
lblScenario.setText("spiders come do you stand your ground");
}
else if (index ==17){
lblScenario.setText(" or no?");
}
else if (index ==18){
lblScenario.setText("are you old?");
}
else if (index ==19){
lblScenario.setText("......?");
}
else if (index ==20){
lblScenario.setText(".........?");
}
}
}
public static void main(String[] args)
{
new MoralGameGUI();
}
}
class YesNoHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
}
}
忽略拼写错误并再次感谢:)
答案 0 :(得分:1)
在MoralGameGUI()
中,您将重新声明一个与实例变量同名的新JLabel
对象(保留null
,并在您调用方法时导致崩溃,像setText
):
JLabel lblScenario = new JLabel("Scenario");
只需这样做,将值分配给真实的:
lblScenario = new JLabel("Scenario");
答案 1 :(得分:1)
在您的构造函数中,您执行:
JLabel lblScenario = new JLabel("Scenario");
因此,您创建新的JLabel ,而不是初始化原始。因此,这个新的JLabel会添加到您的框架中。但是,在actionPerformed
方法中,这个新的JLabel不可见。它可以访问您之前声明的JLabel。但是,该方法不在您的JFrame中,因此无法对其进行任何更改。
因此你的尝试失败了。
你必须这样做:
lblScenario = new JLabel("Scenario");