Note that there are some explanatory texts on larger screens.

plurals
  1. POWorking with JFrames
    primarykey
    data
    text
    <p>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!</p> <p><a href="http://i.stack.imgur.com/xHIQF.png" rel="nofollow">Here's is what the output should look like</a></p> <p>And here is my code so far. it's a bit messed up but any help would be much appreciated. Thanks in advance</p> <pre><code> 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 &gt; 1 &amp;&amp; shape == "Circle") { // label2.getText() = "Enter the radius of the circle: "; Area = (Math.PI * (input * input)); } } else{} } } ); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload