Java 如何使用JOptionPane创建菜单
我创建了一个系统类,它有不同的方法来显示计算机系统的详细信息Java 如何使用JOptionPane创建菜单,java,user-interface,switch-statement,joptionpane,Java,User Interface,Switch Statement,Joptionpane,我创建了一个系统类,它有不同的方法来显示计算机系统的详细信息 import java.util.Properties; public class System_Y3881268 { // the attributes private String make; private String model; private int speed; private int memorySize; private double hardDiskSize;
import java.util.Properties;
public class System_Y3881268
{
// the attributes
private String make;
private String model;
private int speed;
private int memorySize;
private double hardDiskSize;
private double purchaseCost;
// the methods
// the constructor
public System_Y3881268(String makeIn, String modelIn, int speedIn)
{
make = makeIn;
model = modelIn;
speed = 2;
}
// methods to set the corresponding attributes- mutator methods
public void setMemory(int memorySizeIn)
{
memorySize = memorySizeIn;
}
public void setHardDisk(double hardDiskSizeIn)
{
hardDiskSize = hardDiskSizeIn;
}
public void setPurchaseCost(double purchaseCostIn)
{
purchaseCost = purchaseCostIn;
}
// methods to return attribute values- accessor methods
public String getMake()
{
return make;
}
public String getModel()
{
return model;
}
public int getProcessorSpeed()
{
return speed;
}
//display details of the system
public void displayDetails()
{
System.out.println("*****Computer Details*****");
System.out.println("Make: " + getMake());
System.out.println("Model: " + getModel());
System.out.println("Processor speed; " + getProcessorSpeed() + "GHz");
System.out.println("Memory size: " + memorySize + " MB");
System.out.println("Hard disk size: " + hardDiskSize + " GB");
System.out.println("Purchase cost: £" + purchaseCost);
}
//check if the hard disk size is below 2GB and print corresponding message
public String checkHDStatus(double hardDiskSizeIn)
{
if(hardDiskSizeIn<2)
{
return("Low");
}
else
{
return("Ok");
}
}
//Check if the memory size is below 128MB and print corresponding message
public boolean goodMemorySize(int memorySizeIn)
{
if(memorySizeIn<128)
{
return false;
}
else
{
return true;
}
}
/*use the checkHDStatus() method and the goodMemorySize() method to diagnose the
* system by displaying the appropriate messages*/
public void diagnoseSystem()
{
System.out.println();
System.out.println("*****System Diagnosis*****");
System.out.println("Hard disk size = " + checkHDStatus(hardDiskSize));
System.out.println("Memory size Ok = " + goodMemorySize(memorySize));
}
//method to display system properties
public static void displaySystemProperties()
{
Properties pros = System.getProperties();
pros.get(System.out);
System.out.println();
System.out.println("*****System Properties*****");
System.out.println("Operating System Architecture: " + System.getProperty("os.arch"));
System.out.println("Operating System Name: " + System.getProperty("os.name"));
System.out.println("Operating System Version: " + System.getProperty("os.version"));
System.out.println("User Account Name: " + System.getProperty("user.name"));
System.out.println("Java Version: " + System.getProperty("java.version"));
System.out.println();
if(System.getProperty("os.name").equals("Windows 10"))
{
System.out.println("Thumbs up! Your operating system is Windows 10");
}
else if(System.getProperty("os.name").equals("Linux"))
{
System.out.println("Thumbs down! Your operating system is Linux");
}
else
{
System.out.println("Your choice of operating system is ok");
}
}
}
import java.util.Properties;
公共类系统_Y3881268
{
//属性
私人字符串制作;
私有字符串模型;
私人整数速度;
私有内存化;
私人双硬盘大小;
私人双重购买成本;
//方法
//构造器
公共系统_Y3881268(字符串生成、字符串建模、整数速度输入)
{
make=makeIn;
model=modelIn;
速度=2;
}
//方法来设置相应的属性-mutator方法
公共void setMemory(int memorySizeIn)
{
memorySize=memorySizeIn;
}
公共无效设置硬盘(双硬盘大小)
{
硬盘大小=硬盘大小;
}
public void setPurchaseCost(双purchaseCostIn)
{
purchaseCost=purchaseCostIn;
}
//返回属性值的方法-访问器方法
公共字符串getMake()
{
退货;
}
公共字符串getModel()
{
收益模型;
}
public int getProcessorSpeed()
{
返回速度;
}
//显示系统的详细信息
公开资料
{
System.out.println(“******计算机详细信息*****”);
System.out.println(“Make:+getMake());
System.out.println(“Model:+getModel());
System.out.println(“处理器速度;”+getProcessorSpeed()+GHz”);
System.out.println(“内存大小:+memorySize+MB”);
System.out.println(“硬盘大小:+硬盘大小+”GB”);
系统输出打印项次(“采购成本:£”+采购成本);
}
//检查硬盘大小是否低于2GB,并打印相应消息
公共字符串checkHDStatus(双硬盘大小)
{
如果(硬盘大小1)input
是在do
中声明的,因此,当它到达时,
就不再存在了。2)参见3)在switch case中不需要这些花括号,如果您正确缩进代码,那么在没有花括号的情况下应该很容易理解。4)为什么要在Runnable
对象中创建方法你说4是什么意思?我是一个完全的新手,不知道我在做什么public void run()
和private void createGUI()
都在new Runnable(){…}
和main(…)
method。这是一种错误的程序编写方式。层次结构应该是Class>Methods
而不是Class>Methods>Methods
。将这两种方法放在main
之外。然后在此处编辑代码以显示所做的改进
import javax.swing.JOptionPane;
import com.sun.javafx.binding.MapExpressionHelper.SimpleChange;
public class SystemTestGUI_Y3881268
{
javax.swing.SwingUtilities.@invokeLater(new Runnable() {
private StringBuffer input;
public void run() {
createGUI();
}
private void createGUI() {
do {
String[] choices = { "Select...", "1", "2", "3", "4", "5" };
String input = (String) JOptionPane.showInputDialog(null, "Select a choice\n1: Print System Details\n2: Diagnose "
+ "System\n3: Set Details\n4: Print System Properties\n5: Quit the Program",
"Computer System Menu", JOptionPane.INFORMATION_MESSAGE, null, choices, choices[0]);
System_Y3881268 s;
switch(input)
{
case "1":
s.displayDetails();
break;
case "2":
s.diagnoseSystem();
break;
case "3":
JOptionPane.showInputDialog(null, "Enter hard disk size in GB: ");
double hardDiskSize = 0;
s.setHardDisk(hardDiskSize);
if(hardDiskSize<2)
{
JOptionPane.showMessageDialog(null, "Hard disk size = Low");
}
else
{
JOptionPane.showMessageDialog(null, "Hard disk size = Ok");
}
JOptionPane.showInputDialog(null, "Enter memory size in MB: ");
int memorySize = 0;
s.setMemory(memorySize);
if(memorySize<128)
{
JOptionPane.showMessageDialog(null, "Memory Ok = False");
}
else
{
JOptionPane.showMessageDialog(null, "Memory Ok = True");
}
break;
case "4":
System_Y3881268.displaySystemProperties();
break;
case "5": break;
default: JOptionPane.showMessageDialog(null, "Enter only numbers from 1 - 5");
}
}while (!"5".contentEquals(input)
);}
public static void main(String[] args) {
System_Y3881268 s=new System_Y3881268("Lenovo",
"Ideacentre A340-24IWL", 2);
s.setHardDisk(2);
s.setMemory(128);
s.setPurchaseCost(599);
s.displayDetails();
s.diagnoseSystem();
System_Y3881268.displaySystemProperties();
}
}));
}