私はプログラミングに慣れていません。私の質問を表示する方法に関するアドバイスを探しています。私の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)
{
}
}
null
を滞在する(インスタンス変数と同じ名前を持つ新しいJLabel
オブジェクトの再宣言している、:)再びMoralGameGUI()
で
質問自体は非常に明確ではありません。あなたの疑問は何ですか? –
最初に強調するのは、ScenarioHandler.actionPerformedメソッドです。あなたがそこにしようとしているのは、配列に辞書を使ってインデックスをラベルにマッピングする方がよいでしょう。 –