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 语句始终默认为案例 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);
}
});