帮助别人,提升自己。
一个同学发来的江湖救急函>>>
问题如下:
虽然之前没接触,但不能打击咱干饭人的信心,于是翻开书现学现卖,或许有许多不足希望老哥们多多指教,嘻嘻。
代码如下:
package com.baidou;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* Created by baidou on 2020/12/3.
*/
public class PanelTest {
public static void main(String[] args) {
System.out.println("-------欢迎使用乘法计算器-------");
Frame frame = new Frame("简单的乘法计算器"); // 获取窗口
// 获取Panel容器
Panel panel = new Panel();
//创建控件
final TextField textField1 = new TextField(15);
final TextField textField2 = new TextField(15);
final TextField textField3 = new TextField(15);
Button button1 = new Button("*");
Button button2 = new Button("=");
//给按钮设置监听事件
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("我被点击");
//获取文本框数值
double num1 = Double.valueOf(textField1.getText());
//获取文本框数值
double num2 = Double.valueOf(textField2.getText());
double result = (num1 * num2);
System.out.println("输出:" + result);
textField3.setText(String.valueOf(result));
}
});
/**
* 关闭窗体事件
*/
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("用户窗体已关闭");
System.exit(0);
}
});
//添加组件
panel.add(textField1);
panel.add(button1);
panel.add(textField2);
panel.add(button2);
panel.add(textField3);
frame.add(panel);//将容器添加窗体内
frame.setBounds(200, 300, 600, 100); //设置窗体位置与大小:(x,y,w,h)
frame.setVisible(true); //是否展示
}
}
运行效果:
碰到的问题:
Maven打包没有找到主类(不是SpringBoot项目)
解决如下:
向pom.xml中添加如下代码
<build>
<!-- 设置jar的名字 -->
<finalName>awt_demo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!-- 配置主类位置 -->
<mainClass>com.baidou.PanelTest</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
通过命令运行jar:
java -jar xxx.jar