This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2017/9/24

quadratic gui calculator that wont pop up

divinity divinity

2017/9/24

#
hi danpost I am currently trying to build a quadratic calculator from scratch instead of the usual drag and drop, but for some reason when I ran it the gui interface down pop up like it should, dont know what i have done wrong or if the logic I use is correct. I am totally lost and i dont know what to do again. can u help me out here. here is the codes that I use:
package tester;




import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Tester extends JFrame implements ActionListener {

         JTextField factorA = new JTextField("Factor A");
         JTextField factorB = new JTextField("Factor B");
         JTextField factorC = new JTextField("Factor C");
         JTextField solutions = new JTextField("solutions");
         JButton compute= new JButton("compute");
         JButton exits = new JButton("exits");
         JButton clear = new JButton("Clear");
         Tools tls = new Tools();
    
       
    
    public Tester(){
        
        this.setTitle("Quadratic Calculator");
        this.setSize(300, 300);
        this.setLayout(null);
        
        this.getContentPane().add(factorA);
        factorA.setBounds(85, 100, 100, 30);
        
        this.getContentPane().add(factorB);
        factorB.setBounds(190, 100,100, 20);
        
        this.getContentPane().add(factorC);
        factorC.setBounds(295, 100, 100, 20);
        
        this.getContentPane().add(compute);
        compute.setBounds(200, 100, 100, 25);
        
        this.getContentPane().add(solutions);
        solutions.setBounds(295, 100, 100, 25);
        
        this.getContentPane().add(clear);
        clear.setBounds(295, 100, 100,25);
        exits.addActionListener(this);
        compute.addActionListener(this);
        clear.addActionListener(this);
        this.setVisible(true);
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
        if(e.getActionCommand().equalsIgnoreCase("exits"))
         System.exit(0);
            
          if(e.getActionCommand().equalsIgnoreCase("clear"))
           tls.clear(factorA, factorB, factorC);
                
            if(e.getActionCommand().equalsIgnoreCase("compute"))
             compute.setText(tls.addvalues(factorA, factorB, factorC));
                  
              
     and here is the output what i get 
run:
BUILD SUCCESSFUL (total time: 0 seconds)
danpost danpost

2017/9/25

#
Code missing: Tester class method 'compute' and Tools class methods 'clear' and 'addValues'.
divinity divinity

2017/9/25

#
how do i do that, I mean do I have to make a compute and clear and addvalues methods can u show me how plz thanks
danpost danpost

2017/9/25

#
divinity wrote...
how do i do that, I mean do I have to make a compute and clear and addvalues methods can u show me how plz thanks
If the build was successful, then you must already have those methods as they are being called in your code above at lines 60 and 63. What I meant by them being missing is that you did not provide them here in the discussion.
divinity divinity

2017/9/25

#
here it is
import javax.swing.JTextField;

/**
 *
 * @author luanataylor
 */
public class Tools {
    
    public void clear(JTextField fa1, JTextField fa2, JTextField fa3 ){
        
        fa1.setText(null);
        fa2.setText(null);
        fa3.setText(null);
        
    }
    public String addvalues(JTextField fa1, JTextField fa2, JTextField fa3){
        
        int factorA=0, factorB=0, factorC =0;
        factorA = Integer.parseInt(fa1.getText());
        factorB = Integer.parseInt(fa2.getText());
        factorC = Integer.parseInt(fa3.getText());
        int compute = factorA + factorB + factorC;
        return String.valueOf(compute);
    }
}
danpost danpost

2017/9/25

#
My bad! I did not need these methods as you said the GUI is not showing. I need to see the code to the class where you call a Tester object into creation (where you have 'new Tester'). It is probably in the class where your 'main' method is.
divinity divinity

2017/9/25

#
here it is
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tester;

/**
 *
 * @author luanataylor
 */
public class Driver {
    
    
     public static void main(String[] args) {
      
         
          Tester test = new Tester();
    }
    
}
danpost danpost

2017/9/25

#
No problems there !! It is not at all obvious to me why you would not have a visible Tester object provided that the 'main' method is the Driver class executes.
You need to login to post a reply.