我有一个具有保存,打印和退出的JMenuBar。到目前为止,我已尝试退出,但无法让它工作。我希望Exit退出系统,Print打印Total,然后Save保存到文件夹。我只需要朝着正确的方向发展。任何帮助表示赞赏。
这是我的代码:
[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]
答案 0 :(得分:2)
System.exit(0);
即使是受信任的applet也无法结束其他applet可能正在运行的JVM。至少不是直接的。
改为使用AppletContext.showDocument(URL)
。
this.getAppletContext().showDocument(thanksForUsingUrl);
将包含小程序的网页替换为thanksForUsingUrl
stop()
&应该在(最终)JVM结束之前调用页面中每个applet的destroy()
个方法。