Trying to change image when JButton pressed - java

I am trying to change the Image on the panel when the any of the JButtons are pressed. I have set up an array of images and need it to change to the next image in the array once it has been pressed. Here is my code:
public class SimpleGui implements ActionListener {
JButton button = new JButton("Very Happy");
JButton buttonTwo = new JButton("Happy");
JButton buttonThree = new JButton("Neutral");
JButton buttonFour = new JButton("Sad");
JButton buttonFive = new JButton("Very Sad");
static int[] ButtonArray = new int[5];
private static String[] imageList = { "res/snow.jpg", "res/test-gm.jpg" };
public int i = 0;
public static void main(String[] args) throws FileNotFoundException {
SimpleGui gui = new SimpleGui();
gui.go();
File file = new File("out.txt");
FileOutputStream fos = new FileOutputStream(file);
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
ButtonArray[0] = 0;
ButtonArray[1] = 0;
ButtonArray[2] = 0;
ButtonArray[3] = 0;
ButtonArray[4] = 0;
}
public void go() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBackground(Color.darkGray);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
button.addActionListener(this);
buttonTwo.addActionListener(this);
buttonThree.addActionListener(this);
buttonFour.addActionListener(this);
buttonFive.addActionListener(this);
panel.add(button);
panel.add(buttonTwo);
panel.add(buttonThree);
panel.add(buttonFour);
panel.add(buttonFive);
frame.getContentPane().add(BorderLayout.EAST, panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(650, 600);
frame.setVisible(true);
ImageIcon image = new ImageIcon(imageList[i]);
ImageIcon image1 = new ImageIcon(imageList[i + 1]);
JLabel label = new JLabel("", image, JLabel.CENTER);
JPanel panel2 = new JPanel(new BorderLayout());
panel2.add(label, BorderLayout.CENTER);
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
frame.add(panel2, BorderLayout.CENTER);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button) {
ButtonArray[0] = 1;
ButtonArray[1] = 0;
ButtonArray[2] = 0;
ButtonArray[3] = 0;
ButtonArray[4] = 0;
System.out.println("Very Happy");
}
// buttonTwo = (JButton) event.getSource();
if (event.getSource() == buttonTwo) {
ButtonArray[0] = 0;
ButtonArray[1] = 1;
ButtonArray[2] = 0;
ButtonArray[3] = 0;
ButtonArray[4] = 0;
System.out.println("Happy");
}
// buttonThree = (JButton) event.getSource();
if (event.getSource() == buttonThree) {
ButtonArray[0] = 0;
ButtonArray[1] = 0;
ButtonArray[2] = 1;
ButtonArray[3] = 0;
ButtonArray[4] = 0;
System.out.println("Neutral");
}
// buttonFour = (JButton) event.getSource();
if (event.getSource() == buttonFour) {
ButtonArray[0] = 0;
ButtonArray[1] = 0;
ButtonArray[2] = 0;
ButtonArray[3] = 1;
ButtonArray[4] = 0;
System.out.println("Sad");
}
// buttonFive = (JButton) event.getSource();
if (event.getSource() == buttonFive) {
ButtonArray[0] = 0;
ButtonArray[1] = 0;
ButtonArray[2] = 0;
ButtonArray[3] = 0;
ButtonArray[4] = 1;
System.out.println("Very Sad");
}
// System.out.println(Arrays.toString(ButtonArray));
// ImageIcon image = (imageList[i]);
}
}

I don't really see what most parts of you code are supposed to do. So instead, here's a minimal example that should do what you are asking about: One label, and two buttons setting different images to that label.
ImageIcon[] images = new ImageIcon[] {
new ImageIcon("foo.gif"),
new ImageIcon("bar.gif"),
new ImageIcon("blub.gif")
};
JFrame frame = new JFrame("Test");
frame.getContentPane().setLayout(new FlowLayout());
JLabel label = new JLabel(images[0]);
frame.getContentPane().add(label);
JButton button1 = new JButton("Image 1");
button1.addActionListener(e -> label.setIcon(images[0]));
frame.getContentPane().add(button1);
JButton button2 = new JButton("Image 2");
button2.addActionListener(e -> label.setIcon(images[1]));
frame.getContentPane().add(button2);
frame.pack();
frame.setVisible(true);
Note that this is using Lambda functions (Java 8) but you can do the same with one or more "real" ActionListener classes. The important part is that you call label.setIcon(theImage); this part seems to be missing in your code.
If instead you want to cycle through a list or array of pictures, you can do like this:
AtomicInteger index = new AtomicInteger(0);
JButton buttonCycle = new JButton("Cycle");
buttonCycle.addActionListener(e -> label.setIcon(images[index.getAndIncrement() % images.length]));
frame.getContentPane().add(buttonCycle);
Here, the AtomicInteger is used so I can declare it as a local variable and use it in the lambda. You can just as well use a regular int if you make it a member variable of the surrounding class.
private int c = 0;
...
buttonCycle.addActionListener(e -> label.setIcon(images[c++ % images.length]));
The takeaway is: Create a counter variable, increment it each time the button is called and set the label's icon to the element with that count, module the size of the array.

Related

Java AWT controls issue

I'd like to create a calculator using Java AWT library, however I'm having problems putting a label and a textfield on the same row. Also, everything is mixing up. When I run the program, at first it shows 2 labels and 2 textfields on the same row, but if I resize it, the labels, buttons & textfields are one on top of another. Could you please help me?
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Calculator extends Frame {
public static void main(String[] args) {
Frame frame= new Frame();
frame.setTitle("Mini Calculator App");
frame.setVisible(true);
frame.setSize(300,300);
LayoutManager gridBagLayout = new GridBagLayout();
frame.setLayout(gridBagLayout);
Label firstNumber = new Label("Add a number");
frame.add(firstNumber);
TextField numar1 = new TextField();
frame.add(numar1);
Label secondNumber = new Label("Add another number");
frame.add(secondNumber);
TextField numar2 = new TextField();
frame.add(numar2);
Choice operatie = new Choice();
operatie.add("+"); operatie.add("-");
operatie.add("*"); operatie.add("/");
frame.add(operatie);
Button calcul = new Button("Calculate");
frame.add(calcul);
Label result = new Label("Result is");
frame.add(result);
GridBagConstraints afisare;
afisare = new GridBagConstraints();
afisare.gridx = 0;
afisare.gridy = 0;
frame.add(firstNumber, afisare);
afisare = new GridBagConstraints();
afisare.gridx = 0;
afisare.gridy = 1;
afisare.gridwidth = 10;
afisare.gridheight = 10;
frame.add(numar1, afisare);
afisare = new GridBagConstraints();
afisare.gridx = 1;
afisare.gridy = 0;
frame.add(secondNumber, afisare);
afisare = new GridBagConstraints();
afisare.gridx = 1;
afisare.gridy = 1;
frame.add(numar2, afisare);
afisare = new GridBagConstraints();
afisare.gridx = 2;
afisare.gridy = 1;
frame.add(operatie, afisare);
afisare = new GridBagConstraints();
afisare.gridx = 4;
afisare.gridy = 0;
frame.add(result, afisare);
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
enter image description here
enter image description here

Is it possible to change the height of each grid in a GridLayout?

I am trying to create a simple menu interface with 4 rows of various buttons and labels using GridLayout with FlowLayout inside each grid for organising the elements. However the space for the buttons and labels which should only take 1 line takes up a huge amount of space.
This is what my interface looks like minimized:
This is what it looks like maximized:
I am looking to set the maximum size of the labels panels/grid so that it only takes a small amount of space.
I am trying to make all the elements visible without anything being hidden with as small a window size as possible like this:
This is my code:
public class Window extends JFrame{
public Window() {
super("TastyThai Menu Ordering");
}
public static void main(String[] args) {
Window w = new Window();
w.setSize(500, 500);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel title = new JLabel("TastyThai Menu Order", SwingConstants.CENTER);
title.setFont(title.getFont().deriveFont(32f));
//generate page title
Container titlePanel = new JPanel(); // used as a container
titlePanel.setBackground(Color.WHITE);
FlowLayout flow = new FlowLayout(); // Create a layout manager
titlePanel.setLayout(flow);// assign flow layout to panel
titlePanel.add(title); // add label to panel
w.getContentPane().add(BorderLayout.NORTH,titlePanel);
//generate row containers
Container r1 = new JPanel(new FlowLayout());
Container r2 = new JPanel(new FlowLayout());
Container r3 = new JPanel(new FlowLayout());
Container r4 = new JPanel(new FlowLayout());
//generate mains radio buttons
Container mains = new JPanel(new GridLayout(7, 0));
mains.setBackground(Color.RED);
JLabel mainsHeader = new JLabel("Mains");
mains.add(mainsHeader);
String[] mainsChoices = {"Vegetarian", "Chicken", "Beef", "Pork", "Duck", "Seafood Mix"};
JRadioButton[] mainsRadioButton = new JRadioButton[6];
ButtonGroup mainsButtons = new ButtonGroup();
for(int i = 0; i < mainsChoices.length; i++) {
mainsRadioButton[i] = new JRadioButton(mainsChoices[i]);
mains.add(mainsRadioButton[i]);
mainsButtons.add(mainsRadioButton[i]);
}
//generate noodles radio buttons
Container noodles = new JPanel(new GridLayout(7, 0));
noodles.setBackground(Color.GREEN);
JLabel noodlesHeader = new JLabel("Noodles");
noodlesHeader.setFont(noodlesHeader.getFont().deriveFont(24f));
noodles.add(noodlesHeader);
String[] noodlesChoices = {"Pad Thai", "Pad Siew", "Ba Mee"};
JRadioButton[] noodlesRadioButton = new JRadioButton[3];
ButtonGroup noodlesButtons = new ButtonGroup();
for(int i = 0; i < noodlesChoices.length; i++) {
noodlesRadioButton[i] = new JRadioButton(noodlesChoices[i]);
noodles.add(noodlesRadioButton[i]);
noodlesButtons.add(noodlesRadioButton[i]);
}
//generate sauces radio buttons
Container sauces = new JPanel(new GridLayout(7, 0));
sauces.setBackground(Color.BLUE);
JLabel saucesHeader = new JLabel("Sauce");
saucesHeader.setFont(saucesHeader.getFont().deriveFont(24f));
sauces.add(saucesHeader);
String[] saucesChoices = {"Soy Sauce", "Tamarind Sauce"};
JRadioButton[] saucesRadioButton = new JRadioButton[2];
ButtonGroup saucesButtons = new ButtonGroup();
for(int i = 0; i < saucesChoices.length; i++) {
saucesRadioButton[i] = new JRadioButton(saucesChoices[i]);
sauces.add(saucesRadioButton[i]);
saucesButtons.add(saucesRadioButton[i]);
}
//generate extras check boxes
Container extras = new JPanel(new GridLayout(7, 0));
extras.setBackground(Color.YELLOW);
JLabel extrasHeader = new JLabel("Extra");
extrasHeader.setFont(extrasHeader.getFont().deriveFont(24f));
extras.add(extrasHeader);
String[] extrasChoices = {"Mushroom", "Egg", "Broccoli", "Beansrpout", "Tofu"};
JCheckBox[] extrasBoxes = new JCheckBox[5];
for(int i = 0; i < extrasChoices.length; i++) {
extrasBoxes[i] = new JCheckBox(extrasChoices[i]);
extras.add(extrasBoxes[i]);
}
JLabel selectionPrice = new JLabel("Selection Price: $ ");
JLabel selectionPriceVal = new JLabel("_______________");
JButton addToOrder = new JButton("Add to Order");
JLabel totalPrice = new JLabel("Total Price: $ ");
JLabel totalPriceVal = new JLabel("_______________");
JButton clearOrder = new JButton("Clear Order");
JRadioButton pickUp = new JRadioButton("Pick Up");
JRadioButton delivery = new JRadioButton("Delivery");
ButtonGroup pickupDelivery = new ButtonGroup();
pickupDelivery.add(pickUp);
pickupDelivery.add(delivery);
JButton completeOrder = new JButton("Complete Order");
Container menuSelection = new JPanel(new GridLayout(4,0));
menuSelection.add(r1);
r1.add(mains);
r1.add(noodles);
r1.add(sauces);
r1.add(extras);
menuSelection.add(r2);
r2.add(selectionPrice);
r2.add(selectionPriceVal);
r2.add(addToOrder);
menuSelection.add(r3);
r3.add(totalPrice);
r3.add(totalPriceVal);
r3.add(clearOrder);
menuSelection.add(r4);
r4.add(pickUp);
r4.add(delivery);
r4.add(completeOrder);
w.getContentPane().add(BorderLayout.CENTER, menuSelection);
w.setVisible(true);
}
}
GridLayout does not support that. All rectangles have the same size.
Take a look at the GridBagLayout, which supports dynamic resizing and much more.

Random Number won't appear in a JTextField

I have a problem.I created a program that will add two random numbers. I'm trying to put a Math.random() in a JTextField but it won't appear. Here's my code by the way:
public class RandomMathGame extends JFrame {
public RandomMathGame(){
super("Random Math Game");
int random2;
JButton lvl1 = new JButton("LEVEL 1");
JButton lvl2 = new JButton("LEVEL 2");
JButton lvl3 = new JButton("LEVEL 3");
JLabel line1 = new JLabel("Line 1: ");
final JTextField jtf1 = new JTextField(10);
JLabel line2 = new JLabel("Line 2: ");
final JTextField jtf2 = new JTextField(10);
JLabel result = new JLabel("Result: ");
final JTextField jtf3 = new JTextField(10);
JButton ans = new JButton("Answer");
JLabel score = new JLabel("Score: ");
JTextField jtf4 = new JTextField(3);
JLabel itm = new JLabel("Number of Items: ");
JTextField items = new JTextField(3);
FlowLayout flo = new FlowLayout();
setLayout(flo);
add(lvl1);
add(lvl2);
add(lvl3);
add(line1);
add(jtf1);
add(line2);
add(jtf2);
add(result);
add(jtf3);
add(ans);
add(score);
add(jtf4);
add(itm);
add(items);
setSize(140,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
lvl1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int i, j = 10;
int i1 = Integer.valueOf(jtf1.getText());
int i2 = Integer.valueOf(jtf2.getText());
int i3 = i1 + i2;
final int random1 = (int)(Math.random() * 10 + 1);
for (i = 0; i <= j + 1; i++){
try{
jtf1.setText(String.valueOf(random1));
jtf2.setText(String.valueOf(random1));
jtf3.setText(String.valueOf(i3));
}catch(Exception ex){
ex.printStackTrace();
}
}
}
});
}
Never mind the lvl2 and lvl3 because it's the same in lvl1. And also, I want to loop them 10 times. I'm having difficulties on putting up those codes. Can someone help me? Thanks for your help. :)
Updating text fields in a loop won't produce the animated display that you likely want; only the last update will be seen. Instead, use a javax.swing.Timer to periodically update the fields. Related examples may be found here, here and here.

java.lang.numberformatexception empty string java awt

public class CustomCalculator extends Frame implements ActionListener{
Panel jp1 = new Panel();
Panel jp2 = new Panel();
Panel jp3 = new Panel();
Panel jp4 = new Panel();
Panel jp5 = new Panel();
Panel center_merge = new Panel();
Label l2 = new Label("Quantity : ");
TextField l2a = new TextField(20);
Label l3 = new Label("Invoice Value : ");
TextField l3a = new TextField(20);
Label l4 = new Label("Exchange Rate : ");
TextField l4a = new TextField(20);
Label l5 = new Label("Costing(A) : ");
TextField l5a = new TextField();
Label l6 = new Label("(A + 1%)(B) : ");
Label l6a = new Label();
Label l7 = new Label("BCD (C) : ");
Label l7a = new Label("");
Label l8 = new Label("CVD (D) : ");
Label l8a = new Label("");
Label l9 = new Label("Custom Education Cess (E) : ");
Label l9a = new Label("");
Label l10 = new Label("Custom Sec & Higher Edu.Cess (F) : ");
Label l10a = new Label("");
Label l11 = new Label("Additional Duty Imports (G) : ");
Label l11a = new Label("");
Label l12 = new Label("Total (H) : ");
Label l12a = new Label("");
Label l13 = new Label("Costing+Total (I) : ");
Label l13a = new Label("");
Label l14 = new Label("(H/Quantity) (J) : ");
Label l14a = new Label("");
Label l15 = new Label("4% SAD (G/Quantity) (K) : ");
Label l15a = new Label("");
Label l16 = new Label("Net Costing (L) : ");
Label l16a = new Label("");
Label l17 = new Label("Transportation (M) : ");
TextField l17a = new TextField(5);
Label l18 = new Label("Godown Rate (N) : ");
TextField l18a = new TextField(5);
Label l19 = new Label("Brokerage (O) : ");
TextField l19a = new TextField(5);
Label l20 = new Label("Actual Costing (P) : ");
Label l20a = new Label("");
Label l21 = new Label("Small Gatepass (Q) : ");
Label l21a = new Label("");
Label l22 = new Label("Big Gatepass (R) : ");
Label l22a = new Label("");
Button l2b = new Button("reset");
Button l3b = new Button("reset");
Button l4b = new Button("reset");
Button master_reset = new Button("reset all");
Button calc = new Button("Calculate");
public CustomCalculator()
{
super("Custom Calculator");
this.setSize(800,700);
jp1.setLayout(new FlowLayout());
//jp1.setBorder(BorderFactory.createLineBorder(Color.GRAY));
jp1.add(l2);
jp1.add(l2a);
jp1.add(l2b);
jp1.add(l3);
jp1.add(l3a);
jp1.add(l3b);
jp1.add(l4);
jp1.add(l4a);
jp1.add(l4b);
jp2.setLayout(new GridLayout(6,2));
//jp2.setBorder(BorderFactory.createLineBorder(Color.GRAY));
jp2.add(l5);
jp2.add(l5a);
jp2.add(l6);
jp2.add(l6a);
jp2.add(l7);
jp2.add(l7a);
jp2.add(l8);
jp2.add(l8a);
jp2.add(l9);
jp2.add(l9a);
jp2.add(l10);
jp2.add(l10a);
jp3.setLayout(new GridLayout(6,2));
//jp3.setBorder(BorderFactory.createLineBorder(Color.GRAY));
jp3.add(l11);
jp3.add(l11a);
jp3.add(l12);
jp3.add(l12a);
jp3.add(l13);
jp3.add(l13a);
jp3.add(l14);
jp3.add(l14a);
jp3.add(l15);
jp3.add(l15a);
jp3.add(l16);
jp3.add(l16a);
jp4.setLayout(new GridLayout(6,2));
//jp4.setBorder(BorderFactory.createLineBorder(Color.GRAY));
jp4.add(l17);
jp4.add(l17a);
jp4.add(l18);
jp4.add(l18a);
jp4.add(l19);
jp4.add(l19a);
jp4.add(l20);
jp4.add(l20a);
jp4.add(l21);
jp4.add(l21a);
jp4.add(l22);
jp4.add(l22a);
center_merge.setLayout(new GridLayout(1,3));
//center_merge.setBorder(BorderFactory.createLineBorder(Color.GRAY));
center_merge.add(jp2);
center_merge.add(jp3);
center_merge.add(jp4);
jp5.setLayout(new FlowLayout());
//jp5.setBorder(BorderFactory.createLineBorder(Color.GRAY));
jp5.add(calc);
jp5.add(master_reset);
this.setLayout(new BorderLayout());
this.add(jp1,BorderLayout.NORTH);
this.add(center_merge,BorderLayout.CENTER);
this.add(jp5,BorderLayout.SOUTH);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
l2b.addActionListener(this);
l3b.addActionListener(this);
l4b.addActionListener(this);
calc.addActionListener(this);
master_reset.addActionListener(this);
this.setVisible(true);
}
public static void main(String[] args) {
new CustomCalculator();
}
#Override
public void actionPerformed(ActionEvent ae) {
double quantity = 0;
double invoice_value = 0;
double exchange_rate = 0;
double A=0;
double B=0;
double C=0;
double D=0;
double E=0;
double F=0;
double G=0;
double H=0;
double I=0;
double J=0;
double K=0;
double L=0;
double M = 0;
double N = 0;
double O=0;
double P=0;
double Q=0;
double R=0;
try
{
quantity = Double.parseDouble(l2a.getText());
invoice_value = Double.parseDouble(l3a.getText());
exchange_rate = Double.parseDouble(l4a.getText());
M = Double.parseDouble(l17a.getText());
N = Double.parseDouble(l18a.getText());
O = Double.parseDouble(l19a.getText());
A = invoice_value*exchange_rate;
B = A+(0.01*A);
C = 0.075*B;
D = 0.12*(B+C);
E = 0.02*(C+D);
F = 0.01*(C+D);
G = 0.04*(B+C+D+E+F);
H = C+D+E+F+G;
I = A+H;
J = H/quantity;
K = G/quantity;
L = J-K;
P = L+M+N+O;
Q = (0.12*B)/quantity;
R = Q+K;
if(ae.getActionCommand().equals("calc"))
{
l5a.setText(String.valueOf(A));
l6a.setText(String.valueOf(B));
l7a.setText(String.valueOf(C));
l8a.setText(String.valueOf(D));
l9a.setText(String.valueOf(E));
l10a.setText(String.valueOf(F));
l11a.setText(String.valueOf(G));
l12a.setText(String.valueOf(H));
l13a.setText(String.valueOf(I));
l14a.setText(String.valueOf(J));
l15a.setText(String.valueOf(K));
l16a.setText(String.valueOf(L));
l20a.setText(String.valueOf(P));
l21a.setText(String.valueOf(Q));
l22a.setText(String.valueOf(R));
}
else if(ae.getActionCommand().equals("master_reset"))
{
l5a.setText("");
l2a.setText("");
l3a.setText("");
l4a.setText("");
}
}
catch (Exception ex)
{
l5a.setText(ex.toString());
// l3a.setText(ex.toString());
}
}
}
After I click the Calculate button (button calc) the calculated values do not appear in the respective labels and an exception is shown saying java.lang.NumberFormatException: Empty string. I am not able to figure out the solution. please help.
the line exchange_rate = Double.parseDouble(l4a.getText()); gives you this exception, because there is no value in l4a and you are trying to parse it into a double value,
try printing the exception in the catch clause.
It's probably a relatively safe bet to say that it is happening here at the beginning of your function, though you should provide the actual exception and line number for us.
quantity = Double.parseDouble(l2a.getText());
invoice_value = Double.parseDouble(l3a.getText());
exchange_rate = Double.parseDouble(l4a.getText());
M = Double.parseDouble(l17a.getText());
N = Double.parseDouble(l18a.getText());
O = Double.parseDouble(l19a.getText());
Make sure that each of these fields actually has numeric text in it. I would recommend putting a logging line or an alert line to state their values before this is executed. Better yet, you can catch the first line in a debugger and look at the the values of all of the items you're calling getText() on. I bet one has an empty string.

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

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

Categories