package messingaround;
import java.awt.*;
import javax.swing.*;
import messingaround.Secondary;
public class Main {
public static void main (String[] args){
Runnable r = new Runnable() {
@Override
public void run() {
Object[] options = {
"Circle Area",
"Triangle Area",
"Square/Rectangle Area",
"Circle Circumference",
"Cube/Rectangular Prism Volume",
"Cylinder Volume",
"Pyramid Volume"
};
JComboBox shapePicker = new JComboBox(options);
下面是带有下拉菜单的实际GUI
String shapechoice = (String) JOptionPane.showInputDialog(
null,
"Choose from the following options...",
"Formula Calculator",
JOptionPane.QUESTION_MESSAGE, null,
options, // Array of menuChoices
options[0]); // Initial choice
int shapepick = shapePicker.getSelectedIndex();
Secondary CalculationsObject = new Secondary();
这是我遇到问题的地方,switch语句总是默认为case 0;
switch(shapepick) {
它始终默认为大小写0
case 0:
CalculationsObject.CircleArea();
break;
case 1:
CalculationsObject.TriangleArea();
break;
case 2:
CalculationsObject.RectangleArea();
break;
case 3:
CalculationsObject.CircleCircumference();
break;
case 4:
CalculationsObject.CubeVolume();
break;
case 5:
CalculationsObject.CylinderVolume();
break;
case 6:
CalculationsObject.PyramidVolume();
break;
default:
break;
}
}
};
旁注:如果它涉及invokeLater的事情,我需要解释invokeLater实际上做了什么,因为我不太熟悉它。
SwingUtilities.invokeLater(r);
}
}
这是下面的下一课。此类计算形状的面积/体积。我删除了一些方法,因为它们不是必需的或涉及到问题。
package messingaround;
import javax.swing.JOptionPane;
public class Secondary {
public static void CircleArea(){
String arearadiusdefine;
double arearadius, circlearea, arearadiussquared;
arearadiusdefine = JOptionPane.showInputDialog("What is the radius of the circle?");
arearadius = Double.parseDouble(arearadiusdefine);
arearadiussquared = arearadius * arearadius;
circlearea = arearadiussquared * 3.14159265359;
JOptionPane.showMessageDialog(null, "The area of the circle is: " +circlearea, "Formula Calculator", JOptionPane.PLAIN_MESSAGE);
}
public static void TriangleArea(){
String trianglebasedefine, triangleheightdefine;
double trianglebase, triangleheight, trianglearea;
trianglebasedefine = JOptionPane.showInputDialog("What is the base");
triangleheightdefine = JOptionPane.showInputDialog("What is the height?");
triangleheight = Double.parseDouble(triangleheightdefine);
trianglebase = Double.parseDouble(trianglebasedefine);
trianglearea = trianglebase * triangleheight * 0.5;
JOptionPane.showMessageDialog(null, "The area of the triangle is: "
+trianglearea, "Formula Calculator", JOptionPane.PLAIN_MESSAGE);
}
因为你还没有将回调附加到你的JComboBox
,所以你的switch
代码只在启动时运行一次。
首先将你的switch
重构为一个重用函数:
public void onOptionChange(int newOption) {
switch(newOption) {
case 0:
CalculationsObject.CircleArea();
break;
case 1:
CalculationsObject.TriangleArea();
break;
case 2:
CalculationsObject.RectangleArea();
break;
case 3:
CalculationsObject.CircleCircumference();
break;
case 4:
CalculationsObject.CubeVolume();
break;
case 5:
CalculationsObject.CylinderVolume();
break;
case 6:
CalculationsObject.PyramidVolume();
break;
default:
break;
}
}
定义“onOptionChange”侦听器:
public void optionChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
// For each item change, you got two events: One for Deselect and the other for DESELECT
int option = shapePicker.getSelectedIndex();
// Invoke change
onOptionChange(option);
}
}
然后附加一个事件监听器:
JComboBox shapePicker = new JComboBox(options);
onOptionChange(0); // Default first option select.
shapePicker.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
optionChanged(e);
}
});