|
| 14 Jun 2012 04:10 PM |
Hello, I'm working on an interface in Java which simply allows users to insert some data concerning videogames link to grades into a file. The problem is, I'm having some globalization problems with the variables, and I have no idea what's wrong. This may be a forum for Lua, but I don't have access to any other programming forums. =/
Here's what's written on the file, no matter what I type:
Name: Null Average Daily Homework Hours: 0.0 Average Daily Grades: Null Average Daily TV Hours: 0.0 Average Daily Videogame Hours: 0.0
Here's the program:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*;
public class GUI extends JFrame implements ActionListener { String Name = "Null"; String Grades = "Null"; double TV = 0; double VG = 0; double HWH = 0; static File file; static PrintStream Writer; JLabel Title = new JLabel(" Williamson Statistics "); JLabel LName = new JLabel("Name: "); JLabel LTVHours = new JLabel("Daily TV Hours: "); JLabel LVG = new JLabel("Daily Videogame Hours: "); JLabel LHomeWorkHours = new JLabel("Daily Homework Hours: "); JLabel LGrades = new JLabel("Average Grade Level: "); JTextField TFName = new JTextField(20); JTextField TFTVHours = new JTextField(16); JTextField TFVG = new JTextField(12); JTextField TFHomeWorkHours = new JTextField(12); JTextField TFGrades = new JTextField(12); JButton VerificationButton = new JButton(" Verify "); public GUI() { getContentPane().setLayout(new FlowLayout()); getContentPane().add(Title); getContentPane().add(LName); getContentPane().add(TFName); getContentPane().add(LTVHours); getContentPane().add(TFTVHours); getContentPane().add(LVG); getContentPane().add(TFVG); getContentPane().add(LGrades); getContentPane().add(TFGrades); getContentPane().add(VerificationButton); TFName.setEditable(true); TFTVHours.setEditable(true); TFVG.setEditable(true); TFHomeWorkHours.setEditable(true); TFGrades.setEditable(true); TFName.addActionListener(this); TFTVHours.addActionListener(this); TFVG.addActionListener(this); TFHomeWorkHours.addActionListener(this); TFGrades.addActionListener(this); VerificationButton.addActionListener(this); TFName.setActionCommand("Name"); TFTVHours.setActionCommand("TV"); TFVG.setActionCommand("VG"); TFHomeWorkHours.setActionCommand("HW"); TFGrades.setActionCommand("Grades"); VerificationButton.setActionCommand("Save"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent evt) { if(evt.getActionCommand().equals("Name")) { Name = TFName.getText().trim(); } if(evt.getActionCommand().equals("TV")) { if(isDoubleParsable(TFTVHours.getText())) { TV = Double.parseDouble(TFTVHours.getText().trim()); } else { TFTVHours.setText("Invalid Input - Must be a number."); } } if(evt.getActionCommand().equals("VG")) { if(isDoubleParsable(TFVG.getText())) { VG = Double.parseDouble(TFVG.getText().trim()); } else { TFVG.setText("Invalid Input - Must be a number."); } } if(evt.getActionCommand().equals("HW")) { if(isDoubleParsable(TFHomeWorkHours.getText())) { HWH = Double.parseDouble(TFHomeWorkHours.getText().trim()); } else { TFHomeWorkHours.setText("Invalid Input - Must be a number."); } } if(evt.getActionCommand().equals("Grades")) { Grades = TFGrades.getText(); } if(evt.getActionCommand().equals("Save")) { Writer.println("Name: " + Name); Writer.println("Average Daily Homework Hours: " + HWH); Writer.println("Average Daily Grades: " + Grades ); Writer.println("Average Daily TV Hours: " + TV); Writer.println("Average Daily Videogame Hours: " + VG); } } public static void main() throws IOException { GUI gui = new GUI(); gui.setSize(300,300); gui.setVisible(true); file = new File("Results.txt"); Writer = new PrintStream(file); } public boolean isDoubleParsable(String num) throws NumberFormatException { try { Double.parseDouble(num); return true; } catch(NumberFormatException nfe) { return false; } } }
|
|
|
| Report Abuse |
|
|
|
| 14 Jun 2012 04:16 PM |
| I will take a look when I get back. I'll keep this page up so I don't forget. :D |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 14 Jun 2012 04:30 PM |
I'm already working on it. Just got done indenting so it's readable. This is why I hate reading big chunks of code on the Roblox forums.
---------- ~pwnedu46, wiki writer~ |
|
|
| Report Abuse |
|
|
|
| 14 Jun 2012 04:39 PM |
^
Yeah, forums aren't very friendly towards indentation. =( |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 14 Jun 2012 04:44 PM |
It's happening because the event is only getting fired when you click the button and when the text in "Daily TV hours" is being changed. I'm working on a solution.
I used
System.out.println(evt.getActionCommand());
to get the ActionCommand in the actionPerformed method.
---------- ~pwnedu46, wiki writer~ |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 14 Jun 2012 04:57 PM |
Since it was working when you click the save button, instead of trying to fix it, I just moved the code from different events to save it into the save button event. I'd suggest removing the setActionListener commands for all but the save button for size and efficiency. It works now.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; public class GUI extends JFrame implements ActionListener { String Name; String Grades; double TV = 0; double VG = 0; double HWH = 0; static File file; static PrintStream Writer; JLabel Title = new JLabel(" Williamson Statistics "); JLabel LName = new JLabel("Name: "); JLabel LTVHours = new JLabel("Daily TV Hours: "); JLabel LVG = new JLabel("Daily Videogame Hours: "); JLabel LHomeWorkHours = new JLabel("Daily Homework Hours: "); JLabel LGrades = new JLabel("Average Grade Level: "); JTextField TFName = new JTextField(20); JTextField TFTVHours = new JTextField(16); JTextField TFVG = new JTextField(12); JTextField TFHomeWorkHours = new JTextField(12); JTextField TFGrades = new JTextField(12); JButton VerificationButton = new JButton(" Verify "); public GUI() { getContentPane().setLayout(new FlowLayout()); getContentPane().add(Title); getContentPane().add(LName); getContentPane().add(TFName); getContentPane().add(LTVHours); getContentPane().add(TFTVHours); getContentPane().add(LVG); getContentPane().add(TFVG); getContentPane().add(LGrades); getContentPane().add(TFGrades); getContentPane().add(VerificationButton); TFName.setEditable(true); TFTVHours.setEditable(true); TFVG.setEditable(true); TFHomeWorkHours.setEditable(true); TFGrades.setEditable(true); TFName.addActionListener(this); TFTVHours.addActionListener(this); TFVG.addActionListener(this); TFHomeWorkHours.addActionListener(this); TFGrades.addActionListener(this); VerificationButton.addActionListener(this); TFName.setActionCommand("Name"); TFTVHours.setActionCommand("TV"); TFVG.setActionCommand("VG"); TFHomeWorkHours.setActionCommand("HW"); TFGrades.setActionCommand("Grades"); VerificationButton.setActionCommand("Save"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent evt) { //System.out.println(evt.getActionCommand()); // used for debugging //when the save button is clicked: if(evt.getActionCommand().equals("Save")) { //set name Name = TFName.getText().trim(); //set tv hours if(isDoubleParsable(TFTVHours.getText())) { TV = Double.parseDouble(TFTVHours.getText().trim()); } else { TFTVHours.setText("Invalid Input - Must be a number."); } //set videogame time if(isDoubleParsable(TFVG.getText())) { VG = Double.parseDouble(TFVG.getText().trim()); } else { TFVG.setText("Invalid Input - Must be a number."); } //set grades Grades = TFGrades.getText(); Writer.println("Name: " + Name); Writer.println("Average Daily Homework Hours: " + HWH); Writer.println("Average Daily Grades: " + Grades ); Writer.println("Average Daily TV Hours: " + TV); Writer.println("Average Daily Videogame Hours: " + VG); } } public static void main(String[] args) throws IOException { GUI gui = new GUI(); gui.setSize(300,300); gui.setVisible(true); file = new File("Results.txt"); Writer = new PrintStream(file); } public boolean isDoubleParsable(String num) throws NumberFormatException { try { Double.parseDouble(num); return true; } catch(NumberFormatException nfe) { return false; } } }
---------- ~pwnedu46, wiki writer~ |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 14 Jun 2012 05:15 PM |
Wait. it still doesn't save Homework Hours.
---------- ~pwnedu46, wiki writer~ |
|
|
| Report Abuse |
|
|
|
| 14 Jun 2012 05:24 PM |
| I have ActionListeners on the TextFields so that the user knows when he typed bad input. |
|
|
| Report Abuse |
|
|
|
| 14 Jun 2012 05:45 PM |
| Why won't the variables assign to the values? I'm dearly confused. |
|
|
| Report Abuse |
|
|
|
| 14 Jun 2012 05:54 PM |
Figured out the problem; I wasn't pressing enter, XD
Here's the final code, with some adjustments in case the user doesn't like pressing return. =3
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*;
public class GUI extends JFrame implements ActionListener { String Name = "Null"; String Grades = "Null"; double TV = 0; double VG = 0; double HWH = 0; static File file; static PrintStream Writer; JLabel Title = new JLabel(" Williamson Statistics "); JLabel LName = new JLabel("Name: "); JLabel LTVHours = new JLabel("Daily TV Hours: "); JLabel LVG = new JLabel("Daily Videogame Hours: "); JLabel LHomeWorkHours = new JLabel("Daily Homework Hours: "); JLabel LGrades = new JLabel("Average Grade Level: "); JTextField TFName = new JTextField(20); JTextField TFTVHours = new JTextField(16); JTextField TFVG = new JTextField(12); JTextField TFHomeWorkHours = new JTextField(12); JTextField TFGrades = new JTextField(12); JButton VerificationButton = new JButton(" Verify "); public GUI() { getContentPane().setLayout(new FlowLayout()); getContentPane().add(Title); getContentPane().add(LName); getContentPane().add(TFName); getContentPane().add(LTVHours); getContentPane().add(TFTVHours); getContentPane().add(LVG); getContentPane().add(TFVG); getContentPane().add(LGrades); getContentPane().add(TFGrades); getContentPane().add(VerificationButton); TFName.setEditable(true); TFTVHours.setEditable(true); TFVG.setEditable(true); TFHomeWorkHours.setEditable(true); TFGrades.setEditable(true); TFName.addActionListener(this); TFTVHours.addActionListener(this); TFVG.addActionListener(this); TFHomeWorkHours.addActionListener(this); TFGrades.addActionListener(this); VerificationButton.addActionListener(this); TFName.setActionCommand("Name"); TFTVHours.setActionCommand("TV"); TFVG.setActionCommand("VG"); TFHomeWorkHours.setActionCommand("HW"); TFGrades.setActionCommand("Grades"); VerificationButton.setActionCommand("Save"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); }
public void actionPerformed(ActionEvent evt) { if(evt.getActionCommand().equals("Name")) { Name = TFName.getText().trim(); } if(evt.getActionCommand().equals("TV")) { if(isDoubleParsable(TFTVHours.getText())) { TV = Double.parseDouble(TFTVHours.getText().trim()); } else { TFTVHours.setText("Invalid Input - Must be a number."); } } if(evt.getActionCommand().equals("VG")) { if(isDoubleParsable(TFVG.getText())) { VG = Double.parseDouble(TFVG.getText().trim()); } else { TFVG.setText("Invalid Input - Must be a number."); } } if(evt.getActionCommand().equals("HW")) { if(isDoubleParsable(TFHomeWorkHours.getText())) { HWH = Double.parseDouble(TFHomeWorkHours.getText().trim()); } else { TFHomeWorkHours.setText("Invalid Input - Must be a number."); } } if(evt.getActionCommand().equals("Grades")) { Grades = TFGrades.getText(); } if(evt.getActionCommand().equals("Save")) { if( Name.equals("Null")) { Name = TFName.getText(); } if( Grades.equals("Null")) { Grades = TFGrades.getText(); } if( TV == 0) { if(isDoubleParsable(TFTVHours.getText())) { TV = Double.parseDouble(TFTVHours.getText().trim()); } else { TFTVHours.setText("Invalid Input - Must be a number."); } } if( VG == 0) { if(isDoubleParsable(TFVG.getText())) { VG = Double.parseDouble(TFVG.getText().trim()); } else { TFVG.setText("Invalid Input - Must be a number."); } } if(HWH == 0) { if(isDoubleParsable(TFHomeWorkHours.getText())) { HWH = Double.parseDouble(TFHomeWorkHours.getText().trim()); } else { TFHomeWorkHours.setText("Invalid Input - Must be a number."); } } Writer.println("Name: " + Name); Writer.println("Average Daily Homework Hours: " + HWH); Writer.println("Average Daily Grades: " + Grades ); Writer.println("Average Daily TV Hours: " + TV); Writer.println("Average Daily Videogame Hours: " + VG); String Name = "Null"; String Grades = "Null"; double TV = 0; double VG = 0; double HWH = 0; } }
public static void main() throws IOException { GUI gui = new GUI(); gui.setSize(300,300); gui.setVisible(true); file = new File("Results.txt"); Writer = new PrintStream(file); }
public boolean isDoubleParsable(String num) throws NumberFormatException { try { Double.parseDouble(num); return true; } catch(NumberFormatException nfe) { return false; } } }
|
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
| |
|
|
| 14 Jun 2012 07:27 PM |
One word: Applets
Anyways, I found an error in that code, and this is the final copy as it actually works.
// Results accomplished via this Graphical User Interface programmed in Java.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*;
public class GUI extends JFrame implements ActionListener { String Name = "Null"; String Grades = "Null"; double TV = 0; double VG = 0; double HWH = 0; String PastName = "Null"; String PastGrades = "Null"; double PastTV = 0; double PastVG = 0; double PastHWH = 0; static File file; static PrintStream Writer; JLabel Title = new JLabel(" Williamson Statistics "); JLabel LName = new JLabel("Name: "); JLabel LTVHours = new JLabel("Daily TV Hours: "); JLabel LVG = new JLabel("Daily Videogame Hours: "); JLabel LHomeWorkHours = new JLabel("Daily Homework Hours: "); JLabel LGrades = new JLabel("Average Grade Level: "); JTextField TFName = new JTextField(20); JTextField TFTVHours = new JTextField(16); JTextField TFVG = new JTextField(12); JTextField TFHomeWorkHours = new JTextField(12); JTextField TFGrades = new JTextField(12); JButton VerificationButton = new JButton(" Verify "); public GUI() { getContentPane().setLayout(new FlowLayout()); getContentPane().add(Title); getContentPane().add(LName); getContentPane().add(TFName); getContentPane().add(LHomeWorkHours); getContentPane().add(TFHomeWorkHours); getContentPane().add(LTVHours); getContentPane().add(TFTVHours); getContentPane().add(LVG); getContentPane().add(TFVG); getContentPane().add(LGrades); getContentPane().add(TFGrades); getContentPane().add(VerificationButton); TFName.setEditable(true); TFTVHours.setEditable(true); TFVG.setEditable(true); TFHomeWorkHours.setEditable(true); TFGrades.setEditable(true); TFName.addActionListener(this); TFTVHours.addActionListener(this); TFVG.addActionListener(this); TFHomeWorkHours.addActionListener(this); TFGrades.addActionListener(this); VerificationButton.addActionListener(this); TFName.setActionCommand("Name"); TFTVHours.setActionCommand("TV"); TFVG.setActionCommand("VG"); TFHomeWorkHours.setActionCommand("HW"); TFGrades.setActionCommand("Grades"); VerificationButton.setActionCommand("Save"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); }
public void actionPerformed(ActionEvent evt) { if(evt.getActionCommand().equals("Name")) { Name = TFName.getText().trim(); } if(evt.getActionCommand().equals("TV")) { if(isDoubleParsable(TFTVHours.getText())) { TV = Double.parseDouble(TFTVHours.getText().trim()); } else { TFTVHours.setText("Invalid Input - Must be a number."); } } if(evt.getActionCommand().equals("VG")) { if(isDoubleParsable(TFVG.getText())) { VG = Double.parseDouble(TFVG.getText().trim()); } else { TFVG.setText("Invalid Input - Must be a number."); } } if(evt.getActionCommand().equals("HW")) { if(isDoubleParsable(TFHomeWorkHours.getText())) { HWH = Double.parseDouble(TFHomeWorkHours.getText().trim()); } else { TFHomeWorkHours.setText("Invalid Input - Must be a number."); } } if(evt.getActionCommand().equals("Grades")) { Grades = TFGrades.getText(); } if(evt.getActionCommand().equals("Save")) { if( Name.equals(PastName)) { Name = TFName.getText(); } if( Grades.equals(PastGrades)) { Grades = TFGrades.getText(); } if( TV == PastTV) { if(isDoubleParsable(TFTVHours.getText())) { TV = Double.parseDouble(TFTVHours.getText().trim()); } else { TFTVHours.setText("Invalid Input - Must be a number."); } } if( VG == PastVG) { if(isDoubleParsable(TFVG.getText())) { VG = Double.parseDouble(TFVG.getText().trim()); } else { TFVG.setText("Invalid Input - Must be a number."); } } if(HWH == PastHWH) { if(isDoubleParsable(TFHomeWorkHours.getText())) { HWH = Double.parseDouble(TFHomeWorkHours.getText().trim()); } else { TFHomeWorkHours.setText("Invalid Input - Must be a number."); } } PastName = Name; PastHWH = HWH; PastGrades = Grades; PastTV = TV; PastVG = VG; Writer.println("Name: " + Name); Writer.println("Average Daily Homework Hours: " + HWH); Writer.println("Average Daily Grades: " + Grades ); Writer.println("Average Daily TV Hours: " + TV); Writer.println("Average Daily Videogame Hours: " + VG); Writer.println(" "); } }
public static void main() throws IOException { GUI gui = new GUI(); gui.setSize(300,300); gui.setVisible(true); file = new File("Results.txt"); Writer = new PrintStream(file); }
public boolean isDoubleParsable(String num) throws NumberFormatException { try { Double.parseDouble(num); return true; } catch(NumberFormatException nfe) { return false; } } }
|
|
|
| Report Abuse |
|
|
|
| 14 Jun 2012 07:51 PM |
| Oh man, if that's Java I'm gonna get wrecked next year in computer programming class. |
|
|
| Report Abuse |
|
|
|
| 14 Jun 2012 08:34 PM |
Java isn't hard at all. My first semester was purely about looping, conditionals, and arrays. Along with using the console to format output, plus some utilities like Scanner and ArrayList. That's it. We didn't even work on GUIs first semester of the course.
And GUIs are SUPER EASY! :D |
|
|
| Report Abuse |
|
|
|
| 15 Jun 2012 03:49 PM |
| Hmm... I wonder how well I can program an applet, >=3 |
|
|
| Report Abuse |
|
|