Psst... Ready to join TalkBass and start posting, make new friends, sell your gear, and more?  Register your free account in 30 seconds.

Java Help Needed!

Discussion in 'Off Topic [BG]' started by Bob Clayton, Apr 30, 2003.


  1. Bob Clayton

    Bob Clayton Moderator Staff Member Supporting Member

    Aug 14, 2001
    Deptford, NJ
    for those of you that are good at Java programming...i want/need to have part of the text Area displayed in frame 2 in color (some of the text, but not all of it). here is the program

    import java.awt.*;
    import java.awt.event.*;

    public class ClassGrades
    {
    Frame frmMain=new Frame();
    Frame frm2=new Frame();
    Button btn1=new Button("Enter Information");
    Button btn2=new Button("Display all grades");
    Button btn3=new Button("Clear");
    Button btn4=new Button("Exit");
    Button btn5=new Button("Add More Names");
    Button btn6=new Button("Exit");
    Label lbl1=new Label("Student's Name");
    Label lbl2=new Label("Test 1");
    Label lbl3=new Label("Test 2");
    Label lbl4=new Label("Test 3");
    Label lbl5=new Label("Student's Average Will Display Here");
    TextField txtName=new TextField(5);
    TextField txtNum1=new TextField(2);
    TextField txtNum2=new TextField(2);
    TextField txtNum3=new TextField(2);
    TextArea txt4=new TextArea(20,50);
    int studentIndex=0;

    Panel pnl1=new Panel();
    Panel pnl2=new Panel();
    Panel pnl3=new Panel();

    String[] names=new String[30];
    double[] test1=new double[30];
    double[] test2=new double[30];
    double[] test3=new double[30];
    double[] average=new double[30];

    WindowListener wl = new WindowAdapter()
    {
    public void windowClosing(WindowEvent ev)
    {
    System.exit(0);
    };
    };

    ActionListener al=new ActionListener()
    {
    public void actionPerformed(ActionEvent evt)
    {
    if(evt.getSource()==btn1)
    {
    calculate();
    }

    if(evt.getSource()==btn2)
    {
    display();
    }

    if(evt.getSource()==btn4)
    {
    System.exit(0);
    }


    if(evt.getSource()==btn5)
    {
    txtName.setText("");
    txtNum1.setText("");
    txtNum2.setText("");
    txtNum3.setText("");
    lbl5.setText("");
    frm2.setVisible(false);
    frmMain.setVisible(true);
    }

    if(evt.getSource()==btn6)
    {
    System.exit(0);
    }
    }
    };


    public void calculate()
    {
    names[studentIndex]=txtName.getText();
    test1[studentIndex]= Double.parseDouble(txtNum1.getText());
    test2[studentIndex]= Double.parseDouble(txtNum2.getText());
    test3[studentIndex]= Double.parseDouble(txtNum3.getText());
    average[studentIndex]= ((test1[studentIndex] + test2[studentIndex] + test3[studentIndex] )/ 3);
    average[studentIndex]= Math.round (average[studentIndex]);
    lbl5.setText(names[studentIndex] + "'s average is " + average[studentIndex]);
    studentIndex++;
    }

    public void display()
    {
    double test1Total = 0;
    double test2Total = 0;
    double test3Total = 0;

    frm2.setVisible(true);
    frmMain.setVisible(false);

    txt4.setText("Name" + "\t" + "\t" + "Test 1" + "\t" + "Test 2" + "\t" + "Test 3" + "\t" + "Average" + "\n");

    for(int x=0; x<studentIndex ; x++)
    {
    txt4.append(names[x] + "\t" + "\t" + test1[x] + "\t" + test2[x] + "\t" + test3[x] + "\t" + average[x] + "\n");

    test1Total = test1Total + test1[x];
    test2Total = test2Total + test2[x];
    test3Total = test3Total + test3[x];
    }



    double test1ClassAverage= test1Total / studentIndex;
    test1ClassAverage = Math.round(test1ClassAverage);
    txt4.append("\n" + "The class average for test 1 is " + test1ClassAverage + "\n");

    double test2ClassAverage= test2Total / studentIndex;
    test2ClassAverage = Math.round(test2ClassAverage);
    txt4.append("The class average for test 2 is " + test2ClassAverage + "\n");

    double test3ClassAverage= test3Total / studentIndex;
    test3ClassAverage = Math.round(test3ClassAverage);
    txt4.append("The class average for test 3 is " + test3ClassAverage + "\n" + "\n");

    double ClassAverage = ((test1ClassAverage + test2ClassAverage + test3ClassAverage) / 3);
    ClassAverage = Math.round(ClassAverage);
    txt4.append("The Class's overall average is " + ClassAverage);
    }






    public ClassGrades()
    {
    frmMain.setLayout(new BorderLayout());
    frmMain.add(pnl1,BorderLayout.WEST);
    frmMain.add(pnl2,BorderLayout.CENTER);
    frmMain.add(pnl3,BorderLayout.EAST);

    pnl1.setLayout(new GridLayout(4,1));
    pnl1.add(lbl1);
    pnl1.add(lbl2);
    pnl1.add(lbl3);
    pnl1.add(lbl4);

    pnl2.setLayout(new GridLayout(4,1));
    pnl2.add(txtName);
    pnl2.add(txtNum1);
    pnl2.add(txtNum2);
    pnl2.add(txtNum3);

    pnl3.setLayout(new GridLayout(4,1));
    pnl3.add(btn1);
    pnl3.add(btn2);
    pnl3.add(btn3);
    pnl3.add(btn4);

    btn1.addActionListener(al);
    btn2.addActionListener(al);
    btn3.addActionListener(al);
    btn4.addActionListener(al);
    btn5.addActionListener(al);
    btn6.addActionListener(al);

    frmMain.add(lbl5,BorderLayout.SOUTH);


    frmMain.addWindowListener(wl);
    frmMain.pack();
    frmMain.setVisible(true);

    frm2.setLayout(new FlowLayout());
    frm2.add(txt4);
    frm2.add(btn5);
    frm2.add(btn6);
    frm2.addWindowListener(wl);
    frm2.pack();
    }

    public static void main (String[] args)
    {
    new ClassGrades();
    }
    }

    thanks in advance for anyhelp

    bp13
     
  2. :confused:

    Can you give more detailed information about what you have to do?
     
  3. Bob Clayton

    Bob Clayton Moderator Staff Member Supporting Member

    Aug 14, 2001
    Deptford, NJ
    sure, when you enter the peoples name and grades, it will display them in the second frame, in a text area "txt4"...she wants just a certain line a different color (it can be any line, any color)...
    for example try to make "classAverage" a different color...

    i tried

    "classAverage.setForeground(Color.blue);"

    but classAverage is a "double" and colors can't be assigned to a double...

    if you set a color to txt4, the whole thing will be that color, she wants only part of it to change

    bp13
     
  4. Bob Clayton

    Bob Clayton Moderator Staff Member Supporting Member

    Aug 14, 2001
    Deptford, NJ
    :help:

    bp13
     
  5. couldn't you just use toString to convert classAverage to string?

    Double.toString(classAverage);