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...
Related
So i'm creating a program that sort of acts like a calculator for a certain item for sales. I have a calculate button but pressing the button won't initialize the code within it. I am using Swing designer for the gui. Here's the code with the button
JButton btnNewButton = new JButton("Calculate Total");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 43));
btnNewButton.setBounds(37, 1113, 330, 77);
frame.getContentPane().add(btnNewButton);
if(btnNewButton.getModel().isPressed())
{
// Sets value for # of gloves per type
LA = Integer.parseInt(textField_1.getText());
LB = Integer.parseInt(textField_8.getText());
SA = Integer.parseInt(textField_12.getText());
SB = Integer.parseInt(textField_13.getText());
total = LA + LB + SA + SB;
// Sets value for percentage of gloves compared to total
percLA = LA / total;
percLB = LB / total;
percSA = SA / total;
percSB = SB / total;
double percTotal = percLA + percLB + percSA + percSB;
//21;
// Sets value for total # of gloves
textField_22.setText(Double.toString(total));
// Sets value for percentage
textField_2.setText(Double.toString(percLA));
textField_7.setText(Double.toString(percLB));
textField_14.setText(Double.toString(percSA));
textField_15.setText(Double.toString(percSB));
textField_21.setText(Double.toString(percTotal));
// Sets value for cost per pair
costLA = Integer.parseInt(textField_3.getText());
costLB = Integer.parseInt(textField_6.getText());
costSA = Integer.parseInt(textField_16.getText());
costSB = Integer.parseInt(textField_17.getText());
}
And here's the full code if needed
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.Box;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.JButton;
public class Program {
private JFrame frame;
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;
private JTextField textField_12;
private JTextField textField_13;
private JTextField textField_14;
private JTextField textField_15;
private JTextField textField_16;
private JTextField textField_17;
private JTextField textField_18;
private JTextField textField_19;
private JTextField textField_20;
private JTextField textField_21;
private JTextField textField_22;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Program window = new Program();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Program() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1109, 1400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
// Total # of pairs per glove
double LA = 0;
double LB = 0;
double SA = 0;
double SB = 0;
double total = 0;
// Percentage of gloves compared to total
double percLA = 0;
double percLB = 0;
double percSA = 0;
double percSB = 0;
// Cost per glove type
double costLA = 0;
double costLB = 0;
double costSA = 0;
double costSB = 0;
textField_1 = new JTextField();
textField_1.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_1.setColumns(10);
textField_1.setBounds(231, 74, 200, 64);
frame.getContentPane().add(textField_1);
textField_2 = new JTextField();
textField_2.setEditable(false);
textField_2.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_2.setColumns(10);
textField_2.setBounds(440, 74, 200, 64);
frame.getContentPane().add(textField_2);
textField_3 = new JTextField();
textField_3.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_3.setColumns(10);
textField_3.setBounds(650, 74, 200, 64);
frame.getContentPane().add(textField_3);
textField_4 = new JTextField();
textField_4.setEditable(false);
textField_4.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_4.setColumns(10);
textField_4.setBounds(860, 74, 200, 64);
frame.getContentPane().add(textField_4);
textField_5 = new JTextField();
textField_5.setEditable(false);
textField_5.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_5.setColumns(10);
textField_5.setBounds(860, 147, 200, 64);
frame.getContentPane().add(textField_5);
textField_6 = new JTextField();
textField_6.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_6.setColumns(10);
textField_6.setBounds(650, 147, 200, 64);
frame.getContentPane().add(textField_6);
textField_7 = new JTextField();
textField_7.setEditable(false);
textField_7.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_7.setColumns(10);
textField_7.setBounds(440, 147, 200, 64);
frame.getContentPane().add(textField_7);
textField_8 = new JTextField();
textField_8.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_8.setColumns(10);
textField_8.setBounds(231, 147, 200, 64);
frame.getContentPane().add(textField_8);
textField_12 = new JTextField();
textField_12.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_12.setColumns(10);
textField_12.setBounds(231, 219, 200, 64);
frame.getContentPane().add(textField_12);
textField_13 = new JTextField();
textField_13.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_13.setColumns(10);
textField_13.setBounds(231, 293, 200, 64);
frame.getContentPane().add(textField_13);
textField_14 = new JTextField();
textField_14.setEditable(false);
textField_14.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_14.setColumns(10);
textField_14.setBounds(440, 219, 200, 64);
frame.getContentPane().add(textField_14);
textField_15 = new JTextField();
textField_15.setEditable(false);
textField_15.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_15.setColumns(10);
textField_15.setBounds(440, 293, 200, 64);
frame.getContentPane().add(textField_15);
textField_16 = new JTextField();
textField_16.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_16.setColumns(10);
textField_16.setBounds(650, 219, 200, 64);
frame.getContentPane().add(textField_16);
textField_17 = new JTextField();
textField_17.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_17.setColumns(10);
textField_17.setBounds(650, 293, 200, 64);
frame.getContentPane().add(textField_17);
textField_18 = new JTextField();
textField_18.setEditable(false);
textField_18.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_18.setColumns(10);
textField_18.setBounds(860, 219, 200, 64);
frame.getContentPane().add(textField_18);
textField_19 = new JTextField();
textField_19.setEditable(false);
textField_19.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_19.setColumns(10);
textField_19.setBounds(860, 293, 200, 64);
frame.getContentPane().add(textField_19);
textField_20 = new JTextField();
textField_20.setEditable(false);
textField_20.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_20.setColumns(10);
textField_20.setBounds(860, 386, 200, 64);
frame.getContentPane().add(textField_20);
textField_21 = new JTextField();
textField_21.setEditable(false);
textField_21.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_21.setColumns(10);
textField_21.setBounds(440, 386, 200, 64);
frame.getContentPane().add(textField_21);
textField_22 = new JTextField();
textField_22.setEditable(false);
textField_22.setFont(new Font("Tahoma", Font.PLAIN, 40));
textField_22.setColumns(10);
textField_22.setBounds(231, 386, 200, 64);
frame.getContentPane().add(textField_22);
Box horizontalBox = Box.createHorizontalBox();
horizontalBox.setBounds(213, 74, -187, 64);
frame.getContentPane().add(horizontalBox);
Box horizontalBox_1 = Box.createHorizontalBox();
horizontalBox_1.setBounds(22, 74, 200, 64);
frame.getContentPane().add(horizontalBox_1);
JLabel lblTest = new JLabel("Latex A");
lblTest.setFont(new Font("Tahoma", Font.PLAIN, 35));
horizontalBox_1.add(lblTest);
Box horizontalBox_2 = Box.createHorizontalBox();
horizontalBox_2.setBounds(22, 147, 200, 64);
frame.getContentPane().add(horizontalBox_2);
JLabel lblLatexB = new JLabel("Latex B");
lblLatexB.setFont(new Font("Tahoma", Font.PLAIN, 35));
horizontalBox_2.add(lblLatexB);
Box horizontalBox_3 = Box.createHorizontalBox();
horizontalBox_3.setBounds(22, 219, 200, 64);
frame.getContentPane().add(horizontalBox_3);
JLabel lblSyntheticA = new JLabel("Synthetic A");
lblSyntheticA.setFont(new Font("Tahoma", Font.PLAIN, 35));
horizontalBox_3.add(lblSyntheticA);
Box horizontalBox_4 = Box.createHorizontalBox();
horizontalBox_4.setBounds(22, 293, 200, 64);
frame.getContentPane().add(horizontalBox_4);
JLabel lblSyntheticB = new JLabel("Synthetic B");
lblSyntheticB.setFont(new Font("Tahoma", Font.PLAIN, 35));
horizontalBox_4.add(lblSyntheticB);
Box horizontalBox_5 = Box.createHorizontalBox();
horizontalBox_5.setBounds(231, 0, 200, 64);
frame.getContentPane().add(horizontalBox_5);
JLabel lblPairsYear = new JLabel("Pairs / Year");
lblPairsYear.setFont(new Font("Tahoma", Font.PLAIN, 35));
horizontalBox_5.add(lblPairsYear);
Box horizontalBox_6 = Box.createHorizontalBox();
horizontalBox_6.setBounds(440, 0, 200, 64);
frame.getContentPane().add(horizontalBox_6);
JLabel lblOfTotal = new JLabel("% Of Total");
lblOfTotal.setFont(new Font("Tahoma", Font.PLAIN, 35));
horizontalBox_6.add(lblOfTotal);
Box horizontalBox_7 = Box.createHorizontalBox();
horizontalBox_7.setBounds(650, 0, 200, 64);
frame.getContentPane().add(horizontalBox_7);
JLabel lblPricePerPair = new JLabel("Price Per Pair");
lblPricePerPair.setFont(new Font("Tahoma", Font.PLAIN, 33));
horizontalBox_7.add(lblPricePerPair);
Box horizontalBox_8 = Box.createHorizontalBox();
horizontalBox_8.setBounds(860, 0, 200, 64);
frame.getContentPane().add(horizontalBox_8);
JLabel lblTotalSpent = new JLabel("Total Spent");
lblTotalSpent.setFont(new Font("Tahoma", Font.PLAIN, 35));
horizontalBox_8.add(lblTotalSpent);
JSlider slider = new JSlider();
slider.setToolTipText("h");
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.setFont(new Font("Tahoma", Font.PLAIN, 72));
slider.setBounds(39, 592, 1021, 88);
frame.getContentPane().add(slider);
JLabel lblAverageSalePrice = new JLabel("Average Sale Price");
lblAverageSalePrice.setFont(new Font("Tahoma", Font.PLAIN, 35));
lblAverageSalePrice.setBounds(39, 536, 384, 43);
frame.getContentPane().add(lblAverageSalePrice);
JTextArea textArea = new JTextArea();
textArea.setFont(new Font("Monospaced", Font.PLAIN, 43));
textArea.setBounds(468, 525, 172, 61);
frame.getContentPane().add(textArea);
JTextArea textArea_1 = new JTextArea();
textArea_1.setFont(new Font("Monospaced", Font.PLAIN, 43));
textArea_1.setBounds(468, 714, 172, 61);
frame.getContentPane().add(textArea_1);
JLabel lblTotalNumberOf = new JLabel("Total Number Of Pairs");
lblTotalNumberOf.setFont(new Font("Tahoma", Font.PLAIN, 35));
lblTotalNumberOf.setBounds(39, 725, 384, 43);
frame.getContentPane().add(lblTotalNumberOf);
JSlider slider_1 = new JSlider();
slider_1.setToolTipText("h");
slider_1.setPaintTicks(true);
slider_1.setPaintLabels(true);
slider_1.setFont(new Font("Tahoma", Font.PLAIN, 72));
slider_1.setBounds(39, 783, 1021, 88);
frame.getContentPane().add(slider_1);
JTextArea textArea_2 = new JTextArea();
textArea_2.setFont(new Font("Monospaced", Font.PLAIN, 43));
textArea_2.setBounds(468, 895, 172, 61);
frame.getContentPane().add(textArea_2);
JLabel lblPriceReduction = new JLabel("Price Reduction");
lblPriceReduction.setFont(new Font("Tahoma", Font.PLAIN, 35));
lblPriceReduction.setBounds(39, 917, 384, 43);
frame.getContentPane().add(lblPriceReduction);
JSlider slider_2 = new JSlider();
slider_2.setToolTipText("h");
slider_2.setPaintTicks(true);
slider_2.setPaintLabels(true);
slider_2.setFont(new Font("Tahoma", Font.PLAIN, 72));
slider_2.setBounds(39, 968, 1021, 88);
frame.getContentPane().add(slider_2);
JTextArea textArea_3 = new JTextArea();
textArea_3.setEditable(false);
textArea_3.setFont(new Font("Monospaced", Font.PLAIN, 43));
textArea_3.setBounds(417, 1129, 292, 61);
frame.getContentPane().add(textArea_3);
JButton btnNewButton = new JButton("Calculate Total");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 43));
btnNewButton.setBounds(37, 1113, 330, 77);
frame.getContentPane().add(btnNewButton);
if(btnNewButton.getModel().isPressed())
{
// Sets value for # of gloves per type
LA = Integer.parseInt(textField_1.getText());
LB = Integer.parseInt(textField_8.getText());
SA = Integer.parseInt(textField_12.getText());
SB = Integer.parseInt(textField_13.getText());
total = LA + LB + SA + SB;
// Sets value for percentage of gloves compared to total
percLA = LA / total;
percLB = LB / total;
percSA = SA / total;
percSB = SB / total;
double percTotal = percLA + percLB + percSA + percSB;
//21;
// Sets value for total # of gloves
textField_22.setText(Double.toString(total));
// Sets value for percentage
textField_2.setText(Double.toString(percLA));
textField_7.setText(Double.toString(percLB));
textField_14.setText(Double.toString(percSA));
textField_15.setText(Double.toString(percSB));
textField_21.setText(Double.toString(percTotal));
// Sets value for cost per pair
costLA = Integer.parseInt(textField_3.getText());
costLB = Integer.parseInt(textField_6.getText());
costSA = Integer.parseInt(textField_16.getText());
costSB = Integer.parseInt(textField_17.getText());
}
}
}
Update : created an anonymous class for the listener but my code won't initialize for where I place the code for my button to perform. Here's the code
JTextArea textArea_2 = new JTextArea();
textArea_2.setFont(new Font("Monospaced", Font.PLAIN, 43));
textArea_2.setBounds(468, 895, 172, 61);
frame.getContentPane().add(textArea_2);
JLabel lblPriceReduction = new JLabel("Price Reduction");
lblPriceReduction.setFont(new Font("Tahoma", Font.PLAIN, 35));
lblPriceReduction.setBounds(39, 917, 384, 43);
frame.getContentPane().add(lblPriceReduction);
JSlider slider_2 = new JSlider();
slider_2.setToolTipText("h");
slider_2.setPaintTicks(true);
slider_2.setPaintLabels(true);
slider_2.setFont(new Font("Tahoma", Font.PLAIN, 72));
slider_2.setBounds(39, 968, 1021, 88);
frame.getContentPane().add(slider_2);
JTextArea textArea_3 = new JTextArea();
textArea_3.setEditable(false);
textArea_3.setFont(new Font("Monospaced", Font.PLAIN, 43));
textArea_3.setBounds(417, 1129, 292, 61);
frame.getContentPane().add(textArea_3);
JButton btnNewButton = new JButton("Calculate Total");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 43));
btnNewButton.setBounds(37, 1113, 330, 77);
frame.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(new Action());
}
static class Action implements ActionListener {
public void actionPerformed (ActionEvent e){
textArea_1.setText("This is a test");
}
}
}
However where I typed //Trying to place code here, I can't call for example textfield_3.setText();. Nothing will initialize.
You have to add an actionListener to the JButton and implement an actionPerformed method of that interface. Please, take a look at http://docs.oracle.com/javase/tutorial/uiswing/components/button.html and you will get some tips. Your code is just not attached to the button.
I'm not really sure what the real problem is, but I will recommend you to implement ActionListener in your class so you will have to overwrite the ActionPerformed method instead of just using an anonymous class.
This would be the example:
public class Programs implements ActionListener{
// When you create your button
JButton btnNewButton = new JButton("Calculate Total");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 43));
// This line tells the button where the actionListener will be
// handlded
btnNewButton.addActionListener(this);
//all your code...
// I will catch all the actions you send me
public void actionPerformed(ActionEvent e){
// Here goes the name of the instance, remember that.
if(e.getSource(btnNewButton)){
//code to execute when you click the button ...
}
}
}
The second option would be just adding an anonymous class, I don't recommend this is because the code can get a little dirty.
btnNewButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
// Code to execute when clicked
}
});
in this example you don't need to implement ActionListener interface, you just make an anonymous class and internally assing a method.
Good luck, if you have any question i'll be here.
I've been working at this problem for hours now, with no results. I cannot seem to get the applet to display properly when viewing the HTML page OR when running the applet through Eclipse. It is always blank. I've been searching for a long time now and decided just to ask.
Here's the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class Topics extends Applet{
String topicTotal = "";
JPanel topicPanel;
JLabel title, username, topic, paragraph, topicsTitle;
JTextField nameField, topicField;
JButton submitButton;
JTextArea paragraphArea;
public String getTopicTotal() {
return topicTotal;
}
public JPanel createContentPane(){
final JPanel topicGUI = new JPanel();
topicGUI.setLayout(null);
//posting panel
final JPanel postPanel = new JPanel();
postPanel.setLayout(null);
postPanel.setLocation(0, 0);
postPanel.setSize(500, 270);
topicGUI.add(postPanel);
setVisible(true);
// JLabels
JLabel title = new JLabel("Make A Post");
title.setLocation(170, 3);
title.setSize(150,25);
title.setFont(new Font("Serif", Font.PLAIN, 25));
title.setHorizontalAlignment(0);
postPanel.add(title);
JLabel username = new JLabel("Username: ");
username.setLocation(20, 30);
username.setSize(70,15);
username.setHorizontalAlignment(0);
postPanel.add(username);
JLabel topic = new JLabel("Topic: ");
topic.setLocation(20, 50);
topic.setSize(40,15);
topic.setHorizontalAlignment(0);
postPanel.add(topic);
JLabel paragraph = new JLabel("Paragraph: ");
paragraph.setLocation(20, 70);
paragraph.setSize(70,15);
paragraph.setHorizontalAlignment(0);
postPanel.add(paragraph);
// JTextFields
nameField = new JTextField(8);
nameField.setLocation(90, 30);
nameField.setSize(150, 18);
postPanel.add(nameField);
topicField = new JTextField(8);
topicField.setLocation(60, 50);
topicField.setSize(180, 18);
postPanel.add(topicField);
// JTextAreas
paragraphArea = new JTextArea(8, 5);
paragraphArea.setLocation(20, 85);
paragraphArea.setSize(450, 100);
paragraphArea.setLineWrap(true);
paragraphArea.setEditable(true);
JScrollPane scrollParagraph = new JScrollPane (paragraphArea);
scrollParagraph.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
postPanel.add(paragraphArea);
// JButton
JButton submitButton = new JButton("SUBMIT");
submitButton.setLocation(250, 30);
submitButton.setSize(100, 30);
submitButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
topicTotal = topicTotal + "/n" + nameField + "/n" + topicTotal + "/n" + paragraphArea + "/n";
}
});
postPanel.add(submitButton);
setVisible(true);
return topicGUI;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("");
Topics display = new Topics();
frame.setContentPane(display.createContentPane());
frame.setSize(500, 270);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
In order to run as an applet, you need to override the init method. You don't need a main method. Just add all you components inside the init method
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class Topics extends Applet {
String topicTotal = "";
JPanel topicPanel;
JLabel title, username, topic, paragraph, topicsTitle;
JTextField nameField, topicField;
JButton submitButton;
JTextArea paragraphArea;
public String getTopicTotal() {
return topicTotal;
}
public void init() {
final JPanel topicGUI = new JPanel();
topicGUI.setLayout(null);
// posting panel
final JPanel postPanel = new JPanel();
postPanel.setLayout(null);
postPanel.setLocation(0, 0);
postPanel.setSize(500, 270);
add(postPanel);
setVisible(true);
// JLabels
JLabel title = new JLabel("Make A Post");
title.setLocation(170, 3);
title.setSize(150, 25);
title.setFont(new Font("Serif", Font.PLAIN, 25));
title.setHorizontalAlignment(0);
add(title);
JLabel username = new JLabel("Username: ");
username.setLocation(20, 30);
username.setSize(70, 15);
username.setHorizontalAlignment(0);
add(username);
JLabel topic = new JLabel("Topic: ");
topic.setLocation(20, 50);
topic.setSize(40, 15);
topic.setHorizontalAlignment(0);
add(topic);
JLabel paragraph = new JLabel("Paragraph: ");
paragraph.setLocation(20, 70);
paragraph.setSize(70, 15);
paragraph.setHorizontalAlignment(0);
add(paragraph);
// JTextFields
nameField = new JTextField(8);
nameField.setLocation(90, 30);
nameField.setSize(150, 18);
add(nameField);
topicField = new JTextField(8);
topicField.setLocation(60, 50);
topicField.setSize(180, 18);
add(topicField);
// JTextAreas
paragraphArea = new JTextArea(8, 5);
paragraphArea.setLocation(20, 85);
paragraphArea.setSize(450, 100);
paragraphArea.setLineWrap(true);
paragraphArea.setEditable(true);
JScrollPane scrollParagraph = new JScrollPane(paragraphArea);
scrollParagraph
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(paragraphArea);
// JButton
JButton submitButton = new JButton("SUBMIT");
submitButton.setLocation(250, 30);
submitButton.setSize(100, 30);
submitButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
topicTotal = topicTotal + "/n" + nameField + "/n" + topicTotal
+ "/n" + paragraphArea + "/n";
}
});
add(submitButton);
}
}
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();
}
}
}
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){}
}
}