My image is not added to the GUI as the background [duplicate] - java

This question already has answers here:
How do I draw an image to a JPanel or JFrame?
(2 answers)
Trying to draw an image on a JFrame
(1 answer)
Closed 29 days ago.
`import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class RPS implements ActionListener {
JFrame frame;
JPanel panel;
JButton buttonR;
JButton buttonP;
JButton buttonS;
JLabel title;
JLabel comChoice;
JLabel result;
ImageIcon img;
JLabel image;
JTextField name;
int userPoints = 0;
int compPoints = 0;
String userInput;
String rand1;
JLabel ps;
JLabel cs;
public RPS() {
frame = new JFrame();
panel = new JPanel(new GridBagLayout());
buttonR = new JButton("ROCK");
buttonP = new JButton("PAPER");
buttonS = new JButton("SCISSORS");
title = new JLabel("ROCK vs PAPER vs SCISSORS");
ps = new JLabel("Player Score : 0");
cs = new JLabel("Computer Score : 0");
comChoice = new JLabel("Computer choice : " );
result = new JLabel("");
name = new JTextField();
name.setPreferredSize(new Dimension(250,40));
image = new JLabel("",new ImageIcon(background.jpg"),JLabel.CENTER);
frame.add(image);
GridBagConstraints c = new GridBagConstraints(); //ADDING THE COMPONENTS
c.insets = new Insets(30, 30, 30, 30);
c.gridx = 3;
c.gridy = 0;
title.setFont(new Font("Rockwell Condensed", Font.BOLD, 40));
panel.add(title, c);
c.gridx = 3;
c.gridy = 1;
panel.add(result, c);
c.gridx = 3;
c.gridy = 1;
panel.add(name,c);
c.insets = new Insets(10, 10, 10, 10);
c.gridx = 4;
c.gridy = 2;
panel.add(buttonR, c);
c.gridx = 3;
c.gridy = 2;
panel.add(buttonP, c);
c.gridx = 2;
c.gridy = 2;
panel.add(buttonS, c);
c.insets = new Insets(15, 15, 15, 15);
c.gridx = 2;
c.gridy = 3;
panel.add(ps, c);
c.gridx = 4;
c.gridy = 3;
panel.add(cs, c);
c.insets = new Insets(15, 15, 15, 15);
c.gridx = 3;
c.gridy = 3;
panel.add(comChoice, c);
buttonR.addActionListener(this);
buttonP.addActionListener(this);
buttonS.addActionListener(this);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(540,399);
frame.setTitle("Rock Paper Scissors");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new RPS();
}
private String computerChoice() {
Random rand = new Random();
String compGuess[] = {"ROCK", "PAPER", "SCISSORS"};
String rand1 = compGuess[rand.nextInt(3)];
comChoice.setText("Computer choice : " + rand1);
return rand1;
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == buttonR) {
userInput = "ROCK";
computerChoice();
rand1 = computerChoice();
compare();
}
if (e.getSource() == buttonP) {
userInput = "PAPER";
computerChoice();
rand1 = computerChoice();
compare();
}
if (e.getSource() == buttonS) {
userInput = "SCISSORS";
rand1 = computerChoice();
compare();
}
}
private void compare() {
if (userInput.contains("ROCK"))
{
if (rand1 == "PAPER")
{
compPoints++;
cs.setText("Computer Score : "+Integer.toString(compPoints));
ps.setText(name.getText()+"'s Score : "+Integer.toString(userPoints));
result.setText("YOU LOSE !");
} else if (rand1 == "SCISSORS")
{
userPoints++;
ps.setText(name.getText()+"'s Score : "+Integer.toString(userPoints));
result.setText("YOU WIN !");
}
else
{
result.setText("IT'S A DRAW !");
}
} else if (userInput.contains("PAPER"))
{
if (rand1 == "ROCK")
{
userPoints++;
ps.setText(name.getText()+"'s Score : "+Integer.toString(userPoints));
result.setText("YOU WIN !");
} else if (rand1 == "SCISSORS")
{
compPoints++;
cs.setText("Computer Score : "+Integer.toString(compPoints));
ps.setText(name.getText()+"'s Score : "+Integer.toString(userPoints));
result.setText("YOU LOSE !");
}
else
{
result.setText("IT'S A DRAW !");
}
} else if (userInput.contains("SCISSORS"))
{
if (rand1 == "PAPER")
{
userPoints++;
ps.setText(name.getText()+"'s Score : "+Integer.toString(userPoints));
result.setText("YOU WIN !");
} else if (rand1 == "ROCK")
{
compPoints++;
cs.setText("Computer Score : "+Integer.toString(compPoints));
ps.setText(name.getText()+"'s Score : "+Integer.toString(userPoints));
result.setText("YOU LOSE !");
}
else
{
result.setText("IT'S A DRAW !");
ps.setText(name.getText()+"'s Score : "+Integer.toString(userPoints));
}
}
else
{
result.setText("IT'S A DRAW !");
}
}
}`
I already added the background file under the project file but the image is not added to the GUI.
I am very new to JAVA so maybe this is a silly mistake but I really hope somebody can help me with this. I tried other methods but most will give me errrors only this version of code that can rude but the image is not added at all.

Related

How can I clear the JTextField and have it ready for input immediately without requiring a click or other action?

I posted my full code below so you guys could have a solid understanding of what I want. When I click send, the text field clears but is not ready for input. I have to click for the input. I want it to be ready for the input immediately and textfield.setText("") will not work. Any ideas?
package javaapplication2;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.io.*;
import javax.swing.*;//class to use to create GUI's
import javax.swing.border.Border;
import javax.swing.plaf.FontUIResource;
/**
*
* #author Jordan Anthony Hangman with dictionary
*/
public class JavaApplication2 extends JFrame{
static JLabel[] labels = new JLabel[20];//labels for displaying words under 20 characters
static int counter = 0;
static int badguess = 0;//keeps track of wrong guesses
static String wordbank = " Letter Bank:";
static char[] guesses = new char[26];
static int nguesses = 0;
static int amountc = 0;
public JavaApplication2(){
}
public static void main(String[] args) throws FileNotFoundException, IOException {
//Loading images to use for hangman game
ImageIcon hanginit = new ImageIcon("hangmaninit.jpg");
ImageIcon hang1 = new ImageIcon("hangman1.jpg");
ImageIcon hang2 = new ImageIcon("hangman2.jpg");
ImageIcon hang3 = new ImageIcon("hangman3.jpg");
ImageIcon hang4 = new ImageIcon("hangman4.jpg");
ImageIcon hang5 = new ImageIcon("hangman5.jpg");
ImageIcon hang6 = new ImageIcon("hangman6.jpg");
//initializing array for index values
int wordcount = 10;
String[] words1 = new String[wordcount];
int[] wordssel = new int[10];
for (int j=0; j<10; j++){
wordssel[j] = -1;
}
//setting instructions
String Instructions = "To start the game, click on the new game button. You get 6 incorrect guesses for every word. Once you get 6 guesses, the game will end. Click the end game button to go back to the home page. If you want to play again, you can click New Game and you will be able to guess a different word. After every game, a message will appear to tell you how many words you have guesses correctly. When all the words are played, a dialog box will be shown that says Game Over. Exit this box to exit the game.";
//reading in the words
int number = 0;
try(BufferedReader br = new BufferedReader(new FileReader("words.txt")))
{
String line = br.readLine();
while(line != null){
words1[number] = line;
number++;
line = br.readLine();
}
}
//randomizes words
boolean flag = false;
int rand = 0;
int count = 0;
int wordcounter = 0;
while(!flag){
Random random = new Random();
rand = random.nextInt(wordcount);
count = 0;
for (int o=0; o<wordcount; o++){
if(wordssel[o] != rand){
count++;
}
}
if(count == wordcount){
wordssel[wordcounter] = rand;
wordcounter++;
}
if(wordcounter == wordcount){
flag = true;
}
}
String[] randwords = new String[10];
for(int o=0; o<10; o++){
randwords[wordssel[o]] = words1[o];
}
//initializing label
for(int i=0; i<20; i++){
labels[i] = new JLabel(" ");
labels[i].setFont(new Font("Serif", Font.BOLD, 30));
}
//initializing guessedchar array
for(int i=0; i<26; i++){
guesses[i] = ' ';
}
//value for window size
int xlim = 1200;
int ylim = 1200;
int xlim2 = 1600;
int ylim2 = 1600;
int xlim3 = 900;
int ylim3 = 600;
//value for x and y centers
int xcent = xlim/2 - 20;
int ycent = ylim/2;
int xcent2 = xlim2/2;
int ycent2 = ylim2/2 - 20;
int xcent3 = xlim3/2;
int ycent3 = ylim3/2 - 20;
//creating frames
JFrame frame1 = new JFrame("Hangman");//creates a new frame or window
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// I thiunk exits when clicks on red x
frame1.setSize(xlim, ylim); //size of window in pixels
JFrame frame2 = new JFrame("Hangman");//creates a new frame or window
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// I thiunk exits when clicks on red x
frame2.setSize(xlim2, ylim2); //size of window in pixels
JFrame frame3 = new JFrame();
frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame3.setSize(xlim3, ylim3);
//change font size for label, button, and textfield
UIManager.put("Button.font", new FontUIResource(new Font("Dialog", Font.BOLD, 24)));
UIManager.put("Label.font", new FontUIResource(new Font("Dialog", Font.PLAIN, 24)));
UIManager.put("TextField.font", new FontUIResource(new Font("Dialog", Font.PLAIN, 24)));
UIManager.put("TextArea.font", new FontUIResource(new Font("Dialog", Font.PLAIN, 24)));
//creates buttons
JButton button1 = new JButton("New Game?");
button1.setBounds(xcent - 90, ycent - 165, 180, 130);//(xstart, ytop, width, height)
JButton button2 = new JButton("End Game?");
button2.setBounds(xcent - 90, ycent - 65, 180, 130);//(xstart, ytop, width, height)
JButton button3 = new JButton("Instructions");
button3.setBounds(xcent - 120, ycent, 240, 100);
JButton button4 = new JButton("Main Menu");
button4.setBounds(xcent - 100, ycent + 100, 200, 100);
//creating border
Border border = BorderFactory.createLineBorder(Color.BLACK, 4);
//creating labels
JLabel labelresult = new JLabel("");//label for win or losing game
labelresult.setBounds(400, 400, 400, 40);
JLabel labelhang = new JLabel();//label for hangman images
labelhang.setBounds(450, 375, 677, 620);
JLabel label3 = new JLabel("Hello");
label3.setBounds(200, 260, 500, 40);
JLabel label4 = new JLabel("Hello");
label4.setBounds(440, 1300, 800, 40);
JLabel label5 = new JLabel("Hello");
label5.setBounds(590, 1300, 800, 40);
JLabel label6 = new JLabel("Game Over");
label6.setBounds(380, 250, 200, 40);
JLabel lettersw = new JLabel();
JLabel label7 = new JLabel("How To Play");
label7.setBounds(500, 50, 200, 40);
JLabel label8 = new JLabel("Error");
label8.setBounds(590, 1300, 800, 40);
label8.setText("You can only enter one letter at a time.");
JLabel label9 = new JLabel("Error");
label9.setBounds(674, 1300, 800, 40);
label9.setText("You must enter a letter");
JLabel label10 = new JLabel(wordbank);
label10.setBounds(xcent2 - 200, 220, 400, 40);
JLabel label11 = new JLabel("You have already entered this letter.");
label11.setBounds(608, 1300, 800, 40);
JLabel label12 = new JLabel("You must exit game to guess.");
label12.setBounds(639, 1300, 800, 40);
//creating text area and setting properties
JTextArea texta1 = new JTextArea();
texta1.setOpaque(false);
texta1.setLineWrap(true);
texta1.setWrapStyleWord(true);
texta1.setBounds(100, 100, 1000, 600);
texta1.setText(Instructions);
//adding items to frames
frame1.add(button1);
frame1.add(button3);
frame2.add(button2);
frame2.add(labelhang);
frame3.add(label6);
frame2.add(label4);
frame2.add(label5);
frame3.add(label6);
frame1.add(label7);
frame1.add(texta1);
frame1.add(button4);
frame2.add(label8);
frame2.add(label9);
frame2.add(label10);
frame2.add(label11);
frame2.add(label12);
//set item properties
labelresult.setVisible(false);
button2.setVisible(false);
labelhang.setIcon(hanginit);
label4.setVisible(false);
label5.setVisible(false);
label7.setVisible(false);
texta1.setVisible(false);
button4.setVisible(false);
label8.setVisible(false);
label9.setVisible(false);
label10.setBorder(border);
label11.setVisible(false);
label12.setVisible(false);
//set frame properties
frame1.setLayout(null);
frame1.setVisible(true);//makes the frame visible to the user
frame3.setLayout(null);
//When End Game button is pressed
button2.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
//end of game conditions
label5.setVisible(false);
label4.setVisible(false);
label12.setVisible(false);
button2.setVisible(false);
frame2.setVisible(false);
wordbank = " Letter Bank:";
badguess = 0;
amountc = 0;
if(counter == wordcount){
frame3.setVisible(true);//frame after all words gone through
}
else{
labelhang.setIcon(hanginit);
frame1.setVisible(true);
}
}
});
//When Instructions button is pressed
button3.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
button1.setVisible(false);
button3.setVisible(false);
label7.setVisible(true);
texta1.setVisible(true);
button4.setVisible(true);
}
});
//When Main Menu is pressed
button4.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
button1.setVisible(true);
button4.setVisible(false);
button2.setVisible(true);
label7.setVisible(false);
texta1.setVisible(false);
button3.setVisible(true);
}
});
//When New Game is press
button1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
//set item properties
frame2.setLayout(null);
frame2.setVisible(true);
frame1.setVisible(false);
button2.setVisible(false);
label8.setVisible(false);
label9.setVisible(false);
wordbank = " Letter Bank:";
label10.setText(wordbank);
//initializes labels and word
for(int i=0; i<20; i++){
labels[i].setText(" ");
}
String currentword = randwords[counter];
counter++;
//initializing previos guesses
for(int i=0; i<26; i++){
guesses[i] = ' ';
}
nguesses = 0;
//creating panel and panel components always create panels and all components in the same place that they are being added
JButton send = new JButton("Send");
JButton clear = new JButton("clear");
JTextField tf = new JTextField(1);
JLabel label = new JLabel("Enter Guess");
JPanel panel = new JPanel();
panel.add(label);
panel.add(tf);
panel.add(send);
panel.add(clear);
panel.setBounds(xcent2 - 250, ylim2 - 102, 500, 40);
frame2.add(panel);
//placing labels for letters
int letters = currentword.length();
int spacing2 = 800-(int)(letters*12.5);
for (int i=0; i<letters; i++){
labels[i].setText("_");
int spacing = 25 * i;
labels[i].setBounds(spacing2 + spacing, 1100, 25, 40);
frame2.add(labels[i]);
}
//when clear button is pressed
clear.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
tf.setText("");
}
});
//when send button is pressed
send.addActionListener(new ActionListener(){
int amountc1 = 0;//keeps track of correctly guessed amount of words
int counter2 = 0;//keeps track of amount of correct letter guesses for each word
int letters2 = 0;
#Override
public void actionPerformed(ActionEvent e){
//initialize word and user guess
String currentword = randwords[counter-1];
letters2 = currentword.length();
char[] wordcurrent = new char[20];
wordcurrent = currentword.toCharArray();
String userguess = tf.getText();
userguess = userguess.toLowerCase();
tf.setText("");
//set properties
label4.setVisible(false);
label5.setVisible(false);
label8.setVisible(false);
label9.setVisible(false);
label11.setVisible(false);
//checking guess with word
boolean flag2 = false;
boolean flag3 = false;
//checking if word is already solved and user is entering data
System.out.println(badguess);
System.out.println(amountc);
System.out.println(nguesses);
if(nguesses > badguess + amountc){
label4.setVisible(false);
label5.setVisible(false);
label12.setVisible(true);
nguesses++;
flag2 = true;
}
else if(userguess.length() > 1){
label8.setVisible(true);
flag2 = true;
nguesses++;
}
else if(userguess.length() < 1){
label9.setVisible(true);
flag2 = true;
nguesses++;
}
else{
char[] guessuser = userguess.toCharArray();
char guessedchar = guessuser[0];
int charval = (int)guessedchar;//97-122 lowercase, 65-90 for uppercase
//checks for previous guesses
for(int t=0; t<=nguesses; t++){
if(guessedchar == guesses[t]){
flag3 = true;
break;
}
}
nguesses++;
//checking word against guess
for(int i=0; i<letters2; i++){
if(!flag3){
if(((charval>96 & charval<123))){
guesses[nguesses] = guessedchar;
if(wordcurrent[i] == guessedchar){
labels[i].setText(userguess);
labels[i].setVisible(true);
flag2 = true;
counter2++;
nguesses++;
amountc++;
}
}
else{
label9.setVisible(true);
flag2 = true;
}
}
else{
label11.setVisible(true);
flag2 = true;
}
}
}
//checks if full word is guessed correctly
if(counter2 == letters2){
this.amountc1++;
nguesses++;
button2.setVisible(true);
String correctword = "Well done! You have solved " + amountc1 + " out of " + counter;
label5.setText(correctword);
label5.setVisible(true);
counter2 = 0;
if(counter == wordcount){
label3.setText(correctword);
}
}
//checks for wrong guess and changes hangman picture accordingly
if (flag2 == false){
badguess++;
switch(badguess){
case 1:
wordbank += " " + userguess;
label10.setText(wordbank);
labelhang.setIcon(hang1);
break;
case 2:
wordbank += ", " + userguess;
label10.setText(wordbank);
labelhang.setIcon(hang2);
break;
case 3:
wordbank += ", " + userguess;
label10.setText(wordbank);
labelhang.setIcon(hang3);
break;
case 4:
wordbank += ", " + userguess;
label10.setText(wordbank);
labelhang.setIcon(hang4);
break;
case 5:
wordbank += ", " + userguess;
label10.setText(wordbank);
labelhang.setIcon(hang5);
break;
case 6:
nguesses++;
wordbank += ", " + userguess;
label10.setText(wordbank);
labelhang.setIcon(hang6);
button2.setVisible(true);
String incorrectword = "Sorry, The correct word was " + currentword + " You have solved " + amountc1 + " out of " + counter;
label4.setText(incorrectword);
label4.setVisible(true);
this.counter2 = 0;
if(counter == wordcount){
label3.setText(incorrectword);
}
break;
default:
break;
}
}
else{
nguesses--;
}
}
});
}
});
}
}
To focus text field after setText(""); method you can use the following,
textFieldName.requestFocus();
To center the text field you can use this code,
textFieldName.setHorizontalAlignment(JTextField.CENTER);

Calculator Can't have Decimal

My code now works for sum, subtract, multiple, divide.
However it doesn't show the decimal when I put 10.0 and 2.0 or 5.4 and 2.
How should I change, then it can be used for decimal??
This is my code now.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
// instance variables
JLabel num1Label = null;
JTextField num1Text = null;
JLabel num2Label = null;
JTextField num2Text = null;
JButton sumButton = null;
JButton multipleButton=null;
JButton divideButton=null;
JButton subtractButton = null;
JLabel ansLabel = null;
// constructor
public Calculator() {
setLayout(new GridBagLayout()); // set layout (how elements are arranged in window)
// **** label: num 1
GridBagConstraints layoutCon = new GridBagConstraints();
layoutCon.gridx = 0;
layoutCon.gridy = 0;
layoutCon.insets = new Insets(10,10,10,10);
num1Label = new JLabel("Num 1:");
add(num1Label,layoutCon); // added label to JFrame
// **** text field: num 1
layoutCon = new GridBagConstraints();
layoutCon.gridx = 1;
layoutCon.gridy = 0;
layoutCon.insets = new Insets(10,10,10,10);
num1Text = new JTextField(20);
add(num1Text, layoutCon);
// **** label: num 2
layoutCon = new GridBagConstraints();
layoutCon.gridx = 0;
layoutCon.gridy = 1;
layoutCon.insets = new Insets(10,10,10,10);
num2Label = new JLabel("Num 2:");
add(num2Label,layoutCon); // added label to JFrame
// **** text field: num 2
layoutCon = new GridBagConstraints();
layoutCon.gridx = 1;
layoutCon.gridy = 1;
layoutCon.insets = new Insets(10,10,10,10);
num2Text = new JTextField(20);
add(num2Text, layoutCon);
// **** Sum Button
layoutCon = new GridBagConstraints();
layoutCon.gridx = 0;
layoutCon.gridy = 2;
layoutCon.insets = new Insets(10,10,10,10);
sumButton = new JButton("Sum");
add(sumButton, layoutCon);
sumButton.addActionListener(this); // register sumButton with the ActionListener in Calculator
// Multiple Button
layoutCon= new GridBagConstraints();
layoutCon.gridx=0;
layoutCon.gridy=4;
layoutCon.insets=new Insets(10,10,10,10);
multipleButton= new JButton("Multiple");
add(multipleButton, layoutCon);
multipleButton.addActionListener(this);
// Divide Button
layoutCon = new GridBagConstraints();
layoutCon.gridx = 0;
layoutCon.gridy = 5;
layoutCon.insets = new Insets(10,10,10,10);
divideButton = new JButton("Divide");
add(divideButton, layoutCon);
divideButton.addActionListener(this);
// **** label: answer
layoutCon = new GridBagConstraints();
layoutCon.gridx = 1;
layoutCon.gridy = 2;
layoutCon.insets = new Insets(10,10,10,10);
ansLabel = new JLabel("Answer");
add(ansLabel,layoutCon); // added label to JFrame
// **** Subtract Button
layoutCon = new GridBagConstraints();
layoutCon.gridx = 0;
layoutCon.gridy = 3;
layoutCon.insets = new Insets(10,10,10,10);
subtractButton = new JButton("Subtract");
add(subtractButton, layoutCon);
subtractButton.addActionListener(this); // register subtractButton with the ActionListener in Calculator
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Sum")) {
ansLabel.setText("" + (Integer.parseInt(num1Text.getText()) + Integer.parseInt(num2Text.getText())));
}
else if (e.getActionCommand().equals("Subtract")) {
ansLabel.setText("" + (Integer.parseInt(num1Text.getText()) - Integer.parseInt(num2Text.getText())));
}
else if (e.getActionCommand().equals("Multiple")){
ansLabel.setText(""+(double)(Integer.parseInt(num1Text.getText())*Integer.parseInt(num2Text.getText())));
}
else if (e.getActionCommand().equals("Divide")){
ansLabel.setText(""+(Integer.parseInt(num1Text.getText())/Integer.parseInt(num2Text.getText())));
}
}
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.pack(); // resizes window (JFrame) so you can see the elements in it
calc.setVisible(true); // make window visible
calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit program when close window
}
}
actionPerformed is parsing its inputs as int and operating on them as such. If you want to use floating-point numbers, try using double instead.
Parsing as an integer cannot handle double's or any floating point number (decimal) as such change your actionPerformed to parse double's instead or parse int's:
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Sum")) {
ansLabel.setText("" + (Double.parseDouble(num1Text.getText()) + Double.parseDouble(num2Text.getText())));
}
else if (e.getActionCommand().equals("Subtract")) {
ansLabel.setText("" + (Double.parseDouble(num1Text.getText()) - Double.parseDouble(num2Text.getText())));
}
else if (e.getActionCommand().equals("Multiple")){
ansLabel.setText(""+(double)(Double.parseDouble(num1Text.getText())*Double.parseDouble(num2Text.getText())));
}
else if (e.getActionCommand().equals("Divide")){
ansLabel.setText(""+(Double.parseDouble(num1Text.getText())/Double.parseDouble(num2Text.getText())));
}
}
You use
Integer.parseInt(num1Text.getText());
which parses a String to an int.
With
Double.parseDouble(num1Text.getText());
you get a double.
Note that you might want to catch the NumberFormatException, since you most likley don't want your program to stop on malformed user input:
double num1;
try
{
num1 = Double.parseDouble(num1Text.getText());
} catch (NumberFormatException e)
{
// either use a default value or give feedback to the user on what he did wrong
// for example
num1 = 1;
}
If you don't want to print a double value, if only integer values are entered by the user, then you could first try to parse to int, catch the exception (if any) and then parse to double.
Another error in your divison line:
ansLabel.setText(
""+(Integer.parseInt(num1Text.getText())/Integer.parseInt(num2Text.getText())));
You do not check if the second parameter is != 0, otherwise you might divide by 0!
To keep your code more readable you might want to create an extra method for parsing a String to a number:
private static double parseDouble(String s)
{
double num;
try
{
num = Double.parseDouble(num1Text.getText());
} catch (NumberFormatException e)
{
// either use a default value or give feedback to the user on what he did wrong
// for example
num = 1;
}
return num;
}
A possible fix for the division by 0 could be:
double num1 = parseDouble(num1Text.getText());
double num2 = parseDouble(num2Text.getText());
String command = e.getActionCommand();
if (command.equals("Sum"))
{
ansLabel.setText("" + (num1 + num2));
} else if (command.equals("Subtract"))
{
ansLabel.setText("" + (num1 - num2));
} else if (command.equals("Multiple"))
{
ansLabel.setText("" + (num1 * num2));
} else if (command.equals("Divide"))
{
if (num2 == 0)
{
ansLabel.setText("ERROR: Division by zero!");
} else
{
ansLabel.setText("" + (num1 / num2));
}
}
you should use double data type instead of integer for 4 operations
Like this:
public void actionPerformed( ActionEvent e )
{
if( e.getActionCommand().equals( "Sum" ) )
ansLabel.setText( "" + ( Double.
parseDouble(num1Text.getText() ) + Double.parseDouble(
num2Text.getText() ) ) );
// etc.

event mouseclicked in java not working to populate text field

I am making a gui with netbeans. I right clicked a JTable to add event listener for mouse clicked. I added my code to the new method. For the life of me when I click the JTable I get no errors.
Any ideas on what I am doing wrong or how to fix it? if you need to see more of my code let me know. everything besides the connection to the DB is ran out of the driver class.
I moved my program to eclipse to make this easier for me to trouble shoot.
private void tableDisplayMouseClicked(java.awt.event.MouseEvent evt) {
try {
int row = tableDisplay.getSelectedRow();
String tableClick = (tableDisplay.getModel().getValueAt(row, 1).toString());
String sql = " select * from contact where id = ' " + tableClick + " ' ";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if(rs.next()) {
System.out.println("hey dude this method is being called.");
String add2 = rs.getString("Business_Name");
fieldBN.setText(add2);
String add3 = rs.getString("First_Name");
fieldFN.setText(add3);
String add4 = rs.getString("Last_Name");
fieldLN.setText(add4);
String add5 = rs.getString("Phone");
fieldP.setText(add5);
String add6 = rs.getString("Email");
fieldE.setText(add6);
String add7 = rs.getString("Address_Line_1");
fieldA.setText(add7);
String add8 = rs.getString("Address_Line_2");
aLine2.setText(add8);
String add9 = rs.getString("Website");
fieldW.setText(add9);
}
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
Here is the onclick method
Here is my even listener
public Driver() {
gui();
conn = DbConnect.ConnectDb();
UpdateTable();
tableDisplay.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tableDisplayMouseClicked(evt);
}
});
}
Here is the whole Driver Class:
import java.awt.BorderLayout;
public class Driver {
private JFrame f;
private JPanel p;
private JTextField fieldBN;
private JTextField fieldFN;
private JTextField fieldLN;
private JTextField fieldP;
private JTextField fieldE;
private JTextField fieldA;
private JTextField aLine2;
private JTextField fieldW;
private JLabel labelBN;
private JLabel labelFN;
private JLabel labelLN;
private JLabel labelP;
private JLabel labelE;
private JLabel labelA;
private JLabel labelW;
private JComboBox relationship;
private JButton buttonS;
private JTable tableDisplay;
String[] relationshipValues = { "Business", "Friend", "Family", "Professional" };
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
// Constructor:
public Driver() {
gui();
conn = DbConnect.ConnectDb();
UpdateTable();
tableDisplay.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tableDisplayMouseClicked(evt);
}
});
}
public void gui() {
f = new JFrame("Contact Book");
GridBagConstraints c = new GridBagConstraints();
p = new JPanel(new GridBagLayout());
f.getContentPane().add(p, BorderLayout.NORTH);
c.gridx = 0;
c.gridy = 0;
labelBN = new JLabel ("Business Name:");
p.add(labelBN, c);
c.gridx = 1;
c.gridy = 0;
fieldBN = new JTextField(10);
p.add(fieldBN, c);
c.gridx = 0;
c.gridy = 1;
labelFN= new JLabel ("First Name:");
p.add(labelFN, c);
c.gridx = 1;
c.gridy = 1;
fieldFN = new JTextField (10);
p.add(fieldFN, c);
c.gridx = 0;
c.gridy = 2;
labelLN= new JLabel ("Last Name:");
p.add(labelLN, c);
c.gridx = 1;
c.gridy = 2;
fieldLN = new JTextField (10);
p.add(fieldLN, c);
c.gridx = 0;
c.gridy = 3;
labelP = new JLabel ("Phone Number:");
p.add(labelP, c);
c.gridx = 1;
c.gridy = 3;
fieldP = new JTextField (10);
p.add(fieldP, c);
c.gridx = 0;
c.gridy = 4;
labelE = new JLabel ("Email:");
p.add(labelE, c);
c.gridx = 1;
c.gridy = 4;
fieldE = new JTextField (10);
p.add(fieldE, c);
c.gridx = 0;
c.gridy = 5;
labelA = new JLabel ("Address:");
p.add(labelA, c);
c.gridx = 1;
c.gridy = 5;
fieldA = new JTextField (10);
p.add(fieldA, c);
c.gridx = 1;
c.gridy = 6;
aLine2 = new JTextField (10);
p.add(aLine2, c);
c.gridx = 0;
c.gridy = 7;
labelW = new JLabel ("Website:");
p.add(labelW, c);
c.gridx = 1;
c.gridy = 7;
fieldW = new JTextField (10);
p.add(fieldW, c);
c.gridx = 0;
c.gridy = 8;
labelW = new JLabel ("Relationship:");
p.add(labelW, c);
c.gridx = 1;
c.gridy = 8;
relationship = new JComboBox(relationshipValues);
p.add(relationship, c);
c.gridx = 1;
c.gridy = 9;
buttonS = new JButton("Save:");
p.add(buttonS, c);
c.gridx = 0;
c.gridy = 10;
tableDisplay = new JTable();
p.add(tableDisplay, c);
// pack the frame for better cross platform support
f.pack();
// Make it visible
f.setVisible(true);
f.setSize(1400,900); // default size is 0,0
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // End of Gui Method
private void UpdateTable() {
try {
String sql = "SELECT * FROM contact";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
tableDisplay.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
private void tableDisplayMouseClicked(java.awt.event.MouseEvent evt) {
try {
int row = tableDisplay.getSelectedRow();
String tableClick = (tableDisplay.getModel().getValueAt(row, 1).toString());
String sql = " select * from contact where id = ' " + tableClick + " ' ";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if(rs.next()) {
System.out.println("hey dude this method is being called.");
String add2 = rs.getString("Business_Name");
fieldBN.setText(add2);
String add3 = rs.getString("First_Name");
fieldFN.setText(add3);
String add4 = rs.getString("Last_Name");
fieldLN.setText(add4);
String add5 = rs.getString("Phone");
fieldP.setText(add5);
String add6 = rs.getString("Email");
fieldE.setText(add6);
String add7 = rs.getString("Address_Line_1");
fieldA.setText(add7);
String add8 = rs.getString("Address_Line_2");
aLine2.setText(add8);
String add9 = rs.getString("Website");
fieldW.setText(add9);
}
}
catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Driver();
}
});
} // End main Method
} // End class Driver
It's difficult to know why you're having issues with the registering a MouseListener, there's not enough context to diagnose the problem.
You should start by placing out put statements in the code to check to see if the method is been called and that the query is returning values.
If all else fails, you should try regsitering the mouse listener manually. See How to Write a Mouse Listener for more details.
You query is also including spaces in the match...
String sql = " select * from contact where id = ' " + tableClick + " ' ";
^------------------^-----
Which will affect the ability for the database to match results to your query. Try removing these
String sql = " select * from contact where id = '" + tableClick + "'";

GRADING SYSTEM using JComboBox for the entry of grades; Jlabel, JTextFirld,JFrame; JButton for events; all in JFrame

here is my code for our finals. a grading system were the grades are all in 24 JComboBox. some with numbers till 10, 20 and 30. each quarter they are computed and shown in a JTextfield. when the four quarters are complete a Jbutton "COMPUTE ALL" calculates the entire data and will show in another frame the student's grade and weather he or she have passed or failed. this code works good with some problems: (1) my Jbutton "CLEAR ALL" doesn't work. it should show the default number "0" when clicked and the Jtextfields must be empty-(THIS ALREADY WORKS "TEXTFIELD TO EMPTY". (2) i want to hide the second frame when the Jbutton for compute all is clicked (f2.hide();f3.show();) but it keeps saying (cannot find symbol
symbol: variable f2cannot find symbol) (3) and lastly in my 3rd frame where the student's final grade and final rating are shown i cant really have it shown in a message dialog stating weather he or she passed or failed. THANKS. sorry for the poor coding.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class forfinals extends JFrame{
JFrame f1 = new JFrame ("HELLO");
JLabel Lb1 = new JLabel ("Enter your name");
JLabel Lb2 = new JLabel ("QUIZZES (20)");
JLabel Lb3 = new JLabel ("ASSIGNMENTS (10)");
JLabel Lb4 = new JLabel ("PROJECTS (20)");
JLabel Lb5 = new JLabel ("PARTICIPATION (10)");
JLabel Lb6 = new JLabel ("ATTENDANCE(10)");
JLabel Lb7 = new JLabel ("MAJOR EXAM (30)");
JLabel Lb8 = new JLabel ("Western College SY: 2011-2012");
JLabel Lb9 = new JLabel ("Final Grade");
JLabel Lb10 = new JLabel ("PRELIM");
JLabel Lb11 = new JLabel ("MIDTERM");
JLabel Lb12 = new JLabel ("PREFINAL");
JLabel Lb13 = new JLabel ("FINALS");
JLabel label1 = new JLabel ("YOUR FINAL GRADE");
JLabel label2 = new JLabel ("YOU EARNED THE RATING");
JTextField txt1 = new JTextField (15);
JTextField txt2 = new JTextField (2);
JTextField txt3 = new JTextField (2);
JTextField txt4 = new JTextField (2);
JTextField txt5 = new JTextField (2);
JTextField finalg = new JTextField (5);
JTextField finalr = new JTextField (5);
JButton Btn1 = new JButton ("OK");
JButton Btn2 = new JButton ("CANCEL");
JButton Btn3 = new JButton ("COMPUTE");
JButton Btn4 = new JButton ("COMPUTE");
JButton Btn5 = new JButton ("COMPUTE");
JButton Btn6 = new JButton ("COMPUTE");
JButton jcompute = new JButton ("COMPUTE ALL");
JButton jclear = new JButton ("CLEAR ALL");
JButton jexit = new JButton ("EXIT");
JButton finalb = new JButton ("OK");
//prelim
JComboBox cb1 = new JComboBox();
JComboBox cb2 = new JComboBox();
JComboBox cb3 = new JComboBox();
JComboBox cb4 = new JComboBox();
JComboBox cb5 = new JComboBox();
JComboBox cb6 = new JComboBox();
//midterm
JComboBox cb7 = new JComboBox();
JComboBox cb8 = new JComboBox();
JComboBox cb9 = new JComboBox();
JComboBox cb10 = new JComboBox();
JComboBox cb11 = new JComboBox();
JComboBox cb12 = new JComboBox();
//prefinal
JComboBox cb13 = new JComboBox();
JComboBox cb14 = new JComboBox();
JComboBox cb15 = new JComboBox();
JComboBox cb16 = new JComboBox();
JComboBox cb17 = new JComboBox();
JComboBox cb18 = new JComboBox();
//finals
JComboBox cb19 = new JComboBox();
JComboBox cb20 = new JComboBox();
JComboBox cb21 = new JComboBox();
JComboBox cb22 = new JComboBox();
JComboBox cb23 = new JComboBox();
JComboBox cb24 = new JComboBox();
public forfinals(){
f1.getContentPane().setLayout(null);
f1.setSize (300,350);
f1.getContentPane().add(Lb1);
f1.getContentPane().add(txt1);
f1.getContentPane().add(Btn1);
f1.getContentPane().add(Btn2);
Lb1.setBounds(40,70,100,75);
txt1.setBounds(150,90,100,30);
Btn1.setBounds(40,170,100,40);
Btn2.setBounds(150,170,100,40);
Btn1.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String x;
x = txt1.getText();
JFrame f2 = new JFrame (x);
f2.getContentPane().setLayout(null);
f2.setSize (830,600);
f1.hide();
f2.show();
f2.getContentPane().add(Lb2);
f2.getContentPane().add(Lb3);
f2.getContentPane().add(Lb4);
f2.getContentPane().add(Lb5);
f2.getContentPane().add(Lb6);
f2.getContentPane().add(Lb7);
f2.getContentPane().add(Lb8);
f2.getContentPane().add(Lb9);
f2.getContentPane().add(Lb10);
f2.getContentPane().add(Lb11);
f2.getContentPane().add(Lb12);
f2.getContentPane().add(Lb13);
f2.getContentPane().add(jcompute);
f2.getContentPane().add(jclear);
f2.getContentPane().add(jexit);
Lb2.setBounds(30,120,90,70);
Lb3.setBounds(30,170,110,70);
Lb4.setBounds(30,220,90,70);
Lb5.setBounds(30,270,120,70);
Lb6.setBounds(30,320,100,70);
Lb7.setBounds(30,370,110,70);
Lb8.setBounds(280,20,230,20);
Lb9.setBounds(30,420,80,70);
Lb10.setBounds(190,60,100,100);
Lb11.setBounds(315,60,100,100);
Lb12.setBounds(440,60,100,100);
Lb13.setBounds(570,60,100,100);
jcompute.setBounds(660,160,120, 60);
jclear.setBounds(660,260,120, 60);
jexit.setBounds(660,360,120,60);
//PRELIM
f2.getContentPane().add(cb1);
f2.getContentPane().add(cb2);
f2.getContentPane().add(cb3);
f2.getContentPane().add(cb4);
f2.getContentPane().add(cb5);
f2.getContentPane().add(cb6);
f2.getContentPane().add(Btn3);
f2.getContentPane().add(txt2);
txt2.setEditable(false);
cb1.setBounds(190,140,50,30);
cb2.setBounds(190,190,50,30);
cb3.setBounds(190,240,50,30);
cb4.setBounds(190,290,50,30);
cb5.setBounds(190,340,50,30);
cb6.setBounds(190,390,50,30);
Btn3.setBounds(170,490,95,40);
txt2.setBounds(190,440,55,35);
int numbers_to_add_max = 10;
for (int i = 0; i <= numbers_to_add_max; i++) {
cb2.addItem(new Integer(i));
cb4.addItem(new Integer(i));
cb5.addItem(new Integer(i));
}
int numbers = 20;
for (int i = 0; i <= numbers; i++) {
cb1.addItem(new Integer(i));
cb3.addItem(new Integer(i));
}
int numbers_to_add = 30;
for (int i = 0; i <= numbers_to_add; i++) {
cb6.addItem(new Integer(i));
}
//MIDTERM
f2.getContentPane().add(cb7);
f2.getContentPane().add(cb8);
f2.getContentPane().add(cb9);
f2.getContentPane().add(cb10);
f2.getContentPane().add(cb11);
f2.getContentPane().add(cb12);
f2.getContentPane().add(Btn4);
f2.getContentPane().add(txt3);
txt3.setEditable(false);
cb7.setBounds(315,140,50,30);
cb8.setBounds(315,190,50,30);
cb9.setBounds(315,240,50,30);
cb10.setBounds(315,290,50,30);
cb11.setBounds(315,340,50,30);
cb12.setBounds(315,390,50,30);
Btn4.setBounds(295,490,95,40);
txt3.setBounds(315,440,55,35);
int nu = 10;
for (int i = 0; i <= nu; i++) {
cb8.addItem(new Integer(i));
cb10.addItem(new Integer(i));
cb11.addItem(new Integer(i));
}
int num = 20;
for (int i = 0; i <= num; i++) {
cb7.addItem(new Integer(i));
cb9.addItem(new Integer(i));
}
int numb = 30;
for (int i = 0; i <= numb; i++) {
cb12.addItem(new Integer(i));
}
//PREFINAL
f2.getContentPane().add(cb13);
f2.getContentPane().add(cb14);
f2.getContentPane().add(cb15);
f2.getContentPane().add(cb16);
f2.getContentPane().add(cb17);
f2.getContentPane().add(cb18);
f2.getContentPane().add(Btn5);
f2.getContentPane().add(txt4);
txt4.setEditable(false);
cb13.setBounds(440,140,50,30);
cb14.setBounds(440,190,50,30);
cb15.setBounds(440,240,50,30);
cb16.setBounds(440,290,50,30);
cb17.setBounds(440,340,50,30);
cb18.setBounds(440,390,50,30);
Btn5.setBounds(420,490,95,40);
txt4.setBounds(440,440,55,35);
int toaddmax = 10;
for (int i = 0; i <= toaddmax; i++) {
cb14.addItem(new Integer(i));
cb16.addItem(new Integer(i));
cb17.addItem(new Integer(i));
}
int numbersadd = 20;
for (int i = 0; i <= numbersadd; i++) {
cb13.addItem(new Integer(i));
cb15.addItem(new Integer(i));
}
int numbers_toadd = 30;
for (int i = 0; i <= numbers_toadd; i++) {
cb18.addItem(new Integer(i));
}
//FINALS
f2.getContentPane().add(cb19);
f2.getContentPane().add(cb20);
f2.getContentPane().add(cb21);
f2.getContentPane().add(cb22);
f2.getContentPane().add(cb23);
f2.getContentPane().add(cb24);
f2.getContentPane().add(Btn6);
f2.getContentPane().add(txt5);
txt5.setEditable(false);
cb19.setBounds(565,140,50,30);
cb20.setBounds(565,190,50,30);
cb21.setBounds(565,240,50,30);
cb22.setBounds(565,290,50,30);
cb23.setBounds(565,340,50,30);
cb24.setBounds(565,390,50,30);
Btn6.setBounds(545,490,95,40);
txt5.setBounds(565,440,55,35);
int add_max = 10;
for (int i = 0; i <= add_max; i++) {
cb20.addItem(new Integer(i));
cb22.addItem(new Integer(i));
cb23.addItem(new Integer(i));
}
int number = 20;
for (int i = 0; i <= number; i++) {
cb19.addItem(new Integer(i));
cb21.addItem(new Integer(i));
}
int to_add = 30;
for (int i = 0; i <= to_add; i++) {
cb24.addItem(new Integer(i));
}
}
});
Btn2.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
});
Btn3.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb1Int = Integer.parseInt(cb1.getSelectedItem().toString());
int cb2Int = Integer.parseInt(cb2.getSelectedItem().toString());
int cb3Int = Integer.parseInt(cb3.getSelectedItem().toString());
int cb4Int = Integer.parseInt(cb4.getSelectedItem().toString());
int cb5Int = Integer.parseInt(cb5.getSelectedItem().toString());
int cb6Int = Integer.parseInt(cb6.getSelectedItem().toString());
txt2.setText(String.valueOf(cb1Int + cb2Int + cb3Int + cb4Int + cb5Int + cb6Int));
}
});
Btn4.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb7Int = Integer.parseInt(cb7.getSelectedItem().toString());
int cb8Int = Integer.parseInt(cb8.getSelectedItem().toString());
int cb9Int = Integer.parseInt(cb9.getSelectedItem().toString());
int cb10Int = Integer.parseInt(cb10.getSelectedItem().toString());
int cb11Int = Integer.parseInt(cb11.getSelectedItem().toString());
int cb12Int = Integer.parseInt(cb12.getSelectedItem().toString());
txt3.setText(String.valueOf(cb7Int + cb8Int + cb9Int + cb10Int + cb11Int + cb12Int));
}
});
Btn5.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb13Int = Integer.parseInt(cb13.getSelectedItem().toString());
int cb14Int = Integer.parseInt(cb14.getSelectedItem().toString());
int cb15Int = Integer.parseInt(cb15.getSelectedItem().toString());
int cb16Int = Integer.parseInt(cb16.getSelectedItem().toString());
int cb17Int = Integer.parseInt(cb17.getSelectedItem().toString());
int cb18Int = Integer.parseInt(cb18.getSelectedItem().toString());
txt4.setText(String.valueOf(cb13Int + cb14Int + cb15Int + cb16Int + cb17Int + cb18Int));
}
});
Btn6.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb19Int = Integer.parseInt(cb19.getSelectedItem().toString());
int cb20Int = Integer.parseInt(cb20.getSelectedItem().toString());
int cb21Int = Integer.parseInt(cb21.getSelectedItem().toString());
int cb22Int = Integer.parseInt(cb22.getSelectedItem().toString());
int cb23Int = Integer.parseInt(cb23.getSelectedItem().toString());
int cb24Int = Integer.parseInt(cb24.getSelectedItem().toString());
txt5.setText(String.valueOf(cb19Int + cb20Int + cb21Int + cb22Int + cb23Int + cb24Int));
}
});
jcompute.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String prelim, midterm, prefinal, finals, total;
double a, b, c, d, tg;
prelim = txt2.getText();
midterm = txt3.getText();
prefinal = txt4.getText();
finals = txt5.getText();
a = Double.parseDouble(prelim);
b = Double.parseDouble(midterm);
c = Double.parseDouble(prefinal);
d = Double.parseDouble(finals);
tg = (a + b + c + d)/4;
total = Double.toString(tg);
finalg.setText(total);
JFrame f3 = new JFrame ("STUDENT FINAL RATING");
f3.getContentPane().setLayout(null);
f3.setSize (350,300);
//the frame2 (f2); the two frames are still visible
f2.hide();
f3.show();
f3.getContentPane().add(label1);
f3.getContentPane().add(label2);
f3.getContentPane().add(finalg);
finalg.setEditable(false);
f3.getContentPane().add(finalr);
finalr.setEditable(false);
f3.getContentPane().add(finalb);
label1.setBounds(70,20,150,70);
label2.setBounds(90,70,200,70);
finalg.setBounds(205,40,50,30);
finalr.setBounds(140,140,50,40);
finalb.setBounds(130,200,70,30);
//EQUIVALENT
double grade, equiv;
grade = Double.parseDouble(finalg.getText());
if(grade>=99.50 && grade<101)
equiv = 1.00;
else if(grade<99.50 && grade>=98.50)
equiv = 1.10;
else if(grade<98.50 && grade>=97.50)
equiv = 1.20;
else if(grade<97.50 && grade>=96.50)
equiv = 1.30;
else if(grade<96.50 && grade>=95.50)
equiv = 1.40;
else if(grade<95.50 && grade>=94.50)
equiv = 1.50;
else if(grade<94.50 && grade>=93.50)
equiv = 1.60;
else if(grade<93.50 && grade>=92.50)
equiv = 1.70;
else if(grade<92.50 && grade>=91.50)
equiv = 1.80;
else if(grade<91.50 && grade>=90.50)
equiv = 1.90;
else if(grade<90.50 && grade>=89.50)
equiv = 2.00;
else if(grade<89.50 && grade>=88.50)
equiv = 2.10;
else if(grade<88.50 && grade>=87.50)
equiv = 2.20;
else if(grade<87.50 && grade>=86.50)
equiv = 2.30;
else if(grade<86.50 && grade>=85.50)
equiv = 2.40;
else if(grade<85.50 && grade>=84.50)
equiv = 2.50;
else if(grade<84.50 && grade>=83.50)
equiv = 2.60;
else if(grade<83.50 && grade>=82.50)
equiv = 2.70;
else if(grade<82.50 && grade>=81.50)
equiv = 2.80;
else if(grade<81.50 && grade>=80.50)
equiv = 2.90;
else if(grade<80.50 && grade>=79.50)
equiv = 3.00;
else if(grade<79.50 && grade>=78.50)
equiv = 3.10;
else if(grade<78.50 && grade>=77.50)
equiv = 3.20;
else if(grade<77.50 && grade>=76.50)
equiv = 3.30;
else if(grade<76.50 && grade>=75.50)
equiv = 3.40;
else if(grade<75.50 && grade>=74.50)
equiv = 3.50;
else
equiv = 5.0;
finalr.setText("" + equiv);
finalr.setEditable(false);
}
});
finalb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "" + equiv);
if (equi >=1.00 && equiv <=3.0) JOptionPane.showMessageDialog(null, " YOU PASSED!");
' else if (equiv >=3.10 && equiv <=75) JOptionPane.showMessageDialog(null, " YOU FAILED!");
}
});
jclear.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
//this part doesn't work. the JComboBox still contains the initialized number even though the "CLEAR ALL" button is clicked
cb1.setSelectedItem("0");
cb2.setSelectedItem("0");
cb3.setSelectedItem("0");
cb4.setSelectedItem("0");
cb5.setSelectedItem("0");
cb6.setSelectedItem("0");
cb7.setSelectedItem("0");
cb8.setSelectedItem("0");
cb9.setSelectedItem("0");
cb10.setSelectedItem("0");
cb11.setSelectedItem("0");
cb12.setSelectedItem("0");
cb13.setSelectedItem("0");
cb14.setSelectedItem("0");
cb15.setSelectedItem("0");
cb16.setSelectedItem("0");
cb17.setSelectedItem("0");
cb18.setSelectedItem("0");
cb19.setSelectedItem("0");
cb20.setSelectedItem("0");
cb21.setSelectedItem("0");
cb22.setSelectedItem("0");
cb23.setSelectedItem("0");
cb24.setSelectedItem("0");
txt2.setText(" ");
txt3.setText(" ");
txt4.setText(" ");
txt5.setText(" ");
}
});
jexit.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
});
f1.show();
}
public static void main (String args []){
forfinals xx = new forfinals();
}
}
Here is the complete corrected code,
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class forfinals extends JFrame{
JFrame f1 = new JFrame ("HELLO");
JFrame f2 = new JFrame();
double grade, equiv;
JLabel Lb1 = new JLabel ("Enter your name");
JLabel Lb2 = new JLabel ("QUIZZES (20)");
JLabel Lb3 = new JLabel ("ASSIGNMENTS (10)");
JLabel Lb4 = new JLabel ("PROJECTS (20)");
JLabel Lb5 = new JLabel ("PARTICIPATION (10)");
JLabel Lb6 = new JLabel ("ATTENDANCE(10)");
JLabel Lb7 = new JLabel ("MAJOR EXAM (30)");
JLabel Lb8 = new JLabel ("Western College SY: 2011-2012");
JLabel Lb9 = new JLabel ("Final Grade");
JLabel Lb10 = new JLabel ("PRELIM");
JLabel Lb11 = new JLabel ("MIDTERM");
JLabel Lb12 = new JLabel ("PREFINAL");
JLabel Lb13 = new JLabel ("FINALS");
JLabel label1 = new JLabel ("YOUR FINAL GRADE");
JLabel label2 = new JLabel ("YOU EARNED THE RATING");
JTextField txt1 = new JTextField (15);
JTextField txt2 = new JTextField (2);
JTextField txt3 = new JTextField (2);
JTextField txt4 = new JTextField (2);
JTextField txt5 = new JTextField (2);
JTextField finalg = new JTextField (5);
JTextField finalr = new JTextField (5);
JButton Btn1 = new JButton ("OK");
JButton Btn2 = new JButton ("CANCEL");
JButton Btn3 = new JButton ("COMPUTE");
JButton Btn4 = new JButton ("COMPUTE");
JButton Btn5 = new JButton ("COMPUTE");
JButton Btn6 = new JButton ("COMPUTE");
JButton jcompute = new JButton ("COMPUTE ALL");
JButton jclear = new JButton ("CLEAR ALL");
JButton jexit = new JButton ("EXIT");
JButton finalb = new JButton ("OK");
//prelim
JComboBox cb1 = new JComboBox();
JComboBox cb2 = new JComboBox();
JComboBox cb3 = new JComboBox();
JComboBox cb4 = new JComboBox();
JComboBox cb5 = new JComboBox();
JComboBox cb6 = new JComboBox();
//midterm
JComboBox cb7 = new JComboBox();
JComboBox cb8 = new JComboBox();
JComboBox cb9 = new JComboBox();
JComboBox cb10 = new JComboBox();
JComboBox cb11 = new JComboBox();
JComboBox cb12 = new JComboBox();
//prefinal
JComboBox cb13 = new JComboBox();
JComboBox cb14 = new JComboBox();
JComboBox cb15 = new JComboBox();
JComboBox cb16 = new JComboBox();
JComboBox cb17 = new JComboBox();
JComboBox cb18 = new JComboBox();
//finals
JComboBox cb19 = new JComboBox();
JComboBox cb20 = new JComboBox();
JComboBox cb21 = new JComboBox();
JComboBox cb22 = new JComboBox();
JComboBox cb23 = new JComboBox();
JComboBox cb24 = new JComboBox();
public forfinals(){
f1.getContentPane().setLayout(null);
f1.setSize (300,350);
f1.getContentPane().add(Lb1);
f1.getContentPane().add(txt1);
f1.getContentPane().add(Btn1);
f1.getContentPane().add(Btn2);
Lb1.setBounds(40,70,100,75);
txt1.setBounds(150,90,100,30);
Btn1.setBounds(40,170,100,40);
Btn2.setBounds(150,170,100,40);
Btn1.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String x;
x = txt1.getText();
f2.setTitle(x);
f2.getContentPane().setLayout(null);
f2.setSize (830,600);
f1.hide();
f2.show();
f2.getContentPane().add(Lb2);
f2.getContentPane().add(Lb3);
f2.getContentPane().add(Lb4);
f2.getContentPane().add(Lb5);
f2.getContentPane().add(Lb6);
f2.getContentPane().add(Lb7);
f2.getContentPane().add(Lb8);
f2.getContentPane().add(Lb9);
f2.getContentPane().add(Lb10);
f2.getContentPane().add(Lb11);
f2.getContentPane().add(Lb12);
f2.getContentPane().add(Lb13);
f2.getContentPane().add(jcompute);
f2.getContentPane().add(jclear);
f2.getContentPane().add(jexit);
Lb2.setBounds(30,120,90,70);
Lb3.setBounds(30,170,110,70);
Lb4.setBounds(30,220,90,70);
Lb5.setBounds(30,270,120,70);
Lb6.setBounds(30,320,100,70);
Lb7.setBounds(30,370,110,70);
Lb8.setBounds(280,20,230,20);
Lb9.setBounds(30,420,80,70);
Lb10.setBounds(190,60,100,100);
Lb11.setBounds(315,60,100,100);
Lb12.setBounds(440,60,100,100);
Lb13.setBounds(570,60,100,100);
jcompute.setBounds(660,160,120, 60);
jclear.setBounds(660,260,120, 60);
jexit.setBounds(660,360,120,60);
//PRELIM
f2.getContentPane().add(cb1);
f2.getContentPane().add(cb2);
f2.getContentPane().add(cb3);
f2.getContentPane().add(cb4);
f2.getContentPane().add(cb5);
f2.getContentPane().add(cb6);
f2.getContentPane().add(Btn3);
f2.getContentPane().add(txt2);
txt2.setEditable(false);
cb1.setBounds(190,140,50,30);
cb2.setBounds(190,190,50,30);
cb3.setBounds(190,240,50,30);
cb4.setBounds(190,290,50,30);
cb5.setBounds(190,340,50,30);
cb6.setBounds(190,390,50,30);
Btn3.setBounds(170,490,95,40);
txt2.setBounds(190,440,55,35);
int numbers_to_add_max = 10;
for (int i = 0; i <= numbers_to_add_max; i++) {
cb2.addItem(new Integer(i));
cb4.addItem(new Integer(i));
cb5.addItem(new Integer(i));
}
int numbers = 20;
for (int i = 0; i <= numbers; i++) {
cb1.addItem(new Integer(i));
cb3.addItem(new Integer(i));
}
int numbers_to_add = 30;
for (int i = 0; i <= numbers_to_add; i++) {
cb6.addItem(new Integer(i));
}
//MIDTERM
f2.getContentPane().add(cb7);
f2.getContentPane().add(cb8);
f2.getContentPane().add(cb9);
f2.getContentPane().add(cb10);
f2.getContentPane().add(cb11);
f2.getContentPane().add(cb12);
f2.getContentPane().add(Btn4);
f2.getContentPane().add(txt3);
txt3.setEditable(false);
cb7.setBounds(315,140,50,30);
cb8.setBounds(315,190,50,30);
cb9.setBounds(315,240,50,30);
cb10.setBounds(315,290,50,30);
cb11.setBounds(315,340,50,30);
cb12.setBounds(315,390,50,30);
Btn4.setBounds(295,490,95,40);
txt3.setBounds(315,440,55,35);
int nu = 10;
for (int i = 0; i <= nu; i++) {
cb8.addItem(new Integer(i));
cb10.addItem(new Integer(i));
cb11.addItem(new Integer(i));
}
int num = 20;
for (int i = 0; i <= num; i++) {
cb7.addItem(new Integer(i));
cb9.addItem(new Integer(i));
}
int numb = 30;
for (int i = 0; i <= numb; i++) {
cb12.addItem(new Integer(i));
}
//PREFINAL
f2.getContentPane().add(cb13);
f2.getContentPane().add(cb14);
f2.getContentPane().add(cb15);
f2.getContentPane().add(cb16);
f2.getContentPane().add(cb17);
f2.getContentPane().add(cb18);
f2.getContentPane().add(Btn5);
f2.getContentPane().add(txt4);
txt4.setEditable(false);
cb13.setBounds(440,140,50,30);
cb14.setBounds(440,190,50,30);
cb15.setBounds(440,240,50,30);
cb16.setBounds(440,290,50,30);
cb17.setBounds(440,340,50,30);
cb18.setBounds(440,390,50,30);
Btn5.setBounds(420,490,95,40);
txt4.setBounds(440,440,55,35);
int toaddmax = 10;
for (int i = 0; i <= toaddmax; i++) {
cb14.addItem(new Integer(i));
cb16.addItem(new Integer(i));
cb17.addItem(new Integer(i));
}
int numbersadd = 20;
for (int i = 0; i <= numbersadd; i++) {
cb13.addItem(new Integer(i));
cb15.addItem(new Integer(i));
}
int numbers_toadd = 30;
for (int i = 0; i <= numbers_toadd; i++) {
cb18.addItem(new Integer(i));
}
//FINALS
f2.getContentPane().add(cb19);
f2.getContentPane().add(cb20);
f2.getContentPane().add(cb21);
f2.getContentPane().add(cb22);
f2.getContentPane().add(cb23);
f2.getContentPane().add(cb24);
f2.getContentPane().add(Btn6);
f2.getContentPane().add(txt5);
txt5.setEditable(false);
cb19.setBounds(565,140,50,30);
cb20.setBounds(565,190,50,30);
cb21.setBounds(565,240,50,30);
cb22.setBounds(565,290,50,30);
cb23.setBounds(565,340,50,30);
cb24.setBounds(565,390,50,30);
Btn6.setBounds(545,490,95,40);
txt5.setBounds(565,440,55,35);
int add_max = 10;
for (int i = 0; i <= add_max; i++) {
cb20.addItem(new Integer(i));
cb22.addItem(new Integer(i));
cb23.addItem(new Integer(i));
}
int number = 20;
for (int i = 0; i <= number; i++) {
cb19.addItem(new Integer(i));
cb21.addItem(new Integer(i));
}
int to_add = 30;
for (int i = 0; i <= to_add; i++) {
cb24.addItem(new Integer(i));
}
}
});
Btn2.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
});
Btn3.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb1Int = Integer.parseInt(cb1.getSelectedItem().toString());
int cb2Int = Integer.parseInt(cb2.getSelectedItem().toString());
int cb3Int = Integer.parseInt(cb3.getSelectedItem().toString());
int cb4Int = Integer.parseInt(cb4.getSelectedItem().toString());
int cb5Int = Integer.parseInt(cb5.getSelectedItem().toString());
int cb6Int = Integer.parseInt(cb6.getSelectedItem().toString());
txt2.setText(String.valueOf(cb1Int + cb2Int + cb3Int + cb4Int + cb5Int + cb6Int));
}
});
Btn4.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb7Int = Integer.parseInt(cb7.getSelectedItem().toString());
int cb8Int = Integer.parseInt(cb8.getSelectedItem().toString());
int cb9Int = Integer.parseInt(cb9.getSelectedItem().toString());
int cb10Int = Integer.parseInt(cb10.getSelectedItem().toString());
int cb11Int = Integer.parseInt(cb11.getSelectedItem().toString());
int cb12Int = Integer.parseInt(cb12.getSelectedItem().toString());
txt3.setText(String.valueOf(cb7Int + cb8Int + cb9Int + cb10Int + cb11Int + cb12Int));
}
});
Btn5.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb13Int = Integer.parseInt(cb13.getSelectedItem().toString());
int cb14Int = Integer.parseInt(cb14.getSelectedItem().toString());
int cb15Int = Integer.parseInt(cb15.getSelectedItem().toString());
int cb16Int = Integer.parseInt(cb16.getSelectedItem().toString());
int cb17Int = Integer.parseInt(cb17.getSelectedItem().toString());
int cb18Int = Integer.parseInt(cb18.getSelectedItem().toString());
txt4.setText(String.valueOf(cb13Int + cb14Int + cb15Int + cb16Int + cb17Int + cb18Int));
}
});
Btn6.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
int cb19Int = Integer.parseInt(cb19.getSelectedItem().toString());
int cb20Int = Integer.parseInt(cb20.getSelectedItem().toString());
int cb21Int = Integer.parseInt(cb21.getSelectedItem().toString());
int cb22Int = Integer.parseInt(cb22.getSelectedItem().toString());
int cb23Int = Integer.parseInt(cb23.getSelectedItem().toString());
int cb24Int = Integer.parseInt(cb24.getSelectedItem().toString());
txt5.setText(String.valueOf(cb19Int + cb20Int + cb21Int + cb22Int + cb23Int + cb24Int));
}
});
jcompute.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
String prelim, midterm, prefinal, finals, total;
double a, b, c, d, tg;
prelim = txt2.getText();
midterm = txt3.getText();
prefinal = txt4.getText();
finals = txt5.getText();
a = Double.parseDouble(prelim);
b = Double.parseDouble(midterm);
c = Double.parseDouble(prefinal);
d = Double.parseDouble(finals);
tg = (a + b + c + d)/4;
total = Double.toString(tg);
finalg.setText(total);
JFrame f3 = new JFrame ("STUDENT FINAL RATING");
f3.getContentPane().setLayout(null);
f3.setSize (350,300);
//the frame2 (f2); the two frames are still visible
f2.hide();
f3.show();
f3.getContentPane().add(label1);
f3.getContentPane().add(label2);
f3.getContentPane().add(finalg);
finalg.setEditable(false);
f3.getContentPane().add(finalr);
finalr.setEditable(false);
f3.getContentPane().add(finalb);
label1.setBounds(70,20,150,70);
label2.setBounds(90,70,200,70);
finalg.setBounds(205,40,50,30);
finalr.setBounds(140,140,50,40);
finalb.setBounds(130,200,70,30);
//EQUIVALENT
grade = Double.parseDouble(finalg.getText());
if(grade>=99.50 && grade<101)
equiv = 1.00;
else if(grade<99.50 && grade>=98.50)
equiv = 1.10;
else if(grade<98.50 && grade>=97.50)
equiv = 1.20;
else if(grade<97.50 && grade>=96.50)
equiv = 1.30;
else if(grade<96.50 && grade>=95.50)
equiv = 1.40;
else if(grade<95.50 && grade>=94.50)
equiv = 1.50;
else if(grade<94.50 && grade>=93.50)
equiv = 1.60;
else if(grade<93.50 && grade>=92.50)
equiv = 1.70;
else if(grade<92.50 && grade>=91.50)
equiv = 1.80;
else if(grade<91.50 && grade>=90.50)
equiv = 1.90;
else if(grade<90.50 && grade>=89.50)
equiv = 2.00;
else if(grade<89.50 && grade>=88.50)
equiv = 2.10;
else if(grade<88.50 && grade>=87.50)
equiv = 2.20;
else if(grade<87.50 && grade>=86.50)
equiv = 2.30;
else if(grade<86.50 && grade>=85.50)
equiv = 2.40;
else if(grade<85.50 && grade>=84.50)
equiv = 2.50;
else if(grade<84.50 && grade>=83.50)
equiv = 2.60;
else if(grade<83.50 && grade>=82.50)
equiv = 2.70;
else if(grade<82.50 && grade>=81.50)
equiv = 2.80;
else if(grade<81.50 && grade>=80.50)
equiv = 2.90;
else if(grade<80.50 && grade>=79.50)
equiv = 3.00;
else if(grade<79.50 && grade>=78.50)
equiv = 3.10;
else if(grade<78.50 && grade>=77.50)
equiv = 3.20;
else if(grade<77.50 && grade>=76.50)
equiv = 3.30;
else if(grade<76.50 && grade>=75.50)
equiv = 3.40;
else if(grade<75.50 && grade>=74.50)
equiv = 3.50;
else
equiv = 5.0;
finalr.setText("" + equiv);
finalr.setEditable(false);
}
});
finalb.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "" + equiv);
if (equiv >=1.00 && equiv <=3.0) JOptionPane.showMessageDialog(null, " YOU PASSED!");
else if (equiv >=3.10 && equiv <=75) JOptionPane.showMessageDialog(null, " YOU FAILED!");
}
});
jclear.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
cb1.setSelectedItem(Integer.valueOf(0));
cb2.setSelectedItem(Integer.valueOf(0));
cb3.setSelectedItem(Integer.valueOf(0));
cb4.setSelectedItem(Integer.valueOf(0));
cb5.setSelectedItem(Integer.valueOf(0));
cb6.setSelectedItem(Integer.valueOf(0));
cb7.setSelectedItem(Integer.valueOf(0));
cb8.setSelectedItem(Integer.valueOf(0));
cb9.setSelectedItem(Integer.valueOf(0));
cb10.setSelectedItem(Integer.valueOf(0));
cb11.setSelectedItem(Integer.valueOf(0));
cb12.setSelectedItem(Integer.valueOf(0));
cb13.setSelectedItem(Integer.valueOf(0));
cb14.setSelectedItem(Integer.valueOf(0));
cb15.setSelectedItem(Integer.valueOf(0));
cb16.setSelectedItem(Integer.valueOf(0));
cb17.setSelectedItem(Integer.valueOf(0));
cb18.setSelectedItem(Integer.valueOf(0));
cb19.setSelectedItem(Integer.valueOf(0));
cb20.setSelectedItem(Integer.valueOf(0));
cb21.setSelectedItem(Integer.valueOf(0));
cb22.setSelectedItem(Integer.valueOf(0));
cb23.setSelectedItem(Integer.valueOf(0));
cb24.setSelectedItem(Integer.valueOf(0));
txt2.setText(" ");
txt3.setText(" ");
txt4.setText(" ");
txt5.setText(" ");
}
});
jexit.addActionListener(new ActionListener(){
public void actionPerformed (ActionEvent e){
System.exit(0);
}
});
f1.show();
}
public static void main (String args []){
forfinals xx = new forfinals();
}
}
These were the problems,
1.When you give zero in quotes like "0" its taken as string, whereas Integer is what required.
2.The jFrame f2 was not accessible because it was defined inside another action event.
3.The same was the problem with the variable equiv, thats why it was not accessible in the event of jOptionpane.

Write to text file through a java GUI

I have created a java GUI that works as a 6-question questionnaire. For each of the six question, I have 4 choices , i, ii, iii, iv for the user to choose, and then type out one of the four choices in a text box, then click a button "enter" to go to the next question. After all six questions have been answered, it will jump to a finish page.
I want to write the input value (in the text box) of each question to a text file. So After clicking on the "enter button" to answer all six questions I can see something like "i ii iii i ii iii" in a text file.
Is there a way to do it?
Here is my code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class HTML extends Applet implements ActionListener
{
private TextField question;
private Button enter, start;
int count = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
String text, input;
private Label intro1, intro2;
private Label qone1, qone2, qone3, qone4, qone5, qone6;
private Label qtwo1, qtwo2, qtwo3, qtwo4, qtwo5, qtwo6;
private Label qthree1, qthree2, qthree3, qthree4, qthree5, qthree6;
private Label qfour1, qfour2, qfour3, qfour4, qfour5, qfour6;
private Label qfive1, qfive2, qfive3, qfive4, qfive5, qfive6;
private Label qsix1, qsix2, qsix3, qsix4, qsix5, qsix6;
private Label finish1, finish2, finish3;
public void init()
{
setLayout(null);
start = new Button ("Start");
question = new TextField(10);
enter = new Button ("Enter");
if (count == 0)
{
setBackground( Color.yellow);
intro1 = new Label("Target Advertising", Label.CENTER);
intro1.setFont(new Font("Times-Roman", Font.BOLD, 16));
intro2 = new Label("Welcome to this questionnaire. In order to show the most appropriate advertisement, we would like to know more about your personal preferences.");
add(intro1);
add(intro2);
intro1.setBounds(0,0,800,20);
intro2.setBounds(15,20,800,20);
add(start);
start.setBounds(370,60,70,23);
start.addActionListener(this);
}
if(count == 1)
{
setBackground( Color.yellow );
qone1 = new Label("Question 1", Label.LEFT);
qone1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qone2 = new Label("On average, How many hours do you spend on playing sports every week?");
qone3 = new Label("i.0-2");
qone4 = new Label("ii.3-6");
qone5 = new Label("iii.7-10");
qone6 = new Label("iv.10+");
add(qone1);
add(qone2);
add(qone3);
add(qone4);
add(qone5);
add(qone6);
qone1.setBounds(15,0,800,20);
qone2.setBounds(15,20,800,15);
qone3.setBounds(15,60,800,15);
qone4.setBounds(15,80,800,15);
qone5.setBounds(15,100,800,15);
qone6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if (count == 2)
{
qtwo1 = new Label("Question 2", Label.LEFT);
qtwo1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qtwo2 = new Label("On average, How many hours do you spend on qsixening to music every week?");
qtwo3 = new Label("i. 0-4 ");
qtwo4 = new Label("ii. 5-10");
qtwo5 = new Label("iii. 11-20");
qtwo6 = new Label("iv. 20+");
add(qtwo1);
add(qtwo2);
add(qtwo3);
add(qtwo4);
add(qtwo5);
add(qtwo6);
qtwo1.setBounds(15,20,800,15);
qtwo2.setBounds(15,40,800,15);
qtwo3.setBounds(15,60,800,15);
qtwo4.setBounds(15,80,800,15);
qtwo5.setBounds(15,100,800,15);
qtwo6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 3)
{
qthree1 = new Label("Question 3", Label.LEFT);
qthree1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qthree2 = new Label("On average, How many hours do you spend on using computers every week?");
qthree3 = new Label("i.0-2");
qthree4 = new Label("ii.3-10");
qthree5 = new Label("iii.11-15");
qthree6 = new Label("iv.20+");
add(qthree1);
add(qthree2);
add(qthree3);
add(qthree4);
add(qthree5);
add(qthree6);
qthree1.setBounds(15,20,800,20);
qthree2.setBounds(15,40,800,15);
qthree3.setBounds(15,60,800,15);
qthree4.setBounds(15,80,800,15);
qthree5.setBounds(15,100,800,15);
qthree6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 4)
{
qfour1 = new Label("Question 4", Label.LEFT);
qfour1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qfour2 = new Label("On average, How many hours do you spend on groceries every week?");
qfour3 = new Label("i.0-2");
qfour4 = new Label("ii.3-10");
qfour5 = new Label("iii.11-15");
qfour6 = new Label("iv.20+");
add(qfour1);
add(qfour2);
add(qfour3);
add(qfour4);
add(qfour5);
add(qfour6);
qfour1.setBounds(15,20,800,15);
qfour2.setBounds(15,40,800,15);
qfour3.setBounds(15,60,800,15);
qfour4.setBounds(15,80,800,15);
qfour5.setBounds(15,100,800,15);
qfour6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 5)
{
qfive1 = new Label("Question 5", Label.LEFT);
qfive1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qfive2 = new Label("On average, How many hours do you spend on watching TV every week?");
qfive3 = new Label("i.0-2");
qfive4 = new Label("ii.3-10");
qfive5 = new Label("iii.11-15");
qfive6 = new Label("iv.20+");
add(qfive1);
add(qfive2);
add(qfive3);
add(qfive4);
add(qfive5);
add(qfive6);
qfive1.setBounds(15,20,800,15);
qfive2.setBounds(15,40,800,15);
qfive3.setBounds(15,60,800,15);
qfive4.setBounds(15,80,800,15);
qfive5.setBounds(15,100,800,15);
qfive6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
if(count == 6)
{
qsix1 = new Label("Question 6", Label.LEFT);
qsix1.setFont(new Font("Times-Roman", Font.BOLD, 16));
qsix2 = new Label("On average, How many times do you spend with family every week?");
qsix3 = new Label("i.0-2");
qsix4 = new Label("ii.3-10");
qsix5 = new Label("iii.11-15");
qsix6 = new Label("iv.20+");
add(qsix1);
add(qsix2);
add(qsix3);
add(qsix4);
add(qsix5);
add(qsix6);
qsix1.setBounds(15,20,800,15);
qsix2.setBounds(15,40,800,15);
qsix3.setBounds(15,60,800,15);
qsix4.setBounds(15,80,800,15);
qsix5.setBounds(15,100,800,15);
qsix6.setBounds(15,120,800,15);
add(question);
add(enter);
question.setBounds(15,140,70,15);
enter.setBounds(90,140,110,23);
question.addActionListener(this);
enter.addActionListener(this);
}
finish1 = new Label("Thank You." , Label.CENTER);
finish1.setFont(new Font("Times-Roman", Font.BOLD, 50));
finish2 = new Label("Questionnaire Completed.", Label.CENTER);
finish2.setFont(new Font("Times-Roman", Font.BOLD, 50));
add(finish1);
add(finish2);
finish1.setBounds(0,200,800,60);
finish2.setBounds(0,300,800,60);
}
}
public void actionPerformed(ActionEvent ae)
{
String button = ae.getActionCommand();
text = question.getText();
b = 0;
c = 0;
if (count == 6)
{
input = text.toUpperCase();
remove(enter);
remove(question);
question.setText("");
remove(qsix1);
remove(qsix2);
remove(qsix3);
remove(qsix4);
remove(qsix5);
remove(qsix6);
if(input.equals("OL"))
{
b = 1;
count = 7;
init();
}
else
{
b = 2;
count = 7;
init();
}
}
if (count == 5)
{
input = text.toUpperCase();
remove(enter);
remove(question);
question.setText("");
remove(qfive1);
remove(qfive2);
remove(qfive3);
remove(qfive4);
remove(qfive5);
remove(qfive6);
if(input.equals("BR"))
{
b = 1;
count = 6;
init();
}
else
{
b = 2;
count = 6;
init();
}
}
if (count == 4)
{
input = text.toLowerCase();
remove(enter);
remove(question);
question.setText("");
remove(qfour1);
remove(qfour2);
remove(qfour3);
remove(qfour4);
remove(qfour5);
remove(qfour6);
}
if(input.equals("no"))
{
b = 1;
count = 5;
init();
}
else
{
b = 2;
count = 5;
init();
}
}
if (count == 3)
{
input = text.toLowerCase();
remove(enter);
remove(question);
question.setText("");
remove(qthree1);
remove(qthree2);
remove(qthree3);
remove(qthree4);
remove(qthree5);
remove(qthree6);
if(input.equals("black"))
{
b = 1;
count = 4;
init();
}
else
{
b = 2;
count = 4;
init();
}
}
if (count == 2)
{
input = text.toLowerCase();
remove(enter);
remove(question);
question.setText("");
remove(qtwo1);
remove(qtwo2);
remove(qtwo3);
remove(qtwo4);
remove(qtwo5);
remove(qtwo6);
if(input.equals("yes"))
{
b = 1;
count = 3;
init();
}
else
{
b = 2;
count = 3;
init();
}
}
if (count == 1)
{
input = text.toUpperCase();
remove(enter);
remove(question);
question.setText("");
remove(qone1);
remove(qone2);
remove(qone3);
remove(qone4);
remove(qone5);
remove(qone6);
if(input.equals("i"))
{
b = 1;
count = 2;
init();
}
else
{
b = 1;
count = 1;
init();
}
}
if (count == 0)
{
remove(intro1);
remove(intro2);
remove(start);
count = 1;
init();
}
this.validate();
}
}
Sure, something like
OutputStream out = new FileOutputStream("/path/to/your/file");
out.write(answer.getBytes());
out.close();

Categories