3 Replies - 392 Views - Last Post: 10 June 2014 - 01:59 AM Rate Topic: -----

#1 GoldenFyre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 07-September 13

Having trouble changing JButton text outside of the GUI Class

Posted 06 June 2014 - 09:38 AM

So, Once again I'm attempting to make a Who Wants to Be a Millionaire GUI game. This is the actual game screen code

package WWTBAM;

import java.awt.BorderLayout;

public class GUIGame extends JFrame implements ActionListener {
	
	public static int moves = 0;
	public static boolean finished = false;
	public static boolean correct = true;
	public static String question = "";
	public static char correctAns = ' ';
	public static String ansA = " ";
	public static String ansB = " ";
	public static String ansC = " ";
	public static String ansD = " ";
	public int placeholder = 0;
	private JPanel contentPane;

	//Launch the application.
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GUIGame frame = new GUIGame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	//----------------------------END OF JPANEL RUN-------------------------------	
	
	boolean loss = true;
	//int questionTracker = 0;
	//int corAnswerTracker = 0;
	
	//Do While loop for running the game
	do{


		
	}
	while (!finished);
	
	
	
	}

	/**
	 * Create the frame.
	 */
	public GUIGame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(375, 150, 500, 500);
		contentPane = new JPanel();
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		final JButton btnAnswerA = new JButton("A");
		btnAnswerA.addActionListener(this);
			//public void actionPerformed(ActionEvent e) {
			//	moves++;
			//	Game.checkAnswer('A');
			//}
		//});
		btnAnswerA.setBounds(10, 322, 153, 60);
		contentPane.add(btnAnswerA);
		
		final JButton btnAnswerB = new JButton("B");
		//btnAnswerB.addActionListener(new ActionListener() {
			//public void actionPerformed(ActionEvent e) {
			//	moves++;
			//	Game.checkAnswer('B');
			//}
		//});
		btnAnswerB.setBounds(173, 322, 153, 60);
		contentPane.add(btnAnswerB);
		
		final JButton btnAnswerC = new JButton("C");
		btnAnswerC.addActionListener(this);
			//public void actionPerformed(ActionEvent e) {
			//	moves++;
			//	Game.checkAnswer('C');
			//}
		//});
		btnAnswerC.setBounds(10, 393, 153, 60);
		contentPane.add(btnAnswerC);
		
		final JButton btnAnswerD = new JButton("D");
		//btnAnswerD.addActionListener(new ActionListener() {
			//public void actionPerformed(ActionEvent e) {
			//	moves++;
			//	Game.checkAnswer('D');
			//	getQAP(moves);
			//}
		//});
		btnAnswerD.setBounds(173, 393, 153, 60);
		contentPane.add(btnAnswerD);
		
		JButton btnFiftyFifty = new JButton("Fifty Fifty");
		btnFiftyFifty.setBounds(351, 335, 123, 23);
		contentPane.add(btnFiftyFifty);
		
		final JButton btnAskCrowd = new JButton("Ask the Crowd");
		btnAskCrowd.addActionListener(this);
			//public void actionPerformed(ActionEvent e) {
			//	GUIAskTheCrowd frame = new GUIAskTheCrowd();
			//	frame.setVisible(true);
			//	btnAskCrowd.setEnabled(false);
			//}
		//});
		btnAskCrowd.setBounds(351, 380, 123, 23);
		contentPane.add(btnAskCrowd);
		
		JButton btnNewQuestion = new JButton("New Question");
		btnNewQuestion.setBounds(351, 425, 123, 23);
		contentPane.add(btnNewQuestion);
		
		final JTextArea lblQuestion = new JTextArea();
		lblQuestion.setBackground(Color.CYAN);
		lblQuestion.setLineWrap(true);
		lblQuestion.setWrapStyleWord(true);
		lblQuestion.setText("Question");
		lblQuestion.setFont(new Font("Copperplate Gothic Light", Font.PLAIN, 30));
		lblQuestion.setBounds(10, 10, 316, 236);
		contentPane.add(lblQuestion);
		
		JButton btnStart = new JButton("Start Game");
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getQAP(moves);
				lblQuestion.setText(question);
				btnAnswerA.setText(ansA);
				btnAnswerB.setText(ansB);
				btnAnswerC.setText(ansC);
				btnAnswerD.setText(ansD);
			}
		});
		btnStart.setBounds(336, 11, 89, 23);
		contentPane.add(btnStart);
		
		btnAnswerA.setActionCommand("A");
		btnAnswerB.setActionCommand("B");
		btnAnswerC.setActionCommand("C");
		btnAnswerD.setActionCommand("D");
		btnAskCrowd.setActionCommand("ask");
		btnNewQuestion.setActionCommand("new");
		btnFiftyFifty.setActionCommand("fifty");
	}
	
	//Setting the questions correct answers and possible answers
		public void getQAP(int qNum)
		{
						
			if (moves >= 0 && moves <= 2)
			{
				placeholder = 0 + (int)(Math.random() * ((9 - 0) + 1));
				
				while (placeholder == 0)
						placeholder = 0 + (int)(Math.random() * ((9 - 0) + 1));
				
				while (Preparations.questions[0][placeholder] == "X" ||  Preparations.correctAnswer[0][placeholder] == 'X')
					placeholder = 0 + (int)(Math.random() * ((9 - 0) + 1));
				
				question = Preparations.questions[0][placeholder];
				correctAns = Preparations.correctAnswer[0][placeholder];
				
				Preparations.questions[0][placeholder] = "X";
				Preparations.correctAnswer[0][placeholder] = 'X';
				
				ansA = Preparations.possibleAnswers[placeholder][0];
				ansB = Preparations.possibleAnswers[placeholder][1];
				ansC = Preparations.possibleAnswers[placeholder][2];
				ansD = Preparations.possibleAnswers[placeholder][3];
				
				
				
			}else if(moves >= 3 && moves <= 5)
			{
				placeholder = 0 + (int)(Math.random() * ((9 - 0) + 1));
				
				while (placeholder == 0)
					placeholder = 0 + (int)(Math.random() * ((9 - 0) + 1));
			
				while (Preparations.questions[1][placeholder] == "X" ||  Preparations.correctAnswer[1][placeholder] == 'X')
					placeholder = 0 + (int)(Math.random() * ((9 - 0) + 1));
				
				question = Preparations.questions[1][placeholder];
				correctAns = Preparations.correctAnswer[1][placeholder];
				
				Preparations.questions[1][placeholder] = "X";
				Preparations.correctAnswer[1][placeholder] = 'X';
				
				placeholder += 10;
				
				ansA = Preparations.possibleAnswers[placeholder][0];
				ansB = Preparations.possibleAnswers[placeholder][1];
				ansC = Preparations.possibleAnswers[placeholder][2];
				ansD = Preparations.possibleAnswers[placeholder][3];
			
			}else if(moves >=6 && moves <=8)
			{
				placeholder = 0 + (int)(Math.random() * ((9 - 0) + 1));
				
				while (placeholder == 0)
					placeholder = 0 + (int)(Math.random() * ((9 - 0) + 1));
			
				while (Preparations.questions[2][placeholder] == "X" ||  Preparations.correctAnswer[2][placeholder] == 'X')
					placeholder = 0 + (int)(Math.random() * ((9 - 0) + 1));
				
				question = Preparations.questions[2][placeholder];
				correctAns = Preparations.correctAnswer[2][placeholder];
				
				Preparations.questions[2][placeholder] = "X";
				Preparations.correctAnswer[2][placeholder] = 'X';
				
				placeholder += 20;
				
				ansA = Preparations.possibleAnswers[placeholder][0];
				ansB = Preparations.possibleAnswers[placeholder][1];
				ansC = Preparations.possibleAnswers[placeholder][2];
				ansD = Preparations.possibleAnswers[placeholder][3];
				
			}else if(moves == 9)
			{
				placeholder = 0 + (int)(Math.random() * ((9 - 0) + 1));
				
				while (placeholder == 0)
					placeholder = 0 + (int)(Math.random() * ((9 - 0) + 1));
			
				while (Preparations.questions[3][placeholder] == "X" ||  Preparations.correctAnswer[3][placeholder] == 'X')
					placeholder = 0 + (int)(Math.random() * ((9 - 0) + 1));
					
				question = Preparations.questions[3][placeholder];
				correctAns = Preparations.correctAnswer[3][placeholder];
				
				Preparations.questions[3][placeholder] = "X";
				Preparations.correctAnswer[3][placeholder] = 'X';
				
				placeholder += 30;
				
				ansA = Preparations.possibleAnswers[placeholder][0];
				ansB = Preparations.possibleAnswers[placeholder][1];
				ansC = Preparations.possibleAnswers[placeholder][2];
				ansD = Preparations.possibleAnswers[placeholder][3];
			
			}else
				finished = true;			
			
			//
			System.out.println("Q" + moves + ": " + question + " || CA: " + correctAns);
			System.out.println("|| AnsA: " + ansA);
			System.out.println("|| AnsB: " + ansB);
			System.out.println("|| AnsC: " + ansC);
			System.out.println("|| AnsD: " + ansD);
			
		}//End of getQAP() method
		
		public void setQuestions()
		{
			
			
		}
		
		public void actionPerformed(ActionEvent evt)
		{
			//Check which button has been clicked
			if (evt.getActionCommand().equals("A"))
			{
				GUIFinalAnswer frame = new GUIFinalAnswer();
				frame.setVisible(true);
				moves++;
				//getQAP(moves);			
			}
			else if (evt.getActionCommand().equals("B"))
			{	
				GUIFinalAnswer frame = new GUIFinalAnswer();
				frame.setVisible(true);
				moves++;
				//getQAP(moves);
			}
			else if (evt.getActionCommand().equals("C"))
			{	
				GUIFinalAnswer frame = new GUIFinalAnswer();
				frame.setVisible(true);
				moves++;
				//getQAP(moves);
			}
			else if (evt.getActionCommand().equals("D"))
			{
				GUIFinalAnswer frame = new GUIFinalAnswer();
				frame.setVisible(true);
				moves++;
				//getQAP(moves);
			}
			
			//lblQuestion.setText(question);
			//btnAnswerA.setText(ansA);
			//btnAnswerB.setText(ansB);
			//btnAnswerC.setText(ansC);
			//btnAnswerD.setText(ansD);
		}//End of actionPerformed method
}//End of Class



When ever try and change the buttons text outside of the actual GUIGame constructor i can't. Like in the main method or the action listener.

The code won't work if you copy paste because it's a part of a larger package. If you need any more info please ask, and I'll post

Also if you have any suggestions as to what I can do to make the code better or more efficient please inform me


:P/> ty

Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble changing JButton text outside of the GUI Class

#2 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 160
  • View blog
  • Posts: 625
  • Joined: 24-April 10

Re: Having trouble changing JButton text outside of the GUI Class

Posted 06 June 2014 - 09:57 AM

The buttons are declared inside the constructor, so they are only visible within it. You should make them global variables.
Was This Post Helpful? 1
  • +
  • -

#3 GoldenFyre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 07-September 13

Re: Having trouble changing JButton text outside of the GUI Class

Posted 09 June 2014 - 07:02 PM

Thank you! Can't believe I missed something so simple
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2689
  • View blog
  • Posts: 11,351
  • Joined: 20-September 08

Re: Having trouble changing JButton text outside of the GUI Class

Posted 10 June 2014 - 01:59 AM

You can take as a rule of thumb, until you know better, that static should appear once only in your code - in the main method's declaration
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1