So I want to make a program that constantly keeps track of time while other functions of the code can run. For my code specifically, I have 4 JButtons and 4 JLabels. Once I click a JButton, I want the corresponding JLabel to update its text every couple of seconds (let's say 5 seconds). But while the code running, I want to be able to select a different button at any given time to update its corresponding JLabel and stop the one before. I've tried a few things, but every time I run into two problems:
The JLabel doesn't update every 5 seconds.
I can't click a different JButton while the another JButton is clicked.
They both have to do with the fact that my code goes through an infinite loop once a JButton is clicked, but I'm not sure how I can go around doing this.
I'm new to coding so my knowledge of the terminology is quite limited. Any help will be appreciated.
Here's the code for what happens when the button is clicked. (I changed the code a few times here and there to try to fix on my own, but couldn't so currently it just gives me a stackoverflow error.) :
package maingame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JLabel;
public class StatButtonListener implements ActionListener
{
private JButton _button;
private ArrayList<JLabel> _jlList;
private static String _clicked;
public StatButtonListener(JButton button, ArrayList<JLabel> jlList)
{
_button = button;
_jlList = jlList;
}
#Override
public void actionPerformed(ActionEvent arg0)
{
_clicked = _button.getText();
Long value = null;
JLabel valueLabel = null;
// to stop an infinite loop
for(int i=0; i < 8; i++)
{
if(_button.getText().equals(_jlList.get(i).getText()))
{
valueLabel = _jlList.get(i+1);
value = Long.parseLong(valueLabel.getText());
}
}
double startTime = System.currentTimeMillis()/1000;
updateStat(value, valueLabel, startTime);
}
public void updateStat(Long value, JLabel valueLabel, double startTime)
{
if(!(value == null || valueLabel == null))
{
if(_clicked.equals(_button.getText()))
{
double endTime=System.currentTimeMillis()/1000;
if((endTime-startTime) >= 1)
{
startTime = System.currentTimeMillis()/1000;
value = value + 3;
valueLabel.setText(value.toString());
}
updateStat(value, valueLabel, startTime);
}
}
}
}
You can do this using the aforementioned Swing-Timers:
http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html
This invokes actionPerformed() once its done. You can then update whatever you want to update and restart() it.
Or you can do this using a new Thread:
http://de.wikibooks.org/wiki/Java_Standard:_Threads
This runs completely on its own and does not block the main program - and might need a handle of the objects you want to change.
Related
I am creating a Maze game and want a timer to be displayed on the gameStage.
I have tried using java.util but it requires me to get rid of my swing timer.How could i add a refreshing timer to game
This code is used to make the game frame which contains the button pane and the gameStage.
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
/**
* This class Holds the game pane that has the moving player. It also contains
* the GamePane
*
* #author 602052004
*
*/
public class GamePane extends JPanel implements ActionListener, KeyListener {// *change
// GamePane
// to
// GamePane
// This is were the game screen is made and the player is created.
static final long serialVersionUID = 1L;
JLabel player = new JLabel();
JLabel finish = new JLabel();
JFrame gameFrame;
int playerSpeed = 4;
int FPS = 40;
// This array holds my JLabels for the walls.I used it so that i can have a
// for loop with an index for the labels.
JLabel[] walls = new JLabel[3];
{
walls[0] = new JLabel();
walls[1] = new JLabel();
walls[2] = new JLabel();
}
private final Set<Integer> keys = new HashSet<>();
// The keys set holds the keys being pressed
public static void main(String[] args) {
// Open the GUI window
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
// Create a new object and
// run its go() method
new GamePane().go();
}
});
}
GamePane() {
// Run the parent class constructor
super();
// Allow the panel to get focus
setFocusable(true);
// Don't let keys change the focus
}
/**
* This method creates the gameFrame and sets its layout to a cardlayout.It
* then proceeds the set up the GameFrame.The gameFrame contains the button
* pane and the gameStage
*
* The walls are an array and are used to create an index which is then used
* for the collisions.I set up the walls location here
*/
protected void go() {
setLayout(new CardLayout());
// Setup the window
gameFrame = new JFrame();
// Add this panel to the window
gameFrame.setLayout(new CardLayout());
gameFrame.add(this, "main");
gameFrame.setContentPane(this);
// Set's the window properties
gameFrame.setTitle("main");
gameFrame.setSize(800, 600);
gameFrame.setResizable(false);
gameFrame.setLocationRelativeTo(null);
gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameFrame.setVisible(true);
gameFrame.add(new ButtonPane(gameFrame), "buttons");
// Creates the new JPanel that will hold the game.
JPanel gamestage = new JPanel();
gamestage.setBackground(Color.darkGray);
gameFrame.add(gamestage, "game");
gamestage.setLayout(null);
// *Move the setup of the player and the timer under the walls
// Get a sample of collisions going so that i can do it over the weekend
// Setup the movable box
player.setBounds(25, 25, 20, 20);
player.setVisible(true);
player.setBackground(Color.red);
// Opaque makes the background visible
player.setOpaque(true);
// Setup the key listener
addKeyListener(this);
// Null layout allows moving objects!!!
gamestage.add(player);
// Set the timer
Timer tm = new Timer(1000 / FPS, this);
tm.start();
walls[0].setBounds(10, 15, 10, 480);// left height
walls[0].setVisible(true);
walls[0].setBackground(Color.white);
walls[0].setOpaque(true);
gamestage.add(walls[0]);
walls[1].setBounds(10, 10, 490, 10);// top width
walls[1].setVisible(true);
walls[1].setBackground(Color.white);
walls[1].setOpaque(true);
gamestage.add(walls[1]);
// wall3.setBounds(x, y, width, height);
walls[2].setBounds(10, 100, 100, 10);
walls[2].setVisible(true);
walls[2].setBackground(Color.white);
walls[2].setOpaque(true);
gamestage.add(walls[2]);
finish.setBounds(30, 455, 20, 20); // *make the game change to the main
// screen when finished
// Add a timer
finish.setVisible(true);
finish.setBackground(Color.LIGHT_GRAY);
finish.setOpaque(true);
gamestage.add(finish);
}
/**
* Check if two JLabel objects are touching
*
* #param a
* The first JLabel
* #param b
* The second JLabel
* #return true if the JLabels are touching
*/
public boolean areColliding(JLabel a, JLabel b) {
return a.getBounds().intersects(b.getBounds());
}
/**
* this method makes the player move. It takes the players speed and
* subtracts or adds the player speed to the current position of the player.
* It also figures out were the player is at currently aswell.
*
* #param arg0
*/
#Override
public void actionPerformed(ActionEvent arg0) {
// Move up if W is pressed
if (keys.contains(KeyEvent.VK_W)) {
player.setLocation(player.getX(), player.getY() - playerSpeed);
}
// Move right if D is pressed
if (keys.contains(KeyEvent.VK_D)) {
player.setLocation(player.getX() + playerSpeed, player.getY());
}
// Move down if S is pressed
if (keys.contains(KeyEvent.VK_S)) {
player.setLocation(player.getX(), player.getY() + playerSpeed);
}
// Move left if A is pressed
if (keys.contains(KeyEvent.VK_A)) {
player.setLocation(player.getX() - playerSpeed, player.getY());
}
for (int i = 0; i < walls.length; i++) {
// I created a for loop instead
// of a do loop because the for
// loop would have been a lot
// simpler to manage
if (areColliding(walls[i], player)) { // Reposition the target
int newX = (int) (25);
int newY = (int) (25);
player.setLocation(newX, newY);
}
}
if (areColliding(finish, player)) {
// Reposition the target
int newX = 25;
int newY = 25;
player.setLocation(newX, newY);
CardLayout layout = (CardLayout) gameFrame.getContentPane()
.getLayout();
layout.show(gameFrame.getContentPane(), "buttons");
}
}
#Override
public void keyPressed(KeyEvent e) {
// Add the key to the list
// of pressed keys
if (!keys.contains(e.getKeyCode())) {
keys.add(e.getKeyCode());
}
}
#Override
public void keyReleased(KeyEvent e) {
// Remove the key from the
// list of pressed keys
keys.remove((Integer) e.getKeyCode());
}
#Override
public void keyTyped(KeyEvent e) {
}
}
this code shows the game pane when the buttob is pressed
/**
* This pane contains the button and sets up the button pane
*/
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ButtonPane extends JPanel {
private JButton startBTN;// Calls the JButton
JFrame game;
public ButtonPane(JFrame g) {
game = g;
setLayout(new GridBagLayout());
setBackground(Color.gray);// Sets the menu stages color blue
startBTN = new JButton("Game");// Creates a new button
add(startBTN);// Adds the button on the startStage
startBTN.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (game.getContentPane().getLayout() instanceof CardLayout) {
CardLayout layout = (CardLayout) getParent().getLayout();
layout.show(game.getContentPane(), "game");
}
}
});
}
}
Okay, so you have a Timer already, which is ticking away at 1000/FPS times a second, cool. All you really need is away to calculate the difference between two points in time, which, amazingly, is very simple.
Start by defining a "start time"
private Instant startTime;
This will be null till you need it. When you want to start the timer, use startTime = Instant.now();
When startTime != null, you want to calculate the difference between it and now...
Duration runningTime = Duration.between(startTime, Instant.now());
This now tells you how long the timer has been running for.
Next, we need to make some decisions, like what to do when the timer runs out, but for that, we actually need to know how lone the timer should run for...
private Duration timeOutDuration = Duration.ofSeconds(5);
This just sets up a timeout of 5 seconds, you can use what ever range you want.
This then allows us to calculate the remaining time of the timer...
Duration timeRemainig = timeOutDuration.minus(runningTime);
and then to make decisions about what to do...
if (timeRemainig.isNegative() || timeRemainig.isZero()) {
// Time has run out...
// startTime = null; // stop the timer
} else {
// Update the UI
}
The date/time API introduced in Java 8 is incredibly powerful and flexible (and a lot of fun, when you get your head around it)
A solution might start looking something like...
private Duration timeOutDuration = Duration.ofSeconds(5);
private Instant startTime; // Set this when you're ready to start the timer
#Override
public void actionPerformed(ActionEvent arg0) {
if (startTime != null) {
Duration runningTime = Duration.between(startTime, Instant.now());
Duration timeRemainig = timeOutDuration.minus(runningTime);
if (timeRemainig.isNegative() || timeRemainig.isZero()) {
// Time has run out...
// startTime = null; // stop the timer
} else {
// Update the UI
}
}
Formatting a Duration for output generally looks something like...
long hours = timeRemainig.toHours();
long mins = timeRemainig.minusHours(hours).toMinutes();
// Or if you're lucky enough to be using Java 9+
//String formatted = String.format("%dhrs %02dmins", duration.toHours(), duration.toMinutesPart());
String formatted = String.format("%dhrs %02dmins", hours, mins);
or simular, depending on how you want it formatted
Why use this approach instead of some "counter"
Simple, it's (super) accurate. Timer only guarantees a "at least" interval, that is, it will delay no less then the value apply, this means that it's possible to introduce a "drag" over time, where a counter would fall out of sync. Sure, over a short period of time, it's probably not a big deal, but since there is a (super easy) better way to do it, why not make use of it.
The solution is also super flexible, applied to a broad spectrum of similar issues. I use the above concept as part of time based animations, which generally produce a far superior, overall, result.
I am making an choose-your-own-adventure project with multiple outcomes. One of the things I'm trying to do is incorporate quick time events (QTEs), or those events in games where you have to, say, click a button within a short time limit or you get killed. In this case, I'm trying to make it so that when the time comes, the game says "Suddenly you hear the clopping of hooves coming from your right!", at which point a method is called up that make a GUI pop up with a button, and you only have a few seconds to click the button. If you click the button within the proper amount of time, it should continue with the game (starts with 'CRACK!!'). If not, the game should be over, and a message is printed ('GAME OVER - You were slain by Minotaur Prison Guard. However, when I run it, the GUI doesn't pop up, the Game Over message prints three times with the sleepLines delay, and then the 'CRACK!! You quickly turn...' message appears. Here is my code:
import java.util.Scanner;
import java.awt.Container;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class KCP1Main extends JPanel{
public static JButton AttackButton;
static Scanner myScanner = new Scanner(System.in);
static int quicktimecompletion = 0;
public static void main(String[] args) throws InterruptedException {
//insert leading up to this point here
System.out.println("Suddenly, you hear the clomping of hooves to your right!");
QuickTimeEvent(args);
sleepLines(500,2);
System.out.println("CRACK!!");
sleepLines(1000,1);
scrollText("You quickly turn right, swinging your metal pole at the same time... it swings into the head of ");
scrollText("a minotaur that was charging towards you, with enough force to knock him out.");
}
}
public static void QuickTimeEvent(String[] args) throws InterruptedException
{
for(int timer=0 ; timer<3 ; timer++){
ButtonHandler handler = new ButtonHandler();
AttackButton = new JButton("<!");
//static add(AttackButton);
AttackButton.addActionListener(handler);
sleepLines(1000,1);
if(quicktimecompletion == 1)
{
break;
}
if(timer > 3);
{
System.out.println("GAME OVER");
System.out.printf("You were slain by a Minotaur Prison Guard.");
}
}
}
public static class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource() == AttackButton)
{
quicktimecompletion = 1;
}
}
}
public static void scrollText(String message) throws InterruptedException{
for(int i = 0; i < message.length(); i++)
{
System.out.print(message.charAt(i));
Thread.sleep(62);
}
System.out.print("\n");
}
public static void JPanel(String[] args){
JFrame theGUI = new JFrame();
theGUI.setTitle("Incoming!");
KCP1Main makeButtons = new KCP1Main();
theGUI.add(makeButtons);
theGUI.setSize(300, 200);
theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theGUI.setBackground(Color.red);
Container pane = theGUI.getContentPane();
theGUI.setVisible(true);
}
public static void sleepLines(int duration, int lines) throws InterruptedException{
for(int i=0; i < lines; i++){
Thread.sleep(duration);
System.out.println("");
}
}
}
Let's start with the fact that Swing is single threaded, this means that any action which blocks Event Dispatching Thread (like Thread.sleep) will prevent it from processing any new events (including paint events) until it unblocks.
Two golden rules...
Don't block the EDT
Don't update the UI from outside the EDT
In your case, you could achieve your desired result by making use of a Swing Timer
See How to use Swing Timers for more details.
In the back of my head, I'd consider probably making some kind of QTE class, which took the amount of time, a reference to the button the user needs to click and possible some kind of listener/observer/callback.
The QTE would then attached an ActionListener to the button, that if triggered, would stop the Timer and notify the observer of success. Otherwise if the Timer triggered first, it'd notify the observer of failure.
As an idea
I'm trying to write a program that plays musical chords. I'd like to add a window that shows a progress bar displaying the time that the chords play for and how much they have completed. To play the chords, I've been using a slightly modified version of the StdAudio class. So far, I have the following code to be run when I ask a chord to play.
public static void playNotes(double[] frequencies, double duration, double amplitude)
{
PlayAudioGUI g = new PlayAudioGUI(duration);
g.run();
amp = amplitude;
ArrayList<double[]> chord = new ArrayList<double[]>();
for(double freq : frequencies) {
double[] note = StdAudio.tone(freq, duration);
chord.add(note);
}
double[] chordCombined = new double[chord.get(0).length];
for (int i = 0; i < chordCombined.length; i++) {
for (double[] note : chord) {
chordCombined[i] += note[i];
}
chordCombined[i] /= chord.size();
}
StdAudio.play(chordCombined);
}
I've never attempted multithreading before, so I don't know what I'm doing wrong. When I run the code, It shows an empty window while it plays the chord, then afterwards displays the window properly. I'd like for it to display the window at the same time as playing the audio.
Here is my code for the window's class.
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
public class PlayAudioGUI implements Runnable {
private JFrame window;
private JProgressBar prog;
private double duration;
private Timer t;
class TimerListener implements ActionListener {
// This runs every few milliseconds, depending on the delay set below
public void actionPerformed(ActionEvent event) {
prog.setValue(prog.getValue() + 1);
// Stop the timer and hide the window when the progress bar
// completes
if (prog.getValue() == prog.getMaximum()) {
t.stop();
window.setVisible(false);
}
}
}
public PlayAudioGUI(double duration) {
this.window = new JFrame("Playing audio...");
this.duration = duration;
}
#Override
public void run() {
// Setting up gridbag layout. I will add more components later.
Container pane = this.window.getContentPane();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(30, 30, 30, 30);
// Display the approximate duration
String clippedDuration;
if (Double.toString(duration).length() > 5) {
clippedDuration = Double.toString(duration).substring(0, 4);
} else {
clippedDuration = Double.toString(duration);
}
String message = "Playing audio for " + clippedDuration + " seconds";
pane.add(new JLabel(message), c);
// Make a progressbar
c.gridy = 1;
this.prog = new JProgressBar();
this.prog.setMinimum(0);
this.prog.setMaximum(250);
pane.add(this.prog, c);
// More window management stuff
this.window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.window.pack();
this.window.setVisible(true);
// Set up the timer
ActionListener listener = new TimerListener();
final int DELAY = (int) (4 * this.duration); // This works, I did the
// math :)
t = new Timer(DELAY, listener);
t.start();
}
}
Thanks for your help.
Suggestions:
The new dependent dialog window should be just that, a dialog such as a JDialog, not a new JFrame which is creating a whole separate application.
You know that you should be doing your sound creation in a background thread, and your blank screen is being caused by just this problem, and yet I see no thread creation in your code -- why?
Myself, I'd not use a Swing Timer, and poll data, but rather do all within a SwingWorker, within the SwingWorker's doInBackground method I'd update its progress state, and I'd add a PropertyChangeListener to the SwingWorker and monitor this state.
As an aside, you will almost never want to create a Runnable class and then call its run() method. If you're creating the Runnable to allow it to run in a background thread, then you'd likely place it into a Thread and then call start() on the Thread. Since your code above should run on the Swing event thread, then if it is not being called from this thread, it should be queued on to it via SwingUtilities.invokeLater(myRunnable);
I'm not sure how you can get a progress value from your StdAudio library. If there's a way, then use it to set the SwingWorker's progress state via its setProgress(...) method. If not, then you could guess, I suppose or you may be better off using an indeterminate progress bar. I believe JProgressBar has a method called setIndeterminate(true) that would work for this.
I'm pretty sure I can figure out how to do add the picture (although if you know you can save me some research time on that), but how would I go on adding an event if variable bob is over 5?
package clicker;
import java.applet.*;
import java.awt.*;
public class click extends Applet
{
/**
*
*/
private static final long serialVersionUID = 1L;
Button increase, decrease; // These are two buttons
int value = 0;
public void init()
{
increase = new Button("Increase bob ");
add(increase);
decrease = new Button("Decrease bob ");
add(decrease);
}
public boolean action(Event e, Object args)
{
if(e.target == increase)
value++;
if(e.target == decrease)
value--;
repaint();
return true;
}
public void paint(Graphics g)
{
g.drawString("How many bob do you give? " + value, 50, 80);
}
}
Would I just add it into the public boolean action like the e.target == increase? I tried it and I had an error, I figured it would be in there. Where would I write it? Thanks in advance.
Under your action() method, after you change the value variable, you would add another control statement.
if (value > 5) {
new ImageIcon(new URL("image/path")).paintIcon(this, getGraphics(), x, y);
}
I posted a question a few minutes ago and it was requested that I post an "SSCCE" to demonstrate my issue. I have heavily condensed my program so if a few constructs make no sense or seem even more ineffcient than a novice's work ought to be that's simply a product of that operation.
Now, my program essentially serves to copy an array of Russian verbs conjugations to a centralized array, oneRay. In the setup here, a prompt is shown above a blank line, and in that blank line one is to type the appropriate conjugation. Pressing the "Submit" button is supposed to check the answers against those of the array, but by my own fault or lack of understanding I see the message "FAIL" even when directly copying what I know to be the correct answer. With this line I get the input:
build1 = new StringBuilder(ssfield1.getText());
and with this I check it against the element:
if(build1.equals(oneRay[pick][1].toLowerCase().replaceAll("\\s","")))ssfield1.setText("CORRECT");
else{ssfield1.setText("FAIL");}
I feel that this may be a simple issue of checking the wrong element against the wrong input textfield, however it mayn't be, so here is all the code of my condensed "SSCCE":
Not to long I hope, and certainly self-contained (and quite simple too):
Let me know if you need anything else, or if it's too late for these questions!
package wbh;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class SwingImplementation_RUS extends JFrame{
String [] [] oneRay;Random random = new Random();
JPanel panel; JTextField field,sfield1,sfield2,ssfield1,ssfield2; JButton buton;
int pick, hold;StringBuilder build1,build2;
public SwingImplementation_RUS(final int subj){
super("To be updated");
final String RUS_1[][]={
{"знать","знаю","знаешь","знает","знает","знает","знаем","знаете","знают",}};
new Thread(new Runnable() {
public void run() {
buton = new JButton("Submit");
setLayout(new GridLayout(3,1,3,3));
panel = new JPanel();panel.setLayout(new GridLayout(5,2,3,3));
field = new JTextField();
sfield1 = new JTextField("Я");sfield2 = new JTextField("Ты");
ssfield1 = new JTextField("");ssfield2 = new JTextField("");
buton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent m) {
System.out.println("CHECKED");
build1 = new StringBuilder(ssfield1.getText());
build2 = new StringBuilder(ssfield2.getText());
called2();}});
Font f = new Font("Arial", Font.BOLD, 75);
field.setFont(f);
add(field);
add(panel);
panel.add(sfield1);panel.add(ssfield1);
panel.add(sfield2);panel.add(ssfield2);
add(buton);
setLocation(500,0);setSize(865, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);
}}).start();
oneRay = new String[RUS_1.length][9];
for(int i = 0; (RUS_1.length) > i; i++){
oneRay[i][0] = RUS_1[i][0];oneRay[i][1] = RUS_1[i][1];
oneRay[i][2] = RUS_1[i][2];}
try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
hold = oneRay.length;pick = random.nextInt(hold);
field.setText(" "+oneRay[pick][0]);
}
private void called2(){
if(build1.equals(oneRay[pick][1].toLowerCase().replaceAll("\\s","")))ssfield1.setText("CORRECT");
else{ssfield1.setText("FAIL");}
if(build2.equals(oneRay[pick][2].toLowerCase().replaceAll("\\s","")))ssfield2.setText("CORRECT");
else{ssfield2.setText("FAIL");}
}
public static void main (String [] args){
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new SwingImplementation_RUS(1);
}
});
}
}
I think the problem is that you are using StringBuilder#equals and expected it to work like String#equals.
The implementation of equals for StringBuilder looks like...
public boolean equals(Object obj) {
return (this == obj);
}
Which is just comparing the object references, not there contents.
Instead, try using something like...
if (build1.toString().equals(oneRay[pick][1].toLowerCase().replaceAll("\\s", ""))) ...
As a recommendation, you should also use {} around your if-else conditions, for example
if (build1.toString().equals(oneRay[pick][1].toLowerCase().replaceAll("\\s", ""))) {
ssfield1.setText("CORRECT");
} else {
ssfield1.setText("FAIL");
}
if (build2.toString().equals(oneRay[pick][2].toLowerCase().replaceAll("\\s", ""))) {
ssfield2.setText("CORRECT");
} else {
ssfield2.setText("FAIL");
}
Which will make it easier to read (generally) and ensure that you are not accidently executing functionality you didn't expect for a condition...