gpt4 book ai didi

java - JMenu 栏项目(退出)不起作用

转载 作者:行者123 更新时间:2023-11-30 04:07:14 25 4
gpt4 key购买 nike

我有一个 JMenuBar,它具有保存、打印和退出功能。到目前为止,我已尝试退出,但无法使其发挥作用。我希望“退出”退出系统,“打印”打印总计,“保存”保存到文件夹。我只需要一个正确的方向。如有任何帮助,我们将不胜感激。

这是我的代码:

[code]
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;

public class cousinsTree extends JApplet implements ActionListener
{
Container Panel;
JMenuBar mnuBar;
JMenuItem mnuExit;
JMenuItem mnuPrint;
JMenuItem mnuSave;
JButton submitButton;
JButton clearButton;
JTextField firstName;
JTextField lastName;
JTextField Address;
JTextField City;
JTextField Total;
JComboBox Service;
JComboBox howOften;
JComboBox numTrees;
LayoutManager setLayout;
String[] TreeList;
String[] numList;
String[] oftenList;

/**
*
*/
public void init()
{
Panel = getContentPane();
this.setLayout(new FlowLayout());
TreeList= new String[3];
TreeList [0] = "Trim";
TreeList [1] = "Chemical Spray";
TreeList [2] = "Injection";
numList = new String[3];
numList [0] = "0-5";
numList [1] = "6-10";
numList [2] = "11 >";
oftenList = new String[3];
oftenList [0] = "Monthly";
oftenList [1] = "Quarterly";
oftenList [2] = "Annually";
Panel.setBackground (Color.green);
submitButton = new JButton("Submit");
submitButton.addActionListener(this);
submitButton.setPreferredSize(new Dimension(100,30));
clearButton = new JButton("Clear");
clearButton.addActionListener(this);
clearButton.setPreferredSize(new Dimension(100,30));
firstName = new JTextField("", 10);
JLabel lblFirstName = new JLabel("First Name");
lastName = new JTextField("", 10);
JLabel lblLastName = new JLabel("Last Name");
Address = new JTextField("", 15);
JLabel lblAddress = new JLabel("Address");
City = new JTextField("Columbus", 10);
JLabel lblCity = new JLabel("City");
Total = new JTextField("", 10);
JLabel lblTotal = new JLabel("Total");

//Service = new TextField("Service (Trim, Chemical Spray, or Injection).", 20);
JLabel lblService = new JLabel("Service");
Service=new JComboBox(TreeList);

JLabel lblhowOften = new JLabel("How often?");
howOften = new JComboBox(oftenList);

JLabel lblnumTrees = new JLabel("Number of Trees");
numTrees = new JComboBox(numList);

/* Configuration */
//add items to panel
Panel.add(lblFirstName);
Panel.add(firstName);
Panel.add(lblLastName);
Panel.add(lastName);
Panel.add(lblAddress);
Panel.add(Address);
Panel.add(lblCity);
Panel.add(City);
Panel.add(lblnumTrees);
Panel.add(numTrees);
Panel.add(lblService);
Panel.add(Service);
Panel.add(lblhowOften);
Panel.add(howOften);
Panel.add(submitButton);
Panel.add(clearButton);
Panel.add(lblTotal);
Panel.add(Total);



this.setSize(new Dimension(375, 275));
this.setLocation(0,0);

Service.setSelectedIndex (0);
howOften.setSelectedIndex (0);
numTrees.setSelectedIndex (0);

JMenuBar mnuBar = new JMenuBar();
setJMenuBar(mnuBar);

JMenuItem mnuSave = new JMenu("Save", true);
mnuSave.setMnemonic(KeyEvent.VK_S);
mnuSave.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuSave);
mnuSave.addActionListener(this);

JMenuItem mnuPrint = new JMenu("Print", true);
mnuPrint.setMnemonic(KeyEvent.VK_P);
mnuPrint.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuPrint);
mnuPrint.addActionListener(this);

JMenuItem mnuExit = new JMenu("Exit", true);
mnuExit.setMnemonic(KeyEvent.VK_X);
mnuExit.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuExit);
mnuExit.addActionListener(this);
}


@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == clearButton) {

firstName.setText("");
lastName.setText("");
Address.setText("");
City.setText("");
Total.setText("");
}

if(e.getSource()== submitButton) {
int Selection;
int timesPerYear = 0;
int serviceCost = 0;
double rate = 0, serviceRate = 0;
double result = 0;

Selection = howOften.getSelectedIndex();
if(Selection == 0) {
timesPerYear = 12;
} else if (Selection == 1) {
timesPerYear = 4;
} else if (Selection == 2) {
timesPerYear = 1;
}
Selection = Service.getSelectedIndex();
if(Selection == 0) {
serviceCost = 20;
} else if (Selection == 1) {
serviceCost = 25;
} else if (Selection == 2) {
serviceCost = 30;
}
Selection = numTrees.getSelectedIndex();
if(Selection == 0) {
rate = 5;
} else if(Selection == 1) {
rate = 10;
} else if(Selection == 2) {
rate = 15;
}

DecimalFormat twoDigits = new DecimalFormat("$#,###.00");
result = (serviceCost+rate)*timesPerYear;
Total.setText("" + twoDigits.format(result) + "");
}
if(e.getSource() == mnuExit) {
System.out.print("Exiting");
System.exit(0);
}
}
}
[/code]

最佳答案

System.exit(0);

即使是受信任的小程序也无法结束其他小程序可能正在其中运行的 JVM。至少不能直接结束。

使用AppletContext.showDocument(URL)相反。

this.getAppletContext().showDocument(thanksForUsingUrl);

通过用 thanksForUsingUrl 替换包含小程序的页面,页面中每个小程序的 stop()destroy() 方法应该是在(最终)JVM 结束之前调用。

关于java - JMenu 栏项目(退出)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20448999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com