|
||
|
|||
|
Not sure what I'm doing wrong. My JButtons are not working. I've been trying to figure this out for quite a while. Any suggestions would be appreciated. Program is supposed to compute the score and reset the JTextFields. The actionlistener is a little messy, a better quicker way to make it work would be appreciated. Thank you.
Java Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class judgingGUI extends JFrame implements ActionListener
{
private final int FRAME_WIDTH = 775;
private final int FRAME_HEIGHT = 300;
private final int X_ORIGIN = 100;
private final int Y_ORIGIN = 400;
JTextField [] judges;
JLabel [] labels;
JButton reset_scores, comp_score;
JTextField contestant, score, j1,j2,j3,j4,j5,j6,j7,j8,j9;;
JLabel label;
double score_a, scores[];
public judgingGUI()
{
super("Judging");
setLayout(new FlowLayout());
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setLocation(X_ORIGIN,Y_ORIGIN);
label = new JLabel("Name of Contestant", JLabel.CENTER);
contestant = new JTextField(18);
contestant.setEditable(true);
contestant.setBackground(Color.white);
add(label);
add(contestant);
JTextField [] judges = new JTextField[8];
JLabel [] labels = new JLabel[8];
for(int i = 0;i < judges.length;i++)
{
labels[i] = new JLabel("judge" +(i+1), JLabel.LEFT);
judges[i] = new JTextField(3);
labels[i].setVerticalAlignment(JLabel.TOP);
judges[i].setBackground(Color.white);
judges[i].setForeground(Color.black);
judges[i].addActionListener(this);
judges[i].setEditable(true);
add(labels[i]);
add(judges[i]);
}
comp_score = new JButton("Compute Score");
comp_score.setBackground(Color.black);
comp_score.setForeground(Color.white);
comp_score.addActionListener(this);
add(comp_score);
label = new JLabel("Contestant's Score", JLabel.LEFT);
label.setVerticalAlignment(JLabel.TOP);
score = new JTextField(10);
score.setEditable(false);
score.setBackground(Color.white);
add(score);
add(label);
label = new JLabel("Reset Scores for Next Contestant", JLabel.LEFT);
reset_scores = new JButton("Reset");
reset_scores.setBackground(Color.gray);
reset_scores.setForeground(Color.red);
reset_scores.addActionListener(this);
add(reset_scores);
}
public void setScores(double [] scores)
{
this.score_a = score_a;
}
public double [] getScores()
{
return scores;
}
public void actionPerformed(ActionEvent evt)
{
Object source = evt.getSource();
if (source == comp_score)
{
scores = j1;
j1.setEditable(true);
j2.setEditable(true);
j3.setEditable(true);
j4.setEditable(true);
j5.setEditable(true);
j6.setEditable(true);
j7.setEditable(true);
j8.setEditable(true);
}
else if (source == reset_scores)
{
j1.setText("");
j1.setEditable(true);
j2.setText("");
j3.setText("");
j4.setText("");
j5.setText("");
j6.setText("");
j7.setText("");
j8.setText("");
contestant.setText("");
j1.setEditable(true);
j2.setEditable(true);
j3.setEditable(true);
j4.setEditable(true);
j5.setEditable(true);
j6.setEditable(true);
j7.setEditable(true);
j8.setEditable(true);
contestant.setEditable(true);
}
}
}
|
|
||||
|
__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight] Forum Tip: Add to peoples reputation ( ) by clicking the button on their useful posts.
|
|
|||
|
Thanks that thread helped and my JButtons are functional accept for my compute button. I'm trying to call the calculateScores()-method in the my Judging class so it will total all the scores minus the min and max. Both classes are below. Please Help as this has been very frustrating pinpointing my error. Everything compiles it just doesn't run correctly.
GUI class: Java Code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUI extends JFrame implements ActionListener
{
private final int FRAME_WIDTH = 775;
private final int FRAME_HEIGHT = 300;
private final int X_ORIGIN = 100;
private final int Y_ORIGIN = 400;
private JTextField [] judges=new JTextField[8];
private JLabel [] labels;
JButton btnReset, btnCompute;
private String coName;
private JTextField contestant, scr;
private JLabel label;
double score_a, scores[], sc1;
Judging judging=new Judging(8, "contestant");
public GUI()
{
super("Judging");
setLayout(new FlowLayout());
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setLocation(X_ORIGIN,Y_ORIGIN);
label = new JLabel("Name of Contestant", JLabel.CENTER);
contestant = new JTextField(18);
contestant.setEditable(true);
contestant.setBackground(Color.white);
add(label);
add(contestant);
JLabel [] labels = new JLabel[8];
for(int i = 0;i < judges.length;i++)
{
labels[i] = new JLabel("judge" +(i+1), JLabel.LEFT);
judges[i] = new JTextField(3);
labels[i].setVerticalAlignment(JLabel.TOP);
judges[i].setBackground(Color.white);
judges[i].setForeground(Color.black);
judges[i].addActionListener(this);
judges[i].setEditable(true);
add(labels[i]);
add(judges[i]);
}
btnCompute = new JButton("Compute Score");
btnCompute.setBackground(Color.black);
btnCompute.setForeground(Color.white);
btnCompute.addActionListener(this);
add(btnCompute);
label = new JLabel("Contestant's Score", JLabel.LEFT);
label.setVerticalAlignment(JLabel.TOP);
scr = new JTextField(10);
scr.setEditable(false);
scr.setBackground(Color.white);
add(scr);
add(label);
label = new JLabel("Reset Scores for Next Contestant", JLabel.LEFT);
btnReset = new JButton("Reset");
btnReset.setBackground(Color.gray);
btnReset.setForeground(Color.red);
btnReset.addActionListener(this);
add(btnReset);
}
public void setScores(double [] scores)
{
this.scores = scores;
}
public double [] getScores()
{
return scores;
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
double total= judging.calculateScore();
if (source == btnCompute)
{
for (int i = 0; i < judges.length; ++i)
{
judges[i].setEditable(true);
}
}
else if (source == btnReset)
{
for (int i = 0; i < judges.length; ++i)
{
judges[i].setText("");
}
contestant.setText("");
for (int i = 0; i < judges.length; ++i)
{
judges[i].setEditable(true);
}
contestant.setEditable(true);
}
}
}
Java Code
import java.util.*;
public class Judging
{
private double [] scores;
private double total;
private int n;
private String contestant;
public Judging(int n, String contestant)
{
this.n = n;
this.contestant = contestant;
scores = new double[n];
total = 0;
}
public void getData(Scanner console)
{
boolean ok;
if (n > scores.length)
{
System.out.println("The size of the array does not equal the "+
"scores asked for");
System.exit(0);
}
for(int i=0;i<n;i++)
{
do
{
System.out.print("Enter the score for the judge number "+(i+1)+": ");
scores [i]= console.nextDouble();
if (scores[i] < 1 || scores[i] > 10)
System.out.println("Sorry! The score must be between 1 and 10");
}
while (scores[i] < 1 || scores[i] > 10);
}
console.nextLine();
}
public double calculateScore()
{
int largest, smallest;
if (n > scores.length)
{
System.out.println("The size of the array does not equal the "+
"scores asked for");
System.exit(0);
}
if (n < 3)
{
System.out.println("There must be at least 3 scores");
return 0;
}
largest = findMax();
smallest = findMin();
total = 0;
for(int i = 0; i < n; ++i)
total += scores[i];
total -= (scores[smallest] + scores[largest]);
return total;
}
public double getTotal()
{
return total;
}
public double [] getScores()
{
return scores;
}
public String getContestant()
{
return contestant;
}
public void setContestant(String newContestant)
{
contestant = newContestant;
}
public void setScores(double [] scores)
{
this.scores = scores;
}
private int findMin()
{
if (n <= 0 || n > scores.length)
{
System.out.println("You have sent the wrong"+
" value for the number of elements in the array for"+
" findMin. -1 is being returned");
return - 1;
}
if (n < 0 || 0 > scores.length)
{
System.out.println("You have sent the "+
"wrong value for the starting element to search in "+
"the array for findMin. -1 is being returned");
return - 1;
}
int minIndex = 0;
double minValue = scores[0];
for(int i = 0 +1; i < n; ++i)
if (scores[i] < minValue)
{
minValue = scores[i];
minIndex = i;
}
return minIndex;
}
private int findMax()
{
if (n <= 0 || n > scores.length)
{
System.out.println("You have sent the wrong"+
" value for the number of elements in the array for"+
" findMax. -1 is being returned");
return - 1;
}
if (n < 0 || n > scores.length)
{
System.out.println("You have sent the "+
"wrong value for the starting element to search in "+
"the array for findMax. -1 is being returned");
return - 1;
}
int maxIndex = n-1;
double maxValue = scores[n-1];
for(int i = n-1; i > 0; --i)
if (scores[i] > maxValue)
{
maxValue = scores[i];
maxIndex = i;
}
return maxIndex;
}
}
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Action Listener | kray66 | AWT / Java Swing | 2 | 19-04-2010 08:26 PM |
| Problem with Action Listener | JonoScho | AWT / Java Swing | 4 | 19-03-2010 05:03 AM |
| Key Listener not working in jinternalframe | furqankhan | What's Wrong With My Code? | 1 | 12-02-2010 01:48 PM |
| Key listener help | airsim15 | AWT / Java Swing | 2 | 13-12-2009 07:15 PM |
| Need Help Action Listener.... | vhizent23 | AWT / Java Swing | 2 | 09-10-2009 06:46 PM |