2016-04-15 14 views
-3

Ich bin neu im Programmieren und suche Ratschläge, wie ich meine Fragen, die ich im unten stehenden Code gemacht habe, zu meinem lblScenario (Label habe ich) gemacht habe. Die Technik, die ich benutzte funktioniert nicht sehr geschätzt:}Text zu Etiketten anzeigen

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) 
    { 



    } 
} 

ignorieren die spellinng Fehler und nochmals vielen dank :)

+0

Die Frage selbst ist nicht ganz klar. Worüber zweifeln Sie? –

+0

Als erstes würde ich die ScenarioHandler.actionPerformed-Methode hervorheben. Was Sie versuchen, ist es besser, Array oder Dictionary zu verwenden, um Indizes zu Labels zuzuordnen. –

Antwort

1

In MoralGameGUI(), sind Sie ein neues JLabel Objekt mit dem gleichen Namen wie die Instanzvariable erneut erklärt (die null bleibt, und verursacht Abstürze, wenn Sie Methoden aufrufen, wie setText):

JLabel lblScenario = new JLabel("Scenario"); 

dies nur tun, um den Wert der realen zuzuordnen:

lblScenario = new JLabel("Scenario"); 
1

In Ihrem Konstruktor, was Sie tun:

JLabel lblScenario = new JLabel("Scenario"); 

Als solche sind Sie eine neue zu schaffen JLabel, anstelle von Initialisieren der ursprünglichen. Also, dieses neue JLabel wird Ihrem Rahmen hinzugefügt. Aber in der actionPerformed Methode ist dieses neue JLabel nicht sichtbar. Es hat Zugriff auf das JLabel, das Sie zuvor deklariert haben. Aber diese Methode ist nicht in Ihrem JFrame und daher sind Änderungen daran nicht möglich.

Daher scheitern Sie bei Ihrem Versuch.

Sie müssen tun:

lblScenario = new JLabel("Scenario");