I was wondering if it is in JAVA to write the calculated data to a text file. My JAVA code is a GUI based gpa calculator. I just want to add a JButton & ActionListener that will write the Class Name, GPA Points, and calculated GPA to a .txt file.
Here is my JFrame Driver Code:
import javax.swing.JFrame;
public class Driver00
{
public static void main(String[] args)
{
/*
* Create a frame (outside box) and write what text
* will be displayed as the frame title
*/
JFrame frame = new JFrame("PHILIP MCQUITTY");
// give frame a size
frame.setSize(520, 375);
// set location on the computer screen will frame appear
frame.setLocation(400, 166);
// use this so when you press X in corner, frame will close
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add your panel to the frame. name must match Panel class name
frame.setContentPane(new GpaCalc());
// frame.setResizable(false);
// always include
frame.setVisible(true);
}
}
Here is my JPanel Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GpaCalc extends JPanel {
private JLabel GPALabel, c1, c2, c3, c4, c5, c6, c7, g1, g2, g3, g4, g5, g6, g7;
private JTextField Class1, Class2, Class3, Class4, Class5, Class6, Class7, Grade1, Grade2, Grade3, Grade4, Grade5, Grade6, Grade7;
private double GPA1, GPA2, GPA3, GPA4, GPA5, GPA6, GPA7, GPA, BigDec;
public GpaCalc() {
setLayout(new FlowLayout());
// Class Labels
GPALabel = new JLabel("0.00000000000000");
GPALabel.setFont(new Font("Times New Roman", Font.BOLD, 60));
GPALabel.setForeground(Color.red);
c1 = new JLabel("Block 1");
c1.setFont(new Font("Times New Roman", Font.BOLD, 20));
c1.setForeground(Color.black);
c2 = new JLabel("Block 2");
c2.setFont(new Font("Times New Roman", Font.BOLD, 20));
c2.setForeground(Color.black);
c3 = new JLabel("Block 3");
c3.setFont(new Font("Times New Roman", Font.BOLD, 20));
c3.setForeground(Color.black);
c4 = new JLabel("Block 4");
c4.setFont(new Font("Times New Roman", Font.BOLD, 20));
c4.setForeground(Color.black);
c5 = new JLabel("Block 5");
c5.setFont(new Font("Times New Roman", Font.BOLD, 20));
c5.setForeground(Color.black);
c6 = new JLabel("Block 6");
c6.setFont(new Font("Times New Roman", Font.BOLD, 20));
c6.setForeground(Color.black);
c7 = new JLabel("Block 7");
c7.setFont(new Font("Times New Roman", Font.BOLD, 20));
c7.setForeground(Color.black);
// Grade Labels
g1 = new JLabel("GPA Points");
g1.setFont(new Font("Times New Roman", Font.BOLD, 20));
g1.setForeground(Color.black);
g2 = new JLabel("GPA Points");
g2.setFont(new Font("Times New Roman", Font.BOLD, 20));
g2.setForeground(Color.black);
g3 = new JLabel("GPA Points");
g3.setFont(new Font("Times New Roman", Font.BOLD, 20));
g3.setForeground(Color.black);
g4 = new JLabel("GPA Points");
g4.setFont(new Font("Times New Roman", Font.BOLD, 20));
g4.setForeground(Color.black);
g5 = new JLabel("GPA Points");
g5.setFont(new Font("Times New Roman", Font.BOLD, 20));
g5.setForeground(Color.black);
g6 = new JLabel("GPA Points");
g6.setFont(new Font("Times New Roman", Font.BOLD, 20));
g6.setForeground(Color.black);
g7 = new JLabel("GPA Points");
g7.setFont(new Font("Times New Roman", Font.BOLD, 20));
g7.setForeground(Color.black);
// Class Textfields
Class1 = new JTextField("Enter Class Name", 10);
Class1.setHorizontalAlignment(SwingConstants.CENTER);
Class2 = new JTextField("Enter Class Name", 10);
Class2.setHorizontalAlignment(SwingConstants.CENTER);
Class3 = new JTextField("Enter Class Name", 10);
Class3.setHorizontalAlignment(SwingConstants.CENTER);
Class4 = new JTextField("Enter Class Name", 10);
Class4.setHorizontalAlignment(SwingConstants.CENTER);
Class5 = new JTextField("Enter Class Name", 10);
Class5.setHorizontalAlignment(SwingConstants.CENTER);
Class6 = new JTextField("Enter Class Name", 10);
Class6.setHorizontalAlignment(SwingConstants.CENTER);
Class7 = new JTextField("Enter Class Name", 10);
Class7.setHorizontalAlignment(SwingConstants.CENTER);
// Grade Textfields
Grade1 = new JTextField("0.0", 10);
Grade1.setHorizontalAlignment(SwingConstants.CENTER);
Grade2 = new JTextField("0.0", 10);
Grade2.setHorizontalAlignment(SwingConstants.CENTER);
Grade3 = new JTextField("0.0", 10);
Grade3.setHorizontalAlignment(SwingConstants.CENTER);
Grade4 = new JTextField("0.0", 10);
Grade4.setHorizontalAlignment(SwingConstants.CENTER);
Grade5 = new JTextField("0.0", 10);
Grade5.setHorizontalAlignment(SwingConstants.CENTER);
Grade6 = new JTextField("0.0", 10);
Grade6.setHorizontalAlignment(SwingConstants.CENTER);
Grade7 = new JTextField("0.0", 10);
Grade7.setHorizontalAlignment(SwingConstants.CENTER);
// Button(s)
JButton Calculate = new JButton("Calculate");
Calculate.addActionListener(new Listener());
JButton Reset = new JButton("Reset Fields");
Reset.addActionListener(new Listener2());
// Add(s)
add(GPALabel);
add(c1);
add(Class1);
add(Grade1);
add(g1);
add(c2);
add(Class2);
add(Grade2);
add(g2);
add(c3);
add(Class3);
add(Grade3);
add(g3);
add(c4);
add(Class4);
add(Grade4);
add(g4);
add(c5);
add(Class5);
add(Grade5);
add(g5);
add(c6);
add(Class6);
add(Grade6);
add(g6);
add(c7);
add(Class7);
add(Grade7);
add(g7);
add(Calculate);
add(Reset);
}
// Action Listener(s)
private class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
GPA1 = Double.parseDouble(Grade1.getText());
GPA2 = Double.parseDouble(Grade2.getText());
GPA3 = Double.parseDouble(Grade3.getText());
GPA4 = Double.parseDouble(Grade4.getText());
GPA5 = Double.parseDouble(Grade5.getText());
GPA6 = Double.parseDouble(Grade6.getText());
GPA7 = Double.parseDouble(Grade7.getText());
GPA = (GPA1 + GPA2 + GPA3 + GPA4 + GPA5 + GPA6 + GPA7) / 7;
GPALabel.setText("" + GPA);
}
}
private class Listener2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
Class1.setText("Enter Class Name");
Class2.setText("Enter Class Name");
Class3.setText("Enter Class Name");
Class4.setText("Enter Class Name");
Class5.setText("Enter Class Name");
Class6.setText("Enter Class Name");
Class7.setText("Enter Class Name");
Grade1.setText("0.0");
Grade2.setText("0.0");
Grade3.setText("0.0");
Grade4.setText("0.0");
Grade5.setText("0.0");
Grade6.setText("0.0");
Grade7.setText("0.0");
GPALabel.setText("0.00000000000000");
}
}
}
check java I/O api. Below is an example of writing data to file.
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("./output.txt"));
writer.write("your data here");
} catch (IOException e) {
System.err.println(e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
System.err.println(e);
}
}
}
PrintWriter pw = new PrintWriter(new FileWriter("c:\\output.txt");
pw.println("The value is: " + x);
pw.close();
import java.io.*;
class code{
public static void main(String args[]){
try{
File r = new File("C:\\hello.txt");
FileWriter pw = new FileWriter(r);
PrintWriter pr = new PrintWriter(pw);
pr.println("Hello world");
}catch(IOException e){}
}
}
Related
Where to enter the if condition in javax.swing?
I’m creating a registration form and want to add the following conditions to the form:
• Name: can not be less than 3 letters.
• Address1 and address2: Both addresses should not be the same.
• Age: not less than 18 years.
• Height: not less than 130.
• Weight: not less than 30
But I do not know where to enter the if condition.
package com.signupform;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame
extends JFrame
implements ActionListener {
private Container c;
private JLabel title;
private JLabel name;
private JTextField tname;
private JLabel age;
private JTextField tage;
private JLabel height;
private JTextField theight;
private JLabel weight;
private JTextField tweight;
private JLabel address1;
private JTextField taddress1;
private JLabel add2;
private JTextField tadd2;
private JButton sub;
private JTextArea tout;
public MyFrame()
{
setTitle("Registration Form");
setBounds(300, 90, 900, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
getContentPane().setBackground(Color.lightGray);
c = getContentPane();
c.setLayout(null);
title = new JLabel("Registration Form");
title.setFont(new Font("Arial", Font.PLAIN, 30));
title.setBounds(300,30,300,30);
c.add(title);
name = new JLabel("Name");
name.setFont(new Font("Arial", Font.PLAIN, 20));
name.setBounds(50,100,100,20);
c.add(name);
tname = new JTextField();
tname.setFont(new Font("Arial", Font.PLAIN, 15));
tname.setBounds(150,100,190,20);
c.add(tname);
age = new JLabel("Age");
age.setFont(new Font("Arial", Font.PLAIN, 20));
age.setBounds(50,150,100,20);
c.add(age);
tage = new JTextField();
tage.setFont(new Font("Arial", Font.PLAIN, 15));
tage.setBounds(150,150,60,20);
c.add(tage);
height = new JLabel("Height");
height.setFont(new Font("Arial", Font.PLAIN, 20));
height.setBounds(50,200,100,20);
c.add(height);
theight = new JTextField();
theight.setFont(new Font("Arial", Font.PLAIN, 15));
theight.setBounds(150,200,60,20);
c.add(theight);
weight = new JLabel("Weight");
weight.setFont(new Font("Arial", Font.PLAIN, 20));
weight.setBounds(50,250,100,20);
c.add(weight);
tweight = new JTextField();
tweight.setFont(new Font("Arial", Font.PLAIN, 15));
tweight.setBounds(150,250,60,20);
c.add(tweight);
address1 = new JLabel("Address 1");
address1.setFont(new Font("Arial", Font.PLAIN, 20));
address1.setBounds(50,300,100,20);
c.add(address1);
taddress1 = new JTextField();
taddress1.setFont(new Font("Arial", Font.PLAIN, 15));
taddress1.setBounds(150,300,450,30);
c.add(taddress1);
add2 = new JLabel("Address 2");
add2.setFont(new Font("Arial", Font.PLAIN, 20));
add2.setBounds(50,350,100,20);
c.add(add2);
tadd2 = new JTextField();
tadd2.setFont(new Font("Arial", Font.PLAIN, 15));
tadd2.setBounds(150,350,450,30);
c.add(tadd2);
sub = new JButton("Submit");
sub.setFont(new Font("Arial", Font.PLAIN, 15));
sub.setBounds(150,420,100,30);
sub.addActionListener(this);
c.add(sub);
tout = new JTextArea();
tout.setFont(new Font("Arial", Font.PLAIN, 15));
tout.setBounds(500,80,300,200);
tout.setLineWrap(true);
tout.setEditable(false);
c.add(tout);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sub) {
String data1 = "Name : " + tname.getText() + "\n"
+ "Age : " + tage.getText() + "\n";
String data2 = "Height : " + theight.getText() + "\n"
+ "Weight : " + tweight.getText() + "\n";
String data3 = "Address 1 : " + taddress1.getText() + "\n"
+ "Address 2 : " + tadd2.getText();
tout.setText(data1 + data2 + data3 );
tout.setEditable(false);
}
}
}
class Registration {
public static void main(String[] args) throws Exception
{
MyFrame f = new MyFrame();
}
}
If statements can be placed inside actionPerformed() method.
In your case You can do,
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sub) {
if(Integer.parseInt(tname.getText().length()) > 3 &&
address1.getText() != address2.getText())
}
}
and goes...
I need to build a GUI that will allow users to enter basic details about themselves for a new patient form and have that information saved to a .txt file. This data should then be viewable at a later time, but I can't get the save button to work.
//Import packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
//Main class
public class PatientDetailsWindow{
//Declare variables
public JFrame frame1;
public JPanel panel;
public JButton btnSave, btnExit;
public JLabel lblFirstName, lblSurname, lblGender, lblDOB, lblSmoker, lblMedHistory, lblFSlash1, lblFSlash2, lblImage;
public JTextField txtFirstName, txtSurname, txtGender, txtSmoker, txtMedHistory, txtDOB1, txtDOB2, txtDOB3;
public Insets insets;
public JTextArea textArea = new JTextArea();
public static void main (String args[]){
new PatientDetailsWindow();
}
public PatientDetailsWindow(){
createFrame();
createLabels();
createTextFields();
createButtons();
}
//Create the frame
public void createFrame(){
frame1 = new JFrame ("Personal details");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize (600,600);
panel = new JPanel();
panel.setBackground(Color.gray);
panel.setBounds(0, 0, 600, 600);
panel.setLayout(null);
frame1.add(panel);
frame1.setVisible(true);
}
//Creating Labels
public void createLabels(){
lblFirstName = new JLabel ("First Name: ");
lblFirstName.setBounds(50, 50, 500, 20);
lblFirstName.setForeground(Color.blue);
panel.add (lblFirstName);
lblSurname = new JLabel ("Surname: ");
lblSurname.setBounds(50, 100, 500, 20);
lblSurname.setForeground(Color.blue);
panel.add (lblSurname);
lblGender = new JLabel ("Gender: ");
lblGender.setBounds(50, 150, 500, 20);
lblGender.setForeground(Color.blue);
panel.add (lblGender);
lblDOB = new JLabel ("Date of Birth: ");
lblDOB.setBounds(50, 200, 500, 20);
lblDOB.setForeground(Color.blue);
panel.add (lblDOB);
lblFSlash1 = new JLabel ("/");
lblFSlash1.setBounds(440, 200, 20, 20);
lblFSlash1.setForeground(Color.blue);
panel.add (lblFSlash1);
lblFSlash2 = new JLabel ("/");
lblFSlash2.setBounds(490, 200, 40, 20);
lblFSlash2.setForeground(Color.blue);
panel.add (lblFSlash2);
lblSmoker = new JLabel ("Are you a smoker? ");
lblSmoker.setBounds(50, 250, 500, 20);
lblSmoker.setForeground(Color.blue);
panel.add (lblSmoker);
lblMedHistory = new JLabel ("Any other previous medical history? ");
lblMedHistory.setBounds(50, 300, 500, 20);
lblMedHistory.setForeground(Color.blue);
panel.add (lblMedHistory);
/*ImageIcon image= new ImageIcon("heartandstethoscope.jpg");
JLabel lblImage = new JLabel(image);
panel.add(lblImage);
*/
}
//Creating Text Fields
public void createTextFields(){
txtFirstName = new JTextField (10);
txtFirstName.setBounds(400, 50, 100, 20);
txtFirstName.setForeground(Color.blue);
panel.add (txtFirstName);
txtSurname = new JTextField (10);
txtSurname.setBounds(400, 100, 100, 20);
txtSurname.setForeground(Color.blue);
panel.add (txtSurname);
txtGender = new JTextField (10);
txtGender.setBounds(400, 150, 100, 20);
txtGender.setForeground(Color.blue);
panel.add (txtGender);
txtDOB1 = new JTextField (2);
txtDOB1.setBounds(400, 200, 40, 20);
txtDOB1.setForeground(Color.blue);
panel.add (txtDOB1);
txtDOB2 = new JTextField (2);
txtDOB2.setBounds(450, 200, 40, 20);
txtDOB2.setForeground(Color.blue);
panel.add (txtDOB2);
txtDOB3 = new JTextField (4);
txtDOB3.setBounds(500, 200, 80, 20);
txtDOB3.setForeground(Color.blue);
panel.add (txtDOB3);
txtSmoker = new JTextField (3);
txtSmoker.setBounds(400, 250, 100, 20);
txtSmoker.setForeground(Color.blue);
panel.add (txtSmoker);
txtMedHistory = new JTextField (300);
txtMedHistory.setBounds(400, 300, 100, 60);
txtMedHistory.setForeground(Color.blue);
panel.add (txtMedHistory);
JScrollPane areaScrollPane = new JScrollPane(txtMedHistory);
areaScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
textArea.setBounds(400, 300, 100, 80);
JScrollPane scroll = new JScrollPane (textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
textArea.setLineWrap(true);
textArea.setEditable(true);
textArea.setVisible(true);
panel.add(textArea);
}
//Creating buttons
public void createButtons(){
btnSave = new JButton ("Save");
btnSave.setBounds(130, 350, 100, 20);
btnSave.setForeground(Color.blue);
btnSave.addActionListener(new SaveHandler());
panel.add (btnSave);
btnSave.setVisible(true);
btnExit = new JButton ("Exit");
btnExit.setBounds(240, 350, 100, 20);
btnExit.setForeground(Color.blue);
btnExit.addActionListener(new ExitHandler());
panel.add (btnExit);
btnExit.setVisible(true);
}
class ExitHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
int n = JOptionPane.showConfirmDialog(frame1,
"Are you sure you want to exit?", "Exit?",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION){
System.exit(0);
System.out.println("EXIT SUCCESSFUL");
}
}
}
class SaveHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
try( PrintWriter out = new PrintWriter( "DetailsofPatient.txt" )){
out.println(txtFirstName.getText());
out.println(txtGender.getText());
out.println(txtDOB1.getText());
out.println(txtDOB2.getText());
out.println(txtDOB3.getText());
out.println(txtSmoker.getText());
out.println(txtMedHistory.getText());
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
System.out.println("Save successful");
}
}
}
Alright for starters, you didn't add an ActionListener to your save button, so you need to add it like this.
btnSave = new JButton ("Save");
btnSave.setBounds(130, 350, 100, 20);
btnSave.setForeground(Color.blue);
btnSave.addActionListener(new SaveHandler());
panel.add (btnSave);
btnSave.setVisible(true);
Also, in your SaveHandler, your PrintWriter isn't actually printing anything.
Your Code: out.println( ); would not write anything.
Let's say you wanted to write the contents of txtFirstName, you would need to do
out.println(txtFirstName.getText());
Edit: You also need another closing braces at the end of your ExitHandler class
class ExitHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
int n = JOptionPane.showConfirmDialog(frame1,
"Are you sure you want to exit?", "Exit?",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION){
System.exit(0);
System.out.println("EXIT SUCCESSFUL");
}
}
}
You will also then need to remove a closing brace at the end of the program.
import java.awt.*;
public class MainWindow {
private JFrame frmMainwindow;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
private JTextField textField_7;
private JTextField textField_8;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frmMainwindow.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*
*/
Connection conn = null;
Connection conn1 = null;
public MainWindow() {
initialize();
MainPage mp = new MainPage();
mp.setVisible(true);
}
/**
* Initialize the contents of the frame.
*/
public void Reset(){
textField.setText(null);
textField_1.setText(null);
textField_2.setText(null);
textField_3.setText(null);
textField_4.setText(null);
textField_5.setText(null);
textField_6.setText(null);
textField_7.setText(null);
textField_8.setText(null);
}
public void DBCreation(){
conn = CreatingDb.CreateDb();
String DBName = textField.getText();
try{
String query = "CREATE DATABASE " + DBName ;
PreparedStatement pst = conn.prepareStatement(query);
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "DB Created Successful.....");
pst.close();
}catch(Exception R){
JOptionPane.showMessageDialog(null, R);
}
}
public void Insert(){
String DBName = textField.getText();
conn1 = SqlConnection.InsertDB(DBName);
try{
String Table1 = "CREATE TABLE PERSONALINFO (PersonName VARCHAR(25),DoorNO VARCHAR(10),Street VARCHAR(25),Village VARCHAR(25),PhoneNo LONGINT(10),UserName VARCHAR(15),Password VARCHAR(10),Hint VARCHAR(50))";
String Insert1 = "INSERT INTO PERSONALINFO (PersonName,DoorNO,Street,Village,PhoneNo,UserName,Password,Hint) VALUES (?,?,?,?,?,?,?,?)";
PreparedStatement pstt = conn.prepareStatement(Table1);
PreparedStatement psti = conn.prepareStatement(Insert1);
psti.setString(1, textField_1.getText());
psti.setString(2, textField_2.getText());
psti.setString(3, textField_3.getText());
psti.setString(4, textField_4.getText());
psti.setString(5, textField_5.getText());
psti.setString(6, textField_6.getText());
psti.setString(7, textField_7.getText());
psti.setString(8, textField_8.getText());
pstt.execute();
psti.execute();
JOptionPane.showMessageDialog(null, "Table Created and Data Inserted Successfully....");
psti.close();
pstt.close();
}catch(Exception R){
JOptionPane.showMessageDialog(null, R);
}
}
private void initialize() {
frmMainwindow = new JFrame();
frmMainwindow.getContentPane().setBackground(Color.WHITE);
frmMainwindow.getContentPane().setFont(new Font("Times New Roman", Font.BOLD, 14));
frmMainwindow.setTitle("Create DataBase");
frmMainwindow.setBounds(100, 100, 668, 416);
frmMainwindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int height = screenSize.height;
int width = screenSize.width;
frmMainwindow.setSize(width/2, height/2);
// center the jframe on screen
frmMainwindow.setLocationRelativeTo(null);
frmMainwindow.getContentPane().setLayout(null);
JPanel panel_1 = new JPanel();
panel_1.setBackground(Color.WHITE);
panel_1.setBounds(120, 70, 426, 205);
frmMainwindow.getContentPane().add(panel_1);
panel_1.setLayout(null);
panel_1.setVisible(false);
JLabel lblDatabaseName = new JLabel("DataBase Name");
lblDatabaseName.setBounds(0, 3, 107, 14);
panel_1.add(lblDatabaseName);
lblDatabaseName.setFont(new Font("Times New Roman", Font.BOLD, 14));
JLabel lblPersonName = new JLabel("Person Name");
lblPersonName.setBounds(0, 32, 89, 14);
panel_1.add(lblPersonName);
lblPersonName.setFont(new Font("Times New Roman", Font.BOLD, 14));
JLabel lblDoorNo = new JLabel("Door No");
lblDoorNo.setBounds(0, 57, 79, 14);
panel_1.add(lblDoorNo);
lblDoorNo.setFont(new Font("Times New Roman", Font.BOLD, 14));
JLabel lblStreet = new JLabel("Street");
lblStreet.setBounds(0, 85, 46, 14);
panel_1.add(lblStreet);
lblStreet.setFont(new Font("Times New Roman", Font.BOLD, 14));
JLabel lblVillage = new JLabel("Village");
lblVillage.setBounds(0, 110, 46, 14);
panel_1.add(lblVillage);
lblVillage.setFont(new Font("Times New Roman", Font.BOLD, 14));
JLabel lblPhoneNo = new JLabel("Phone No");
lblPhoneNo.setBounds(0, 135, 58, 14);
panel_1.add(lblPhoneNo);
lblPhoneNo.setFont(new Font("Times New Roman", Font.BOLD, 14));
JLabel lblUserbame = new JLabel("UserName");
lblUserbame.setBounds(232, 3, 79, 14);
panel_1.add(lblUserbame);
lblUserbame.setFont(new Font("Times New Roman", Font.BOLD, 14));
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(232, 32, 68, 14);
panel_1.add(lblPassword);
lblPassword.setFont(new Font("Times New Roman", Font.BOLD, 14));
JLabel lblHint = new JLabel("Hint");
lblHint.setBounds(232, 57, 46, 14);
panel_1.add(lblHint);
lblHint.setFont(new Font("Times New Roman", Font.BOLD, 14));
textField = new JTextField();
textField.setBounds(117, 0, 105, 20);
panel_1.add(textField);
textField.setFont(new Font("Times New Roman", Font.BOLD, 14));
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(117, 29, 105, 20);
panel_1.add(textField_1);
textField_1.setFont(new Font("Times New Roman", Font.BOLD, 14));
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(117, 54, 105, 20);
panel_1.add(textField_2);
textField_2.setFont(new Font("Times New Roman", Font.BOLD, 14));
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(117, 82, 105, 20);
panel_1.add(textField_3);
textField_3.setFont(new Font("Times New Roman", Font.BOLD, 14));
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(117, 107, 105, 20);
panel_1.add(textField_4);
textField_4.setFont(new Font("Times New Roman", Font.BOLD, 14));
textField_4.setColumns(10);
textField_5 = new JTextField();
textField_5.setBounds(117, 132, 105, 20);
panel_1.add(textField_5);
textField_5.setFont(new Font("Times New Roman", Font.BOLD, 14));
textField_5.setColumns(10);
textField_6 = new JTextField();
textField_6.setBounds(321, 0, 105, 20);
panel_1.add(textField_6);
textField_6.setFont(new Font("Times New Roman", Font.BOLD, 14));
textField_6.setColumns(10);
textField_7 = new JTextField();
textField_7.setBounds(321, 29, 105, 20);
panel_1.add(textField_7);
textField_7.setFont(new Font("Times New Roman", Font.BOLD, 14));
textField_7.setColumns(10);
textField_8 = new JTextField();
textField_8.setBounds(321, 54, 105, 20);
panel_1.add(textField_8);
textField_8.setFont(new Font("Times New Roman", Font.BOLD, 14));
textField_8.setColumns(10);
JButton btnReset = new JButton("Reset");
btnReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Reset();
}
});
btnReset.setBounds(232, 171, 89, 23);
panel_1.add(btnReset);
JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DBCreation();
Insert();
Reset();
}
});
btnSave.setBounds(327, 171, 89, 23);
panel_1.add(btnSave);
panel_1.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{lblDatabaseName, textField_1, lblPersonName, lblDoorNo, lblStreet, lblVillage, lblPhoneNo, lblUserbame, lblPassword, lblHint, textField_2, textField_3, textField_4, textField_5, textField_6, textField_7, textField_8, btnSave, btnReset, textField}));
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setBounds(217, 122, 233, 101);
frmMainwindow.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblCreateDatabase = new JLabel("Create DataBase");
lblCreateDatabase.setBounds(0, 4, 120, 14);
panel.add(lblCreateDatabase);
lblCreateDatabase.setFont(new Font("Times New Roman", Font.BOLD, 14));
JLabel lblOpenDatabase = new JLabel("Open DataBase");
lblOpenDatabase.setBounds(0, 42, 120, 14);
panel.add(lblOpenDatabase);
lblOpenDatabase.setFont(new Font("Times New Roman", Font.BOLD, 14));
JLabel lblNewLabel = new JLabel("Delete DataBase");
lblNewLabel.setBounds(0, 82, 120, 14);
panel.add(lblNewLabel);
lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
JButton btnNew = new JButton("New");
btnNew.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel.setVisible(false);
panel_1.setVisible(true);
}
});
btnNew.setBounds(144, 0, 89, 23);
panel.add(btnNew);
btnNew.setFont(new Font("Times New Roman", Font.BOLD, 14));
JButton btnSelect = new JButton("Select");
btnSelect.setBounds(144, 38, 89, 23);
panel.add(btnSelect);
btnSelect.setFont(new Font("Times New Roman", Font.BOLD, 14));
JButton btnRemove = new JButton("Remove");
btnRemove.setBounds(144, 78, 89, 23);
panel.add(btnRemove);
btnRemove.setFont(new Font("Times New Roman", Font.BOLD, 14));
}
}
My Second java file
public class SqlConnection {
Connection conn1 = null;
public static Connection InsertDB(String DBName){
String value = DBName;
try{
Class.forName("com.mysql.jdbc.Driver");
String DB = "jdbc:mysql://localhost:3306/";
Connection conn = DriverManager.getConnection(DB+value,"root","");
JOptionPane.showMessageDialog(null,value);
return conn;
}catch(Exception e){
JOptionPane.showMessageDialog(null,e);
return null;
}
}
}
MY Third java File
public class CreatingDb {
Connection conn = null;
public static Connection CreateDb(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/","root","");
return conn;
}catch(Exception e){
JOptionPane.showMessageDialog(null,e);
return null;
}
}
}
MY Forth java File..
public class MainPage extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainPage frame = new MainPage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainPage() {
setExtendedState(Frame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmNew = new JMenuItem("New");
mnFile.add(mntmNew);
JMenuItem mntmOpen = new JMenuItem("Open");
mnFile.add(mntmOpen);
JMenuItem mntmSave = new JMenuItem("Save");
mnFile.add(mntmSave);
JMenuItem mntmSaveAs = new JMenuItem("Save As...");
mnFile.add(mntmSaveAs);
JMenuItem mntmExit = new JMenuItem("Exit");
mnFile.add(mntmExit);
JMenu mnEdit = new JMenu("Edit");
menuBar.add(mnEdit);
JMenu mnHelp = new JMenu("Help");
menuBar.add(mnHelp);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}
I Have imported required packages...
In the above code I am creating database using textfield at beginning of application. DataBase is created successfully but i am unable to access the created dataBase . It show java.sql.Exception: Unknown database Selection.
please any one help me ....
String DB = "jdbc:mysql://localhost:3306/";
Connection conn = DriverManager.getConnection(DB+value,"root","");
i think your problem appending. you do not append DB + value. give password.
The MySQL manual says this:
If the database is not specified, the connection is made with no default database. In this case, either call the setCatalog() method on the Connection instance, or fully specify table names using the database name (that is, SELECT dbname.tablename.colname FROM dbname.tablename...) in your SQL. Opening a connection without specifying the database to use is generally only useful when building tools that work with multiple databases, such as GUI database managers.
Note
Always use the Connection.setCatalog() method to specify the desired database in JDBC applications, rather than the USE database statement.
It doesn't specify what happens if you don't call setCatalog() but given your problem I guess "nothing good." You should add a schema to your connect string or call setCatalog().
I have a half complete coding which basically comes down to a gui interface. The problem is that I don't know how to get it working as ticket vending machine with fixed rates and fixed amount of cash being able to put in.
So, far, I've done up to calculating total where nomatter what I do, it always comes up with a £0.00 as the totalamount answer.
I am in desperate need of help. This is very frustrating. Below is the code for the TicketCalculation Class
import java.awt.*;
import java.text.*;
import java.awt.event.*;
import javax.swing.*;
public class TicketCalculation extends JFrame implements ActionListener {
DecimalFormat pounds = new DecimalFormat("£#,##0.00");
//creating and naming buttons and textfields
private JButton twentypenceBtn = new JButton("20p");
private JButton fiftypenceBtn = new JButton("50p");
private JButton onepoundBtn = new JButton("£1");
private JButton twopoundsBtn = new JButton("£2");
private JButton fivepoundsBtn = new JButton("£5");
private JButton tenpoundsBtn = new JButton("£10");
private JButton twentypoundsBtn = new JButton("£20");
private JButton C = new JButton("Calculate");
private JButton frontBtn = new JButton("<<Front Stalls>>");
private JButton private1Btn = new JButton("<<Private Box>>");
private JButton middleBtn = new JButton("<<Middle Stalls>>");
private JButton backBtn = new JButton("<<Back Stalls>>");
private JButton calcBtn = new JButton("Calculate Bill");
private JTextField tickettypeTxt = new JTextField(14);
private JTextField stalltypeTxt = new JTextField(25);
private JTextField amountticketsTxt = new JTextField(14);
private JTextField totalamountTxt = new JTextField(10);
private JTextField amountdueTxt = new JTextField(13);
private JTextField amountpaidTxt = new JTextField(10);
//creating labels
private JLabel pickstall = new JLabel();
private JLabel tictype = new JLabel ();
private JLabel amontic = new JLabel();
private JLabel ttamon = new JLabel();
private JLabel amondue = new JLabel();
private JLabel amonpaid = new JLabel();
private JLabel label5 = new JLabel();
private JLabel spacing6 = new JLabel();
private JLabel spacing7 = new JLabel();
private JLabel spacing8 = new JLabel();
private JLabel spacing9 = new JLabel();
private JLabel spacing10 = new JLabel();
//image icon declarations
private ImageIcon paycount = new ImageIcon(getClass().getResource("paycount.jpg"));
double middleprice;
double privateprice;
double backprice;
double frontprice;
double type;
int number;
public static void main(String[] args){
new TicketCalculation();
}
public TicketCalculationt(){
setLayout(new BorderLayout());
setSize(650,750);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//naming labels
pickstall = new JLabel("==> Pick a Stall Type: ");
tictype = new JLabel ("==> Price of a Ticket: £");
amontic = new JLabel("==> Amount of Tickets: ");
ttamon = new JLabel("==> Total Amount: ");
amondue = new JLabel("==> Amount Due: £");
amonpaid = new JLabel("==> Amount Paid: £");
label5 = new JLabel(paycount);
spacing6 = new JLabel(" ");
spacing7 = new JLabel(" ");
spacing8 = new JLabel(" ");
spacing9 = new JLabel(" ");
spacing10 = new JLabel(" ");
//setting font for buttons, textfields and labels
pickstall.setFont(new Font("Rockwell", Font.BOLD, 20));
frontBtn.setFont(new Font("System", Font.BOLD, 22));
middleBtn.setFont(new Font("System", Font.BOLD, 22));
backBtn.setFont(new Font("System", Font.BOLD, 22));
private1Btn.setFont(new Font("System", Font.BOLD, 22));
tictype.setFont(new Font("Rockwell", Font.BOLD, 20));
amontic.setFont(new Font("Rockwell", Font.BOLD, 20));
ttamon.setFont(new Font("Rockwell", Font.BOLD, 20));
amondue.setFont(new Font("Rockwell", Font.BOLD, 20));
amonpaid.setFont(new Font("Rockwell", Font.BOLD, 20));
stalltypeTxt.setFont(new Font("Verdana", Font.BOLD, 20));
tickettypeTxt.setFont(new Font("Verdana", Font.BOLD, 20));
amountticketsTxt.setFont(new Font("Verdana", Font.BOLD, 20));
totalamountTxt.setFont(new Font("Verdana", Font.BOLD, 20));
amountdueTxt.setFont(new Font("Verdana", Font.BOLD, 20));
amountpaidTxt.setFont(new Font("Verdana", Font.BOLD, 20));
twentypenceBtn.setFont(new Font("Century Gothic", Font.BOLD, 28));
fiftypenceBtn.setFont(new Font("Century Gothic", Font.BOLD, 28));
onepoundBtn.setFont(new Font("Century Gothic", Font.BOLD, 28));
twopoundsBtn.setFont(new Font("Century Gothic", Font.BOLD, 28));
fivepoundsBtn.setFont(new Font("Century Gothic", Font.BOLD, 28));
tenpoundsBtn.setFont(new Font("Century Gothic", Font.BOLD, 28));
twentypoundsBtn.setFont(new Font("Century Gothic", Font.BOLD, 28));
C.setFont(new Font("Serif", Font.BOLD, 28));
stalltypeTxt.setEditable(false);
tickettypeTxt.setEditable(false);
totalamountTxt.setEditable(false);
amountdueTxt.setEditable(false);
amountpaidTxt.setEditable(false);
//positioning all buttons, textfields and labels
JPanel top = new JPanel();
top.add(label5);
add("North", top);
JPanel mid = new JPanel();
mid.add(pickstall);
mid.add(stalltypeTxt);
mid.add(spacing6);
mid.add(private1Btn);
mid.add(frontBtn);
mid.add(middleBtn);
mid.add(backBtn);
mid.add(tictype);
mid.add(tickettypeTxt);
mid.add(spacing7);
mid.add(amontic);
mid.add(amountticketsTxt);
mid.add(spacing8);
mid.add(ttamon);
mid.add(totalamountTxt);
mid.add(calcBtn);
mid.add(spacing10);
mid.add(amonpaid);
mid.add(amountpaidTxt);
mid.add(spacing9);
mid.add(twentypenceBtn);
mid.add(fiftypenceBtn);
mid.add(onepoundBtn);
mid.add(twopoundsBtn);
mid.add(fivepoundsBtn);
mid.add(tenpoundsBtn);
mid.add(twentypoundsBtn);
mid.add(amondue);
mid.add(amountdueTxt);
mid.add(C);
add("Center", mid);
calcBtn.addActionListener(this);
private1Btn.addActionListener(this);
frontBtn.addActionListener(this);
middleBtn.addActionListener(this);
backBtn.addActionListener(this);
twentypenceBtn.addActionListener(this);
fiftypenceBtn.addActionListener(this);
onepoundBtn.addActionListener(this);
twopoundsBtn.addActionListener(this);
fivepoundsBtn.addActionListener(this);
tenpoundsBtn.addActionListener(this);
twentypoundsBtn.addActionListener(this);
C.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
// event handler for the buttons
if (e.getSource() == private1Btn)
{
stalltypeTxt.setText("Private Stall Location is Chosen");
tickettypeTxt.setText("30.85");
}
else if (e.getSource() == frontBtn)
{
stalltypeTxt.setText("Front Stall Location is Chosen");
tickettypeTxt.setText("15.00");
}
else if (e.getSource() == middleBtn)
{
stalltypeTxt.setText("Middle Stall Location is Chosen");
tickettypeTxt.setText("10.20");
}
else if (e.getSource() == backBtn)
{
stalltypeTxt.setText("Back Stall Location is Chosen");
tickettypeTxt.setText("5.70");
}
else if (e.getSource() == tickettypeTxt)
{
type = Double.parseDouble(tickettypeTxt.getText());
}
else if (e.getSource() == amountticketsTxt)
{
number = Integer.parseInt(amountticketsTxt.getText());
}
else if (e.getSource() == calcBtn)
{
Workings c = new Workings();
w.setType(type);
w.setNumber(number);
double total = w.calculateBill();
totalamountTxt.setText(pounds.format(total));
}
}
}
with a halfway done auxiliary class called Workings
public class Workings {
// private instance variables
private double type, number;
// public no-argument constructor
public Workings() { }
// public constructor with four arguments
public Workings(double t, int n)
{
type = t;
number = n;
}
// public 'set' methods
public void setType(double t) { type = t; }
public void setNumber(int n) { number = n; }
// public method to calculate the bill
public double calculateBill()
{
double total = type * number ;
return total;
}
}
The ActionListener is only triggered on a JTextField when the user hits the Enter key on the keyboard while in that field. This is unintuitive, so I would say it's a big no-no to use ActionListener for that.
Instead, use a FocusListener to get the "user left the text field" events from the text fields. That should pick up the data entered and then it should work.
Here's your problem
else if (e.getSource() == tickettypeTxt)
{
type = Double.parseDouble(tickettypeTxt.getText());
}
else if (e.getSource() == amountticketsTxt)
{
number = Integer.parseInt(amountticketsTxt.getText());
}
else if (e.getSource() == calcBtn)
{
Workings c = new Workings();
w.setType(type);
w.setNumber(number);
double total = w.calculateBill();
totalamountTxt.setText(pounds.format(total));
}
Delete the first two else ifs and just do this
else if (e.getSource() == calcBtn)
{
type = Double.parseDouble(tickettypeTxt.getText());
number = Integer.parseInt(amountticketsTxt.getText());
Workings c = new Workings();
w.setType(type);
w.setNumber(number);
double total = w.calculateBill();
totalamountTxt.setText(pounds.format(total));
}
I have a problem regarding the JTextField, I always get a NullPointerException whenever i want to get the textfield value from my class Student and pass it to my other class which is class myAction what i want to happen is that after i clicked the button or Jbutton all the data in the textfield will be save to the array, and i already have an actionlistener to my button and create a class myAction.
Here are my code:
Student class(Main class)
/*
package Student;
import java.util.Scanner;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Student{
private static Scanner input = new Scanner(System.in);
public String[] name = {"Lara", "Jerome", "Ferdie", "Jeffrey", "Eduard", "King"};
public int[] grade = new int[6];
JFrame frame;
JLabel label1, label2, label3, label4, label5, label6;
JPanel panel;
public JTextField text1, text2, text3, text4, text5, text6;
JButton button;
public static void main(String[] args)
{
Student myStudent = new Student();
myStudent.Layout();
//myStudent.
}
public void GetValue()
{
grade[0] = Integer.parseInt(text1.getText());
grade[1] = Integer.parseInt(text2.getText());
grade[2] = Integer.parseInt(text3.getText());
grade[3] = Integer.parseInt(text4.getText());
grade[4] = Integer.parseInt(text5.getText());
grade[5] = Integer.parseInt(text6.getText());
for(int i = 0; i < grade.length; i++)
{
System.out.println(grade[i]);
}
}
public void Layout()
{
//Design Layout
panel = new JPanel();
panel.setLayout(null);
frame = new JFrame("Student Grades");
button = new JButton("Get Grade");
button.setBounds(50, 260, 200, 30);
label1 = new JLabel("Your Grade: " + name[0]);
label1.setBounds(10, 10, 150, 30);
text1 = new JTextField(5);
text1.setBounds(140, 15, 150, 20);
label2 = new JLabel("Your Grade: " + name[1]);
label2.setBounds(10, 50, 150, 30);
text2 = new JTextField(5);
text2.setBounds(140, 55, 150, 20);
label3 = new JLabel("Your Grade: " + name[2]);
label3.setBounds(10, 90, 150, 30);
text3 = new JTextField(5);
text3.setBounds(140, 95, 150, 20);
label4 = new JLabel("Your Grade: " + name[3]);
label4.setBounds(10, 130, 150, 30);
text4 = new JTextField(5);
text4.setBounds(140, 135, 150, 20);
label5 = new JLabel("Your Grade: " + name[4]);
label5.setBounds(10, 170, 150, 30);
text5 = new JTextField(5);
text5.setBounds(140, 175, 150, 20);
label6 = new JLabel("Your Grade: " + name[5]);
label6.setBounds(10,210, 150, 30);
text6 = new JTextField(5);
text6.setBounds(140, 215, 150, 20);
frame.setSize(350, 350);
frame.setVisible(true);
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(text4);
panel.add(label5);
panel.add(text5);
panel.add(label6);
panel.add(text6);
panel.add(button);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyEvent myAction = new MyEvent();
button.addActionListener(myAction);
}
}
and my class myAction(or actionlistener) class
class MyEvent implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Get Grade"))
{
Student EventStudent = new Student();
EventStudent.GetValue();
}
}
}
*/
here are all my code hope you can help me.
The problem in your code is you created a Student object in main.So your text fields are associated with that object.Now in your event listener you are creating another object of the student that doesn't belong with any of the component you need.you have to retrieve data from the object in the main method.
Do like below.Hopefully it will help.
import java.util.Scanner;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Student{
static Student myStudent ;
private static Scanner input = new Scanner(System.in);
public String[] name = {"Lara", "Jerome", "Ferdie", "Jeffrey", "Eduard", "King"};
public int[] grade = new int[6];
JFrame frame;
JLabel label1, label2, label3, label4, label5, label6;
JPanel panel;
public JTextField text1, text2, text3, text4, text5, text6;
JButton button;
public static void main(String[] args)
{
myStudent = new Student();
myStudent.Layout();
//myStudent.
}
public void GetValue()
{
grade[0] = Integer.parseInt(text1.getText());
grade[1] = Integer.parseInt(text2.getText());
grade[2] = Integer.parseInt(text3.getText());
grade[3] = Integer.parseInt(text4.getText());
grade[4] = Integer.parseInt(text5.getText());
grade[5] = Integer.parseInt(text6.getText());
for(int i = 0; i < grade.length; i++)
{
System.out.println(grade[i]);
}
}
public void Layout()
{
//Design Layout
panel = new JPanel();
panel.setLayout(null);
frame = new JFrame("Student Grades");
button = new JButton("Get Grade");
button.setBounds(50, 260, 200, 30);
label1 = new JLabel("Your Grade: " + name[0]);
label1.setBounds(10, 10, 150, 30);
text1 = new JTextField(5);
text1.setBounds(140, 15, 150, 20);
label2 = new JLabel("Your Grade: " + name[1]);
label2.setBounds(10, 50, 150, 30);
text2 = new JTextField(5);
text2.setBounds(140, 55, 150, 20);
label3 = new JLabel("Your Grade: " + name[2]);
label3.setBounds(10, 90, 150, 30);
text3 = new JTextField(5);
text3.setBounds(140, 95, 150, 20);
label4 = new JLabel("Your Grade: " + name[3]);
label4.setBounds(10, 130, 150, 30);
text4 = new JTextField(5);
text4.setBounds(140, 135, 150, 20);
label5 = new JLabel("Your Grade: " + name[4]);
label5.setBounds(10, 170, 150, 30);
text5 = new JTextField(5);
text5.setBounds(140, 175, 150, 20);
label6 = new JLabel("Your Grade: " + name[5]);
label6.setBounds(10,210, 150, 30);
text6 = new JTextField(5);
text6.setBounds(140, 215, 150, 20);
frame.setSize(350, 350);
frame.setVisible(true);
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(text4);
panel.add(label5);
panel.add(text5);
panel.add(label6);
panel.add(text6);
panel.add(button);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyEvent myAction = new MyEvent(myStudent);
button.addActionListener(myAction);
}
}
class MyEvent implements ActionListener
{
Student myStudent;
public MyEvent(Student myStudent) {
this.myStudent=myStudent;
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Get Grade"))
{
myStudent.GetValue();
}
}
}