Alright guys, after some delays I'm back to trying to write my *simple* java program. I just started working on a GUI, and applet. This is my first attempt and I'm really stuck here. I've created the first half of it, I've got it were the user can choose the amounts for each drop list. I'm using Java GUI Builder to visual build the GUI, then JCreator to modify the code. Now what I need to do is be able to (as seen in my command lien version) take what the user selects, subtract it from a defined number, then get the computer to print it out on the other side of the screen. How do I do this?
I'm modeling the applet after this command line version of my program.
Here is my code so far for my applet version.
I'm modeling the applet after this command line version of my program.
Code:
import java.io.*;
import java.text.NumberFormat;
class Credits {
public static void main(String args[]) throws IOException
{
BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));
Double creditsn, credits, LA, MM, Science, WH, AH;
Double Economics, AG, Arts, LMS, PF, PE, Electives;
System.out.print("How many Language Arts credits do you have? ");
LA = Double.parseDouble(keyboard.readLine());
System.out.print("How many Mathematics credits do you have? ");
MM = Double.parseDouble(keyboard.readLine());
System.out.print("How many Science credits do you have? ");
Science = Double.parseDouble(keyboard.readLine());
System.out.print("How many World History credits do you have? ");
WH = Double.parseDouble(keyboard.readLine());
System.out.print("How many American History credits do you have? ");
AH = Double.parseDouble(keyboard.readLine());
System.out.print("How many Econimic credits do you have? ");
Economics = Double.parseDouble(keyboard.readLine());
System.out.print("How many American Goverment credits do you have? ");
AG = Double.parseDouble(keyboard.readLine());
System.out.print("How many Practical/Fine Art credits do you have? ");
Arts = Double.parseDouble(keyboard.readLine());
System.out.print("How many Life Management Skill credits do you have? ");
LMS = Double.parseDouble(keyboard.readLine());
System.out.print("How many Personal Fitness credits do you have? ");
PF = Double.parseDouble(keyboard.readLine());
System.out.print("How many Pyscial Education credits do you have? ");
PE = Double.parseDouble(keyboard.readLine());
System.out.print("How many Elective credits do you have? ");
Electives = Double.parseDouble(keyboard.readLine());
creditsn = LA + MM + Science + WH + AH + Economics + AG + Arts + LMS + PF + PE + Electives;
System.out.println("------------------------------");
System.out.print("You need ");
System.out.print(4 - LA );
System.out.println(" more Language Arts credits");
System.out.print("You need ");
System.out.print(3 - MM );
System.out.println(" more Mathematic credits");
System.out.print("You need ");
System.out.print(3 - Science );
System.out.println(" more Science credits");
System.out.print("You need ");
System.out.print(1 - WH);
System.out.println(" more World History credits");
System.out.print("You need ");
System.out.print(1 - AH );
System.out.println(" more American History credits");
System.out.print("You need ");
System.out.print(.5 - Economics );
System.out.println(" more Economics credits");
System.out.print("You need ");
System.out.print(.5 - AG );
System.out.println(" more American Government credits");
System.out.print("You need ");
System.out.print(1 - Arts );
System.out.println(" more Fine/Pratical Arts credits");
System.out.print("You need ");
System.out.print(.5 - LMS );
System.out.println(" more Life Management Skills credits");
System.out.print("You need ");
System.out.print(.5 - PF );
System.out.println(" more Physical Fitness credits");
System.out.print("You need ");
System.out.print(.5 - PE );
System.out.println(" more Pyhiscal Education credits");
System.out.print("You need ");
System.out.print(8.5 - Electives );
System.out.println(" more elective credits");
System.out.print("You need ");
System.out.print(24 - creditsn );
System.out.println(" more total credits");
}
}
Here is my code so far for my applet version.
Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;
class gui extends JApplet {
JTextField textfield_1;
Choice choice_1;
Choice choice_2;
Choice choice_3;
Choice choice_4;
Choice choice_5;
Choice choice_6;
Choice choice_7;
Choice choice_8;
Choice choice_9;
Choice choice_10;
Choice choice_11;
Choice choice_12;
TextField textfield_2;
TextField textfield_3;
TextField textfield_4;
TextField textfield_5;
TextField textfield_6;
TextField textfield_7;
TextField textfield_8;
TextField textfield_9;
TextField textfield_10;
TextField textfield_11;
TextField textfield_12;
TextField textfield_13;
public void init() {
guiLayout customLayout = new guiLayout();
getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12));
getContentPane().setLayout(customLayout);
textfield_1 = new JTextField("This program will tell you how many credits you need to graduate");
getContentPane().add(textfield_1);
choice_1 = new Choice();
choice_1.addItem("0");
choice_1.addItem("0.5");
choice_1.addItem("1");
choice_1.addItem("1.5");
choice_1.addItem("2");
choice_1.addItem("2.5");
choice_1.addItem("3");
choice_1.addItem("3.5");
choice_1.addItem("4");
getContentPane().add(choice_1);
choice_2 = new Choice();
choice_2.addItem("0");
choice_2.addItem("0.5");
choice_2.addItem("1");
choice_2.addItem("1.5");
choice_2.addItem("2");
choice_2.addItem("2.5");
choice_2.addItem("3");
getContentPane().add(choice_2);
choice_3 = new Choice();
choice_3.addItem("0");
choice_3.addItem("0.5");
choice_3.addItem("1");
choice_3.addItem("1.5");
choice_3.addItem("2");
choice_3.addItem("2.5");
choice_3.addItem("3");
getContentPane().add(choice_3);
choice_4 = new Choice();
choice_4.addItem("0");
choice_4.addItem("0.5");
choice_4.addItem("1");
getContentPane().add(choice_4);
choice_5 = new Choice();
choice_5.addItem("0");
choice_5.addItem("0.5");
choice_5.addItem("1");
getContentPane().add(choice_5);
choice_6 = new Choice();
choice_6.addItem("0");
choice_6.addItem("0.5");
getContentPane().add(choice_6);
choice_7 = new Choice();
choice_7.addItem("0");
choice_7.addItem("0.5");
getContentPane().add(choice_7);
choice_8 = new Choice();
choice_8.addItem("0");
choice_8.addItem("0.5");
choice_8.addItem("1");
getContentPane().add(choice_8);
choice_9 = new Choice();
choice_9.addItem("0");
choice_9.addItem(".5");
getContentPane().add(choice_9);
choice_10 = new Choice();
choice_10.addItem("0");
choice_10.addItem("0.5");
getContentPane().add(choice_10);
choice_11 = new Choice();
choice_11.addItem("0");
choice_11.addItem("0.5");
getContentPane().add(choice_11);
choice_12 = new Choice();
choice_12.addItem("0");
choice_12.addItem("0.5");
choice_12.addItem("1");
choice_12.addItem("1.5");
choice_12.addItem("2");
choice_12.addItem("2.5");
choice_12.addItem("3");
choice_12.addItem("3.5");
choice_12.addItem("4");
choice_12.addItem("4.5");
choice_12.addItem("5");
choice_12.addItem("5.5");
choice_12.addItem("6");
choice_12.addItem("6.5");
choice_12.addItem("7");
choice_12.addItem("7.5");
choice_12.addItem("8");
choice_12.addItem("8.5");
getContentPane().add(choice_12);
textfield_2 = new TextField("language Arts Credits");
getContentPane().add(textfield_2);
textfield_3 = new TextField("Mathematics credits");
getContentPane().add(textfield_3);
textfield_4 = new TextField("Science credits");
getContentPane().add(textfield_4);
textfield_5 = new TextField("World History credits");
getContentPane().add(textfield_5);
textfield_6 = new TextField("American History credits");
getContentPane().add(textfield_6);
textfield_7 = new TextField("Econimic credits");
getContentPane().add(textfield_7);
textfield_8 = new TextField("American Goverment credits");
getContentPane().add(textfield_8);
textfield_9 = new TextField("Practical/Fine Art credits");
getContentPane().add(textfield_9);
textfield_10 = new TextField("Life Management Skill credits");
getContentPane().add(textfield_10);
textfield_11 = new TextField("Personal Fitness credits");
getContentPane().add(textfield_11);
textfield_12 = new TextField("Personal Education credits");
getContentPane().add(textfield_12);
textfield_13 = new TextField("Elective credits");
getContentPane().add(textfield_13);
setSize(getPreferredSize());
}
public static void main(String args[]) {
gui applet = new gui();
JFrame window = new JFrame("gui");
window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
applet.init();
window.getContentPane().add("Center", applet);
window.pack();
window.setVisible(true);
}
}
class guiLayout implements LayoutManager {
public guiLayout() {
}
public void addLayoutComponent(String name, Component comp) {
}
public void removeLayoutComponent(Component comp) {
}
public Dimension preferredLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 975 + insets.left + insets.right;
dim.height = 891 + insets.top + insets.bottom;
return dim;
}
public Dimension minimumLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
return dim;
}
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible()) {c.setBounds(insets.left+216,insets.top+8,408,32);}
c = parent.getComponent(1);
if (c.isVisible()) {c.setBounds(insets.left+168,insets.top+136,72,24);}
c = parent.getComponent(2);
if (c.isVisible()) {c.setBounds(insets.left+168,insets.top+168,72,24);}
c = parent.getComponent(3);
if (c.isVisible()) {c.setBounds(insets.left+168,insets.top+200,72,24);}
c = parent.getComponent(4);
if (c.isVisible()) {c.setBounds(insets.left+168,insets.top+232,72,24);}
c = parent.getComponent(5);
if (c.isVisible()) {c.setBounds(insets.left+168,insets.top+264,72,24);}
c = parent.getComponent(6);
if (c.isVisible()) {c.setBounds(insets.left+168,insets.top+296,72,24);}
c = parent.getComponent(7);
if (c.isVisible()) {c.setBounds(insets.left+168,insets.top+328,72,24);}
c = parent.getComponent(8);
if (c.isVisible()) {c.setBounds(insets.left+168,insets.top+360,72,24);}
c = parent.getComponent(9);
if (c.isVisible()) {c.setBounds(insets.left+168,insets.top+392,72,24);}
c = parent.getComponent(10);
if (c.isVisible()) {c.setBounds(insets.left+168,insets.top+424,72,24);}
c = parent.getComponent(11);
if (c.isVisible()) {c.setBounds(insets.left+168,insets.top+456,72,24);}
c = parent.getComponent(12);
if (c.isVisible()) {c.setBounds(insets.left+168,insets.top+488,72,24);}
c = parent.getComponent(13);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+136,144,24);}
c = parent.getComponent(14);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+168,144,24);}
c = parent.getComponent(15);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+200,144,24);}
c = parent.getComponent(16);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+232,144,24);}
c = parent.getComponent(17);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+264,144,24);}
c = parent.getComponent(18);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+296,144,24);}
c = parent.getComponent(19);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+328,144,24);}
c = parent.getComponent(20);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+360,144,24);}
c = parent.getComponent(21);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+392,144,24);}
c = parent.getComponent(22);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+424,144,24);}
c = parent.getComponent(23);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+456,144,24);}
c = parent.getComponent(24);
if (c.isVisible()) {c.setBounds(insets.left+8,insets.top+488,144,24);}
}
}