Java JMenu工具栏项(退出)不工作
我有一个JMenuBar,它具有保存、打印和退出功能。到目前为止,我已尝试退出,但无法使其工作。我希望退出以退出系统,打印以打印总数,保存以保存到文件夹。我只需要一个方向正确的牛排。感谢您的帮助 这是我的密码:Java JMenu工具栏项(退出)不工作,java,swing,applet,japplet,jmenuitem,Java,Swing,Applet,Japplet,Jmenuitem,我有一个JMenuBar,它具有保存、打印和退出功能。到目前为止,我已尝试退出,但无法使其工作。我希望退出以退出系统,打印以打印总数,保存以保存到文件夹。我只需要一个方向正确的牛排。感谢您的帮助 这是我的密码: [code] import java.awt.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; import java.text.Decim
[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]
即使是受信任的小程序也无法终止其他小程序可能正在运行的JVM。至少不是直接的
改用
通过将包含小程序的页面替换为
Thanksforsingurl
,页面中每个小程序的stop()
和destroy()
方法应该在JVM结束之前调用。我不确定您希望和小程序如何“退出”?我对这一点不熟悉,所以我也不确定。我只需要在菜单栏上打印并保存最终产品。如果您希望人们查看您的代码,请遵循Java变量命名约定。变量名称不应以大写字符开头。小程序在由浏览器控制的自包含沙盒中运行。基本上,除非对其进行了签名,并且客户机已设置为允许,否则您可能无法读取/写入本地磁盘,甚至无法访问打印机……我正在上第一堂java课,我的老师已经多次查看了我的代码,但什么也没说。我正在尽力,只是想寻求帮助。我只是需要你帮我弄一下JMenuBar。我的老师对其他一切都满意。
System.exit(0);
this.getAppletContext().showDocument(thanksForUsingUrl);