本文的代码在windows下和linux下均测试通过:
Java代码:
packagecom.pinus.util;
importjava.awt.Container;
importjava.awt.FlowLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JTextField;
importjavax.swing.UIManager;
publicclassCalculator {
privateJFrame jFrame;
privateJLabel firstLabel, secondLabel, resultLabel;
privateJTextField firstTextField, secondTextField, resultTextField;
privateCounter counter;
publicCalculator() {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e) {
System.err.println("-----");
}
jFrame =newJFrame("Calculator");
counter =newCounter();
Container container = jFrame.getContentPane();
container.setLayout(newFlowLayout());
firstLabel =newJLabel();
firstTextField =newJTextField(15);
firstTextField.addActionListener(counter);
firstLabel.setText(" FirstNumber:");
secondLabel =newJLabel();
secondTextField =newJTextField(15);
secondLabel.setText("SecondNumber:");
secondTextField.addActionListener(counter);
resultLabel =newJLabel();
resultTextField =newJTextField(15);
resultLabel.setText(" Total:");
container.add(firstLabel);
container.add(firstTextField);
container.add(secondLabel);
container.add(secondTextField);
container.add(resultLabel);
container.add(resultTextField);
jFrame.setResizable(false);
jFrame.setLocationRelativeTo(null);
jFrame.setSize(200,140);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
publicclassCounterimplementsActionListener {
publicvoidactionPerformed(ActionEvent e) {
intfirst = Integer.parseInt(firstTextField.getText().trim());
intsecond = Integer.parseInt(secondTextField.getText().trim());
resultTextField.setText(first + second +"");
}
}
publicstaticvoidmain(String[] args)throwsException {
newCalculator();
}
}
windows下打开命令提示符:
1. 编译 :javac -d . Calculator.java
2. 执行 :java com.pinus.util.Calculator
3. 因此我们可以把执行的命令写为批处理文件(run.bat)
其内容为:java com.pinus.util.Calculator
注意:要把此bat放在与com同一级文件夹下
4.新建一文件夹bin,把编译后的.class文件(带上其所在文件夹)和批处理文件.bat
在此文件件夹下
5.建一文件start.bat(与bin文件夹处于同一级文件夹下)内容为:cd bin
start run.bat
linux下打开终端:
1. 编译 :javac -d . Calculator.java
2. 执行 :java com.pinus.util.Calculator
3. 因此我们可以把执行的命令写为shell脚本文件(run.sh)
其内容为:#!/bin/sh
java com.pinus.util.Calculator
注意:要把此sh脚本放在与com同一级目录下
4.新建一目录bin,把编译后的.class文件(带上其目录)和shell脚本文件.sh放在此目录下
5.建一文件start.sh(与bin目录处于同一级目录下)内容为:cd bin
sh run.sh