摘要:
1、SwingLearn是主程序的类
2、Main-Class: SwingLearn (Main-Class:[空格][主程序类])
3、jar -cvmf myfile.mf myjar.jar *.class:打包生成jar文件
4、 java -jar myjar.jar 运行jar文件验证
程序代码:
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax . Swing .*;
public class SwingLearn {
public static void main(String[] args) {
//实例化 JFrame
JFrame frame = new JFrame();
//设置相关属性
frame.setTitle("Swing学习");//标题
frame.setSize(300,300);//窗体大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击关闭按钮是关闭程序
frame.setLocationRelativeTo(null); //设置居中
frame.setResizable(false); //不允许修改界面大小
//获取容器
Container container = frame.getContentPane();
frame.setLayout(null);
//创建按钮
JButton jButton = new JButton("我是按钮");
/*
使用addActionListener(ActionListener l)方法为jButton(按钮)添加监听事件
实现addActionListener里的参数接口ActionListener,重写ActionListener接口的actionPerformed方法
如果发生了点击事件,系统将会 回调 actionPerformed方法
*/ jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
// JOptionPane.showInputDialog("Please input a value");
JOptionPane.showMessageDialog(null,"Hello world");
}
});
jButton.setBounds(100, 100, 100, 40);
//按钮添加到容器中
container.add(jButton);
//设置显示
frame.setVisible(true);
}
}
myfile.mf
Main-Class: SwingLearn
生成 jar 的代码
jar -cvmf myfile.mf myjar.jar *.class
运行jar文件
java -jar myjar.jar