2016-04-15 165 views
-3

我是新来编程,并正在寻找关于如何显示我的问题,我已经在我的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) 
    { 



    } 
} 

再次忽略spellinng错误,谢谢:)

+0

这个问题本身不是很清楚。你有什么疑问? –

+0

我要强调的第一件事是ScenarioHandler.actionPerformed方法。使用数组或字典将索引映射到标签可以更好地实现。 –

回答

1

MoralGameGUI(),您要重新声明具有相同的名称作为实例变量(停留nullJLabel对象,当您调用方法时会导致崩溃,如setText):

JLabel lblScenario = new JLabel("Scenario"); 

只要做到这一点,到指定值,与实际:

lblScenario = new JLabel("Scenario"); 
1

在你的构造函数,你这样做:

JLabel lblScenario = new JLabel("Scenario"); 

这样,你是创建一个新的JLabel,而不是初始化原来的一个。所以,这个新的JLabel被添加到你的框架中。但是,在actionPerformed方法中,这个新的JLabel不可见。它可以访问您之前声明的JLabel。但是,该方法不在您的JFrame中,因此对它的任何更改都是不可能的。

因此,你尝试失败。

你必须做到:

lblScenario = new JLabel("Scenario");