Go Back   Java Programming Forums > Java Standard Edition Programming Help > AWT / Java Swing


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 27-04-2010, 12:17 AM
Junior Member
 

Join Date: Feb 2010
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
gradstudent is on a distinguished road
Default Beginner Help (Action Listener)

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);
       
        }
        
    }
}



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 27-04-2010, 11:03 AM
JavaPF's Avatar
mmm.. coffee
 
7 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,585
Thanks: 104
Thanked 93 Times in 86 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Mellow
Default Re: Beginner Help (Action Listener)

Hello.

Please see this thread.

How to Add ActionListener to a JButton. Java Swing
__________________
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.
Reply With Quote
  #3 (permalink)  
Old 30-04-2010, 03:26 PM
Junior Member
 

Join Date: Feb 2010
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
gradstudent is on a distinguished road
Default Re: Beginner Help (Action Listener)

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);
        
             }
 
    }   

}
Judging Class
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;      
     }
}
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



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


100 most searched terms
Search Cloud
2359-unable-move action listener in java actionlistener actionlistener in java addactionlistener addactionlister cannot find symbol method create an abstract class called shape with abstract methods+java double to integer double to integer in java double to integer java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space format double java get files from folder java by threads get mouse position java how to convert list to map in java how to make a calculator in jframe using jcreator http://www.javaprogrammingforums.com/object-oriented-programming/3713-limiting-decimal-places-double.html iphone java java actionlistener java cos java deallocate java double format java double to int java font attributes java format double java forum java forums java get mouse position java heap size exception java jbutton java nextline() java program to find dimensions of a room java programming codes using astirisks java programming forum java programming forums java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space jbutton actionlistener jbutton with key enter jtable questions in java jtext bold jtextarea font jxl.read.biff.biffexception: unable to recognize ole stream mean value decimal double java programmer forum transaction using gui and 2 dimensional array code in java two dimensional arraylist in java typecast in java

All times are GMT. The time now is 09:34 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.