1,105,427 Community Members

help needed in makin GUI for a maths program(with lots of loops).

Member Avatar
BOOMBOOMF
Newbie Poster
5 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hey im new in java. i needto make GUI for my program buh im confused as to what to wirte in GUI codes and wat to write in Main codes etc. could someone plz help me?

mathprogram.java main class

public class mathprogram {
    public  static void main (String[] args){

         addition add = new addition();
         random ran = new random();
         subtraction sub=new subtraction();
         multiplication mult=new multiplication();
         division divide=new division();
         mathmenu mm=new mathmenu();
         mm.setVisible(true);
            
        
        int grade = 1;
        int equation = 10;
        int equationtype;
        int range1 = 0;
        int range2 = 0;
        int question1;
        int question2;
        int questionAnswer = 0;
        int answer = 0;
        int totalCorrect = 0;

         Scanner scanner = new Scanner(System.in);
        boolean menu = true;

        while (menu) {
            try {
                System.out.println("What is your grade: ");
                grade = scanner.nextInt();
            } catch (Exception ex) {
                System.out.println("Incorrect grade. Please re-enter.\n");
                continue;
            }
            if (grade < 1 || grade > 6) {
                System.out.println("Incorrect grade. Please re-enter.\n");
            } else {
                menu = false;
            }
            switch (grade) {
                case 1:
                    range1 = 1;
                    range2 = 20;
                    break;
                case 2:
                    range1 = 1;
                    range2 = 50;
                    break;
                case 3:
                    range1 = 10;
                    range2 = 50;
                    break;
                case 4:
                    range1 = 10;
                    range2 = 100;
                    break;
                case 5:
                    range1 = 50;
                    range2 = 100;
                    break;
                case 6:
                    range1 = 100;
                    range2 = 500;
                    break;//is used to break the loop or the block of statemnt
            }
        }
        menu = true;

        while (menu) {
            try {
                System.out.println("How many equations you want to solve? (10, 20, 30 or 50): ");
                equation = scanner.nextInt();
            } catch (Exception ex) {
                System.out.println("Incorrect numbers. Please re-enter.\n");
                continue;
            }
            switch (equation) {
                case 10:
                case 20:
                case 30:
                case 50:
                    menu = false;
                    break;
                default:
                    System.out.println("Incorrect numbers. Please re-enter.\n");
            }
        }
         menu = true;
    System.out.println("wat do u want to solve ure equations in?:\n 1.addition\n 2.substraction\n 3.multiplication\n 4.dividion");
  equationtype=scanner.nextInt();
        for (int i = 0; i < equation; i++) {
            System.out.println("Question " + (i + 1));
            question1 = ran.random(range1, range2);
            question2 = ran.random(range1, range2);

    if (equationtype==1){
            System.out.println(question1 + " + " + question2);
            questionAnswer = add.addition(question1, question2);
            }
            else if(equationtype==2){
            System.out.println(question1 + " - " + question2);
            questionAnswer = sub.subtraction(question1, question2);
            }
            else if(equationtype==3){
                System.out.println(question1 + " * " + question2);
            questionAnswer = mult.multiplication(question1, question2);
            }
            else if(equationtype==4){
                System.out.println(question1 + " / " + question2);
            questionAnswer = divide.division(question1, question2);
            }
            else{
                System.out.println("Incorect");
    }
            menu = true;
            while (menu) {
                try {
                    System.out.print("Your answer: ");
                    answer = scanner.nextInt();
                } catch (Exception ex) {
                    System.out.println("Incorrect answer. Please re-enter.\n");
                    continue;
                }
                if (answer == questionAnswer) {
                    System.out.println("Correct!\n");
                    totalCorrect++;
                } else {
                    System.out.println("Wrong!\n");
                }
                menu = false;
            }
            }
        System.out.println("Total Questions: " + equation);
        System.out.println("Total Corrects: " + totalCorrect);
        System.out.println("Total Wrongs: " + (equation-totalCorrec
            }
    }

random.java class

import java.util.Random;
public class random {
    public int random(int ran1, int ran2) {
        Random random = new Random();
        return random.nextInt(ran2-ran1+1) + ran1;
    }
}

addition.java class

public class addition {
    public int addition(int num1, int num2) {
        return num1+num2;
    }
}

subtraction.java class

public class subtraction {
public int subtraction (int num3, int num4){
return  num3 -num4;

multiplication.java class

public class multiplication {
 public int multiplication(int num5, int num6) {
        return num5*num6;
    }
}

division.java class

public class division {
 public int division(int num7, int num8) {
        return num7/num8;
    }
}

mathmenu.java guiclass(i juss started learnin abtgui like literally yesterday, all i know is how to design the GUI and how to let the user key in- m confused how to code in this class, amd i supose to write al the loops here instead of the mathprogram.java(main class). could someone show me how to do it?)

public class mathmenu extends javax.swing.JFrame {
    public mathmenu() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        questionlabel1 = new javax.swing.JLabel();
        answer3 = new javax.swing.JTextField();
        questionlabel2 = new javax.swing.JLabel();
        answer2 = new javax.swing.JTextField();
        questionlabel3 = new javax.swing.JLabel();
        answer1 = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        questionlabel1.setText("wat grade do u study in?");

        answer3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                answer3ActionPerformed(evt);
            }
        });

        questionlabel2.setText("how many questions do u wish to solve:10, 20, 30,50?");

        answer2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                answer2ActionPerformed(evt);
            }
        });

        questionlabel3.setText("watdo u wish to  use while solving equation:+ - * /");

        answer1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                answer1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(questionlabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(answer3, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(97, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(questionlabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(89, 89, 89))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(answer2, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                        .addGap(89, 89, 89))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(questionlabel3)
                        .addContainerGap(115, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(answer1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                        .addGap(89, 89, 89))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(questionlabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(answer3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(questionlabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(answer2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(questionlabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(answer1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("tab1", jPanel1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 477, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 227, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("tab2", jPanel2);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 477, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 227, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("tab3", jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 485, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(400, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(36, Short.MAX_VALUE))
        );
        pack();
    }

    private void answer3ActionPerformed(java.awt.event.ActionEvent evt) {
     try{
        int grade= Integer.parseInt(
                this.answer3.getText());
    }
   catch(Exception ex){
    JOptionPane.showMessageDialog(this, "error!-wrong option", null,  JOptionPane.ERROR_MESSAGE);
  return;
    }
    }
     private void answer2ActionPerformed(java.awt.event.ActionEvent evt) {
         try{
        int question= Integer.parseInt(
                this.answer2.getText());
    }
   catch(Exception ex){
    JOptionPane.showMessageDialog(this, "error!-wrong option", null,  JOptionPane.ERROR_MESSAGE);
  return;
    }
     }

     private void answer1ActionPerformed(java.awt.event.ActionEvent evt) {
    try{
        int grade= Integer.parseInt(
                this.answer1.getText());
    }
   catch(Exception ex){
    JOptionPane.showMessageDialog(this, "error!-wrong option", null,  JOptionPane.ERROR_MESSAGE);
  return;
    }
     }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new mathmenu().setVisible(true);
            }
        });
    }
    private javax.swing.JTextField answer1;
    private javax.swing.JTextField answer2;
    private javax.swing.JTextField answer3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel questionlabel1;
    private javax.swing.JLabel questionlabel2;
    private javax.swing.JLabel questionlabel3;

}
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

You've posted a lot of code to go thru.
If you're just starting with GUI, perhaps it'd be better if you started with a small simple GUI, get that to work and then expand it towards what you need.

My approach to using GUI is to present the GUI, let the user key in the input and press a button and then have a listener get the user's input and do some work/computations, display the results on the GUI and exit waiting for the user's next input.

Could you explain your problem some more.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 
Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 

if you have proble with loop post here only code contains your problem, because (inc. me) nobody going to debug your problem for you, or with update to the GUI, your desription is vague

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: