Ikemesit Ansa
Ikemesit Ansa

Reputation: 133

Working with JFrames

hi I'm basically give up. Ok so this is what I am trying to do. So far I have written code to create a JFrame which contains a text field and combo box. The code is supposed to calculate the area of an input in the text field depending on what shape is selected from the combo box and output the result on the JFrame!

Here's is what the output should look like

And here is my code so far. it's a bit messed up but any help would be much appreciated. Thanks in advance

 import javax.swing. *;
 import java.awt.event. *;   
 import java.awt.FlowLayout;
 import java.lang.Math; 
 public class AreaFrame3  extends JFrame
 {  

     double Area;
     double input;

 public static void main(String[]args)
 {
   //Create array containing shapes
   String[] shapes ={"(no shape selected)","Circle","Equilateral Triangle","Square"};

   //Use combobox to create drop down menu
   JComboBox comboBox=new JComboBox(shapes);
   JLabel label1 = new JLabel("Select shape:");
   JPanel panel1 = new JPanel(new FlowLayout()); //set frame layout

   JLabel label2 = new JLabel("(select shape first)");
   JTextField text = new JTextField(10); //create text field
   text.setEnabled(false);



   panel1.add(label1);
   panel1.add(comboBox);
   panel1.add(label2);
   panel1.add(text);



   JFrame frame=new JFrame("Area Calculator Window");//create a JFrame to put combobox
   frame.setLayout(new FlowLayout()); //set layout
   frame.add(panel1);
   frame.add(text);
   //JButton button = new JButton("GO"); //create GO button
   //frame.add(button);

   //set default close operation for JFrame
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.pack();
   //set JFrame ssize
   frame.setSize(400,250);

   //make JFrame visible. So we can see it
   frame.setVisible(true);


   // public void actionPerformed(ActionEvent e)
    //{

  }

 public void AreaCalc()
 {
    JButton button = new JButton("GO"); //create GO button
   frame.add(button);
   button.addActionListener(
      new ActionListener(){
         public void actionPerformed(ActionEvent e)
        {
           int input = double.parseDouble(text.getText());


        if(e.getSource() == button)
        {              
            String shape = (String).comboBox.getSelectedItem(); 

           if(shape == "(no shape selected)")
           {
             text.setEnabled(false);
           }

          else{
              text.setEnabled(true);
          }

          if(input > 1 && shape == "Circle")
          {
             // label2.getText() = "Enter the radius of the circle: ";
              Area = (Math.PI * (input * input));

          }
        }

         else{}
       }
     }         
    );
  } 
}            

Upvotes: 0

Views: 405

Answers (2)

Maxim Shoustin
Maxim Shoustin

Reputation: 77904

I try to understand what you did here:

panel1.add(label1);
panel1.add(comboBox);
panel1.add(label2);
panel1.add(text); // <---


JFrame frame=new JFrame("Area Calculator Window");//create a JFrame to put combobox
frame.setLayout(new FlowLayout()); //set layout
frame.add(panel1);
frame.add(text); // <---

Especially frame.add(text); and panel1.add(text);. Don't add text to JFrame. Use JPanel.

Further,

public class AreaFrame3  extends Frame

Use public class AreaFrame3 extends JFrame so you don't need create additional JFrame:

JFrame frame=new JFrame("Area Calculator Window");

Something like:

super.setLayout(new FlowLayout()); //set layout
super.add(panel1); 

super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.pack();
//set JFrame ssize
super.setSize(400,250);

//make JFrame visible. So we can see it
super.setVisible(true);

At last Ill give you some tamplate to start with (that will help you):

public class FrameExmpl extends JFrame{

private static final long serialVersionUID = 1L;

private        JTabbedPane       tabbedPane;        
private        JPanel            topPanel;
private JTextField               txtf_loadDS_;


public static int valueInt = 0; // responsible for Task status updating
public static Boolean isFinish = false;


public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{ 

    UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );

    FrameExmpl UI_L = new FrameExmpl();

    UI_L.buildGUI();

    UI_L.setVisible(true);
}


public void buildGUI(){

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            dispose();
        }
    });

    setSize(435, 225);
    setLocation(285, 105);  

    setResizable(false);

    topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());
    getContentPane().add(topPanel);

    txtf_loadDS_ = new JTextField();                
    txtf_loadDS_.setBounds(22, 22, 382, 25);
    topPanel.add(txtf_loadDS_);


    finishBuildGUI();
}


public void finishBuildGUI(){       
    tabbedPane = new JTabbedPane();     
    topPanel.add(tabbedPane, BorderLayout.CENTER);
}
}

enter image description here

Upvotes: 3

Reimeus
Reimeus

Reputation: 159774

There are multiple issues with this application such as extending from Frame rather than JFrame & attempting to assign an int from Double.parseDouble. I would recommend that you start again building a small but working application and incrementally add functionality, this way errors are easier to fix.

Upvotes: 2

Related Questions