# 1.java GUI 编程(基于 swing 组件)
# 1. 首先要创建顶层容器,通常使用 JFrame
- 首先要创建 JFrame 对象
头文件:
import javax.swing.*; |
实例化 JFrame
类:
JFrame root =new JFrame(); // 默认没有标题 | |
JFrame root =new JFrame("求平方根");// 标题为 “求平方根” |
此时已经实例化对象,但是框架窗口默认不可见,需要手动设置:
root.setVisible(true); // 设置窗口可见 |
2. 设置窗口大小
root.setSize(200,300)// 宽,高 |
# 2. 创建面板(为了管理组件)
头文件:
import java.awt.*; |
定义参数
Panel panel1,panel2; |
实例化
JPanel jpanel=new JPanel();// 默认使用 FlowLayout(从左到右从上到下布局) |
设置背景颜色
jpanel1.setBackground(Color.ORANGE); | |
jpanel2.setBackground(Color.yellow); |
重置框体的布局管理器 (没有这个会影响显示,jpanel1 不显示)
this.setLayout(new FlowLayout()); |
设置框架的窗口标题
setTitle("一元二次方程求根"); |
# 3. 创建文本域
定义
JTextField a,b,c; |
实例化
JTextField a=new JTextField(10);// 10 为文本域列长 | |
JTextField b=new JTextField(10); | |
JTextField c=new JTextField(10); |
创建输出结果(根)的文本域
JTextArea root1,root2; |
实例化
JTextArea root1=new JTextArea(String.valueOf(10)); | |
JTextArea root2=new JTextArea(String.valueOf(10)); |
# 4. 创建标签
定义
JLabel in,result; |
实例化
Label in=new JLabel("输入系数 a,b,c"); | |
JLabel result=new JLabel("两个根"); |
# 5. 添加组件进入面板
jpanel1:
jpanel1.add(in); | |
jpanel1.add(a); | |
jpanel1.add(b); | |
jpanel1.add(c); |
root1.setEditable(false);// 禁止编辑 | |
root2.setEditable(false); | |
jpanel2.add(result); | |
jpanel2.add(root1); | |
jpanel2.add(root2); |
# 6. 将面板加入框架
this.add(jpanel1); | |
this.add(jpanel2); | |
this.add(jpanel3); |
# 7. 设置按钮
定义
JButton cal,ex; |
实例化
JButton cal=new JButton("求根"); | |
JButton ex=new JButton("退出"); |
# 8. 设置事件监听器
头文件
import java.awt.event.ActionListener; |
监听按钮
cal.addActionListener(this); | |
ex.addActionListener(this); |
# 9. 进行事件处理(当 a=0 时抛出异常)
头文件
import java.awt.event.ActionEvent;
构造方法
public void actionPerformed(ActionEvent event){ | |
} |
获得文本框内容
String a1=a.getText(); | |
String b1=b.getText(); | |
String c1=c.getText(); |
判断点击 "求根" 时进行计算
double a2 = Double.parseDouble(a.getText()); | |
double b2 = Double.parseDouble(b.getText()); | |
double c2 = Double.parseDouble(c.getText()); | |
double discriminant = b2 * b2 - 4 * a2 * c2; | |
if (discriminant > 0) { | |
root1.setText(String.valueOf((-b2 - sqrt(discriminant)) / (2 * a2))); | |
root2.setText(String.valueOf((-b2 + sqrt(discriminant)) / (2 * a2))); | |
} else if (discriminant == 0) { | |
double r = -b2 / (2 * a2); | |
root1.setText(String.valueOf(r)); | |
root2.setText(String.valueOf(r)); | |
} else { | |
root1.setText("无实根"); | |
root2.setText("无实根"); | |
} |
# 完整代码
自己写的,一开始没成功
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.KeyListener; | |
import static java.lang.Math.sqrt; | |
import static java.lang.System.exit; | |
public class Myjframe extends JFrame implements ActionListener { | |
private Panel panel1,panel2,panel3,panel4; | |
private JTextField a,b,c; | |
private JTextArea root1,root2; // 不能忘记定义,不然后续事件无法处理,且与上面的文本域不同 | |
private JLabel in,result; | |
private JButton cal,ex; | |
public Myjframe(){ | |
JPanel jpanel1=new JPanel(); | |
JPanel jpanel2=new JPanel(); | |
JPanel jpanel3=new JPanel(); | |
//JPanel jpanel4=new JPanel(); | |
jpanel1.setBackground(Color.white); | |
jpanel2.setBackground(Color.yellow); | |
this.setLayout(new FlowLayout()); // 重置框体的布局管理器 | |
setTitle("一元二次方程求根"); | |
a = new JTextField(10); | |
b = new JTextField(10); | |
c = new JTextField(10); | |
JTextArea root1=new JTextArea(3,5); | |
JTextArea root2=new JTextArea(3,5); | |
JLabel in=new JLabel("输入系数 a,b,c"); | |
JLabel result=new JLabel("两个根"); | |
// 组件加入面板 | |
jpanel1.add(in); | |
jpanel1.add(a); | |
jpanel1.add(b); | |
jpanel1.add(c); | |
//root1.setEditable (false);// 禁止编辑 | |
//root2.setEditable(false); | |
jpanel2.add(result); | |
jpanel2.add(root1); | |
jpanel2.add(root2); | |
// 示例化按钮 | |
JButton cal=new JButton("求根"); | |
JButton ex=new JButton("退出"); | |
// 监听按钮 | |
//cal.addActionListener(this); | |
//ex.addActionListener(this); | |
// 将按钮加入面板 | |
jpanel3.add(cal); | |
jpanel3.add(ex); | |
// 将面板加入框架 | |
this.add(jpanel1); | |
this.add(jpanel2); | |
this.add(jpanel3); | |
cal.addActionListener(this); | |
ex.addActionListener(this); | |
this.setDefaultCloseOperation(EXIT_ON_CLOSE); | |
} | |
// 进行事件处理 | |
public void actionPerformed(ActionEvent event){ | |
// 获得文本框内容 | |
String a1=a.getText(); | |
String b1=b.getText(); | |
String c1=c.getText(); | |
if(event.getSource()==cal){ | |
double a2=Double.parseDouble(a1);// 将 string 转为 double 类型 | |
double b2=Double.parseDouble(b1); | |
double c2=Double.parseDouble(c1); | |
if(b2*b2-4*a2*c2>0){ | |
double r1=((-b2)-sqrt(b2*b2-4*a2*c2))/(2*a2); | |
double r2=((-b2)+sqrt(b2*b2-4*a2*c2))/(2*a2); | |
root1.setText(String.valueOf(r1)); | |
root2.setText(String.valueOf(r2)); | |
} | |
if((b2*b2-4*a2*c2)==0){ | |
double r1=((-b2)-sqrt(b2*b2-4*a2*c2))/(2*a2); | |
double r2=((-b2)+sqrt(b2*b2-4*a2*c2))/(2*a2); | |
root1.setText(String.valueOf(r1)); | |
root2.setText(String.valueOf(r2)); | |
} | |
if(b2*b2-4*a2*c2<0){ | |
root1.setText(String.valueOf("无实根")); | |
root2.setText(String.valueOf("无实根")); | |
} | |
} | |
if(event.getSource()==ex){ | |
exit(0); | |
} | |
} | |
public static void main(String[] args) { | |
//JFrame root =new JFrame ("求平方根"); | |
Myjframe jframe = new Myjframe(); | |
jframe.setVisible(true); | |
jframe.setSize(500, 500); | |
//JPanel jpanel=new JPanel(); | |
} | |
} |
自己写的,一开始没成功,原因是因为 root1 和 root2 声明成了新的局部变量,把局部变量的申明删了只留成员变量即可成功
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.KeyListener; | |
import static java.lang.Math.sqrt; | |
import static java.lang.System.exit; | |
public class Myjframe extends JFrame implements ActionListener { | |
private Panel panel1,panel2,panel3,panel4; | |
private JTextField a,b,c; | |
private JTextArea root1,root2; // 不能忘记定义,不然后续事件无法处理,且与上面的文本域不同 | |
private JLabel in,result; | |
private JButton cal,ex; | |
public Myjframe(){ | |
JPanel jpanel1=new JPanel(); | |
JPanel jpanel2=new JPanel(); | |
JPanel jpanel3=new JPanel(); | |
//JPanel jpanel4=new JPanel(); | |
jpanel1.setBackground(Color.white); | |
jpanel2.setBackground(Color.yellow); | |
this.setLayout(new FlowLayout()); // 重置框体的布局管理器 | |
setTitle("一元二次方程求根"); | |
a = new JTextField(10); | |
b = new JTextField(10); | |
c = new JTextField(10); | |
root1=new JTextArea(3,5); | |
root2=new JTextArea(3,5); | |
JLabel in=new JLabel("输入系数 a,b,c"); | |
JLabel result=new JLabel("两个根"); | |
// 组件加入面板 | |
jpanel1.add(in); | |
jpanel1.add(a); | |
jpanel1.add(b); | |
jpanel1.add(c); | |
//root1.setEditable (false);// 禁止编辑 | |
//root2.setEditable(false); | |
jpanel2.add(result); | |
jpanel2.add(root1); | |
jpanel2.add(root2); | |
// 示例化按钮 | |
cal=new JButton("求根"); | |
ex=new JButton("退出"); | |
// 监听按钮 | |
//cal.addActionListener(this); | |
//ex.addActionListener(this); | |
// 将按钮加入面板 | |
jpanel3.add(cal); | |
jpanel3.add(ex); | |
// 将面板加入框架 | |
this.add(jpanel1); | |
this.add(jpanel2); | |
this.add(jpanel3); | |
cal.addActionListener(this); | |
ex.addActionListener(this); | |
this.setDefaultCloseOperation(EXIT_ON_CLOSE); | |
} | |
// 进行事件处理 | |
public void actionPerformed(ActionEvent event){ | |
// 获得文本框内容 | |
String a1=a.getText(); | |
String b1=b.getText(); | |
String c1=c.getText(); | |
if(event.getSource()==ex){ | |
exit(0); | |
} | |
// if(event.getSource()==cal){ | |
double a2=Double.parseDouble(a1);// 将 string 转为 double 类型 | |
double b2=Double.parseDouble(b1); | |
double c2=Double.parseDouble(c1); | |
if(b2*b2-4*a2*c2>0){ | |
double r1=((-b2)-sqrt(b2*b2-4*a2*c2))/(2*a2); | |
double r2=((-b2)+sqrt(b2*b2-4*a2*c2))/(2*a2); | |
root1.setText(String.valueOf(r1)); | |
root2.setText(String.valueOf(r2)); | |
} else if ((b2*b2-4*a2*c2)==0){ | |
double r1=((-b2)-sqrt(b2*b2-4*a2*c2))/(2*a2); | |
double r2=((-b2)+sqrt(b2*b2-4*a2*c2))/(2*a2); | |
root1.setText(String.valueOf(r1)); | |
root2.setText(String.valueOf(r2)); | |
} else { | |
root1.setText(String.valueOf("无实根")); | |
root2.setText(String.valueOf("无实根")); | |
} | |
// } | |
if(event.getSource()==ex){ | |
exit(0); | |
} | |
} | |
public static void main(String[] args) { | |
//JFrame root =new JFrame ("求平方根"); | |
Myjframe jframe = new Myjframe(); | |
jframe.setVisible(true); | |
jframe.setSize(500, 500); | |
//JPanel jpanel=new JPanel(); | |
} | |
} | |
自己写的运行不成功,利用gpt修改: |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
import static java.lang.Math.sqrt; | |
import static java.lang.System.exit; | |
public class Myjframe extends JFrame implements ActionListener { | |
private JTextField a, b, c; | |
private JTextArea root1, root2; | |
private JButton ex,cal; | |
public Myjframe() { | |
this.setLayout(new FlowLayout()); | |
setTitle("一元二次方程求根"); | |
a = new JTextField(10); | |
b = new JTextField(10); | |
c = new JTextField(10); | |
root1 = new JTextArea(3, 10); | |
root2 = new JTextArea(3, 10); | |
JLabel in = new JLabel("输入系数 a,b,c"); | |
JLabel result = new JLabel("两个根"); | |
JPanel jpanel1 = new JPanel(); | |
jpanel1.add(in); | |
jpanel1.add(a); | |
jpanel1.add(b); | |
jpanel1.add(c); | |
JPanel jpanel2 = new JPanel(); | |
jpanel2.add(result); | |
jpanel2.add(root1); | |
jpanel2.add(root2); | |
JButton cal = new JButton("求根"); | |
JButton ex = new JButton("退出"); | |
cal.addActionListener(this); | |
ex.addActionListener(this); | |
JPanel jpanel3 = new JPanel(); | |
jpanel3.add(cal); | |
jpanel3.add(ex); | |
this.add(jpanel1); | |
this.add(jpanel2); | |
this.add(jpanel3); | |
this.setDefaultCloseOperation(EXIT_ON_CLOSE); | |
} | |
public void actionPerformed(ActionEvent event) { | |
if (event.getSource() == ex) { | |
exit(0); | |
return; | |
} | |
double a2 = Double.parseDouble(a.getText()); | |
double b2 = Double.parseDouble(b.getText()); | |
double c2 = Double.parseDouble(c.getText()); | |
double discriminant = b2 * b2 - 4 * a2 * c2; | |
if (discriminant > 0) { | |
root1.setText(String.valueOf((-b2 - sqrt(discriminant)) / (2 * a2))); | |
root2.setText(String.valueOf((-b2 + sqrt(discriminant)) / (2 * a2))); | |
} else if (discriminant == 0) { | |
double r = -b2 / (2 * a2); | |
root1.setText(String.valueOf(r)); | |
root2.setText(String.valueOf(r)); | |
} else { | |
root1.setText("无实根"); | |
root2.setText("无实根"); | |
} | |
} | |
public static void main(String[] args) { | |
Myjframe jframe = new Myjframe(); | |
jframe.setVisible(true); | |
jframe.setSize(500, 500); | |
} | |
} |