• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Gui actionperformed method for many buttons and text

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to use this class and method with all of my buttons and text fields. My program contains 1 push button that adds questions and answers to a test, 2nd button that starts a test, a 3rd button that scores the test, and a text field that enters the user information. How would this work? I know how to create questions and answers, start a test, and score a test it's just a matter of how to implement the button and text fields. I'll post the code that I have done so far. This is my final exam and it looks like a real fun project and learning experience. I really would like your input on this program. I have no shot of failing the class even if I bomb this final, but it's a learning thing to me.

Please see:private class ButtonListener implements ActionListener{
public void actionPerformed (ActionEvent event)
with all of my buttons and text fields.





Driver Program

_________________________________________________________________________________

import javax.swing.JFrame;
public class TestingDriver
{
//-----------------------------------------------------------------
// Creates and displays the main program frame.
//-----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Testing Program");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
TestingPanel panel = new TestingPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
Class Program


____________________________________________________________________________-_______
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestingPanel extends JPanel
{

private JButton addButton, testButton, scoreButton;
private JTextField ts;
private JLabel addQuestionAnswerLabel, questionAnswerLabel, scoreLabel, resultAddQuestionLabel, resultQuestionAnswerLabel, resultScoreLabel;
//-----------------------------------------------------------------
// Constructor: Sets up the GUI.
//-----------------------------------------------------------------
public TestingPanel()
{

addButton = new JButton ("Add Question/Test");//Button to add questions and answers
addButton.addActionListener (new ButtonListener());//Add question and answer button to action listener
testButton = new JButton ("Start Test");//Test button to start the test
testButton.addActionListener (new ButtonListener());//button to action listener
scoreButton = new JButton ("Score Test");//Score test button
scoreButton.addActionListener (new ButtonListener());score button to action listener//Add the score button to the action listener



addQuestionAnswerLabel = new JLabel ("Input for Question and Answer are accepted:");//Label that says display the questions and answers entered
resultAddQuestionLabel = new JLabel ();//Label that the question and answer were entered
questionAnswerLabel = new JLabel ("Your test questions and answers will start now:");//Label that says the test will start
resultQuestionAnswerLabel = new JLabel ();//Questions and answers displayed in the test
scoreLabel = new JLabel ("Your score is:");//Label that says the score
resultScoreLabel = new JLabel ();//Results of the score label



ts = new JTextField(200);//Create 200 field text field
ts.addActionListener (new ButtonListener());//Add text field to the action listener

add (ts);//add to panel

add (addButton);//add to panel
add (testButton);//add to panel
add (scoreButton);//add to panel

add (addQuestionAnswerLabel);//add to panel
add (resultAddQuestionLabel);//add to panel
add (questionAnswerLabel);//add to panel
add (resultQuestionAnswerLabel);//add to panel
add (scoreLabel);//add to panel
add (resultScoreLabel);//add to panel

setBackground (Color.cyan);
setPreferredSize (new Dimension(300, 500));




}

//Button Listener Class implements Action Listener
private class ButtonListener implements ActionListener{

//Method to accept action events
public void actionPerformed (ActionEvent event)
{

}
}
}


1. Create a program named Testing that it will:
• Uses a GUI interface with three buttons.
• The first button will allow an instructor to add questions and answers to a test and store the questions in a file named Test.txt. (If you would like two separate files one for tests and one for answers that will be ok, name the second file Answers.txt).
• The second button will allow a student to take a test. It will display the question and accept input from the student user. It should write answers to a file named Responses.txt.
• The third button will score the test by comparing the answers in the Responses.txt file to the answers in the Test.txt file (or the Answers.txt file if you choose that route)
• The display for the third button should output the question #, the question and answers to the screen. A score should be generated based on whether the question was accurate or inaccurate (in this case you don’t have to check to see if it was close, if the answer was exact then give it points, if not give it zero points.)
• Use approximately 5 questions for test data.

2. User inputs the following data:
 Command button (Create Test, Take Test, Score Test)
 Display accepts user input and stores to a file

3. User receives the following output to the screen:
 Assessment type
 Question #
 Points assigned 2 per question
 Question
 Correct Answer


Sample GUI






Sample Output










 
Saloon Keeper
Posts: 9348
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what you're stuck on. There are tutorials online for implementing listeners.
As a next step in your code I'd call event.getSource() to determine which button was pressed.
 
Saloon Keeper
Posts: 14010
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:As a next step in your code I'd call event.getSource() to determine which button was pressed.


Please never do this! Actions should not depend on which controls initiated them. Create separate ActionListener (or even better: Action) implementations instead.
 
Carey Brown
Saloon Keeper
Posts: 9348
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Carey Brown wrote:As a next step in your code I'd call event.getSource() to determine which button was pressed.


Please never do this! Actions should not depend on which controls initiated them. Create separate ActionListener (or even better: Action) implementations instead.


Excellent observation. I've implemented all of my own GUI code using Actions. I guess I was basing my response on the Patrick's code as is. OK Patrick, go with Stephan's suggestion.
 
Stephan van Hulst
Saloon Keeper
Posts: 14010
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example which may help you understand Actions a bit better, Patrick.
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Patrick, please UseCodeTags (← that's a link) when posting code.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic