package com.lwy;
import java.awt.*;
import javax.swing.*;
public class Window extends JFrame {
public static void main(String[] args) {
Window w = new Window();
}
// 文本框
private JTextField t_size;
private JTextArea t_ratio;
private JTextArea t_value;
private JTextArea t_initVector;
private JTextField t_error;
private JTextArea t_answer;
// 按钮
private JButton bt_caculate;
private JButton bt_reset;
// 小容器
private JLabel jl1;
private JLabel jl2;
private JLabel jl3;
private JLabel jl4;
private JLabel jl5;
private JLabel jl6;
private JLabel jl7;
private JLabel jl8;
// 构造方法
public Window() {
super();
this.setTitle("高斯赛德尔迭代法求解线性方程组");
Init();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置布局方式为绝对定位
this.setLayout(null);
this.setBounds(0, 0, 600, 500);
// 设置窗体的标题图标
Image img = new ImageIcon("E:/set.png").getImage();
this.setIconImage(img);
// 设置窗体的大小可以改变
this.setResizable(true);
// 居中显示
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void Init() {
Container con = this.getContentPane();
// 设置背景图片
jl1 = new JLabel();
Image img1 = new ImageIcon("E:/backgroundImg.jpg").getImage();
jl1.setIcon(new ImageIcon(img1));
jl1.setBounds(0, 0, 600, 500);
// 欢迎标题
jl2 = new JLabel("欢迎使用高斯赛德尔迭代法求解线性方程组");
jl2.setBounds(130, 10, 350, 30);
jl2.setFont(new Font("宋体", Font.BOLD, 16));
// 维度
jl3 = new JLabel("请输入方程组的维度:");
jl3.setBounds(20, 40, 150, 20);
jl3.setFont(new Font("宋体", Font.BOLD, 14));
t_size = new JTextField();
t_size.setBounds(170, 40, 250, 25);
// 系数矩阵
jl4 = new JLabel("请输入系数矩阵:");
jl4.setBounds(20, 70, 150, 20);
jl4.setFont(new Font("宋体", Font.BOLD, 14));
t_ratio = new JTextArea();
t_ratio.setBounds(170, 70, 250, 100);
// 值向量
jl5 = new JLabel("请输入方程的值向量:");
jl5.setBounds(20, 250, 150, 20);
jl5.setFont(new Font("宋体", Font.BOLD, 14));
t_ratio = new JTextArea();
t_ratio.setBounds(170, 250, 250, 25);
// 迭代初始向量
jl6 = new JLabel("请输入迭代初始向量:");
jl6.setBounds(20, 280, 150, 20);
jl6.setFont(new Font("宋体", Font.BOLD, 14));
t_initVector = new JTextArea();
t_initVector.setBounds(170, 280, 250, 25);
// 误差
jl7 = new JLabel("请输入误差范围:");
jl7.setBounds(20, 310, 150, 20);
jl7.setFont(new Font("宋体", Font.BOLD, 14));
t_error = new JTextField();
t_error.setBounds(170, 310, 250, 25);
// 解向量
jl8 = new JLabel("运算结果为:");
jl8.setBounds(20, 340, 150, 20);
jl8.setFont(new Font("宋体", Font.BOLD, 14));
t_answer = new JTextArea();
t_answer.setBounds(170, 340, 250, 25);
// 计算按钮
bt_caculate = new JButton("计算");
bt_caculate.setBounds(170, 400, 70, 30);
// 重置按钮
bt_reset = new JButton("重置");
bt_reset.setBounds(350, 400, 70, 30);
jl1.add(jl2);
jl1.add(jl3);
jl1.add(jl4);
jl1.add(jl5);
jl1.add(jl6);
jl1.add(jl7);
jl1.add(jl8);
jl1.add(bt_caculate);
jl1.add(bt_reset);
con.add(jl1);
con.add(t_size);
con.add(t_ratio);
con.add(t_value);
con.add(t_initVector);
con.add(t_error);
con.add(t_answer);
}
}
错误:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1093)
at java.awt.Container.add(Container.java:417)
at com.lwy.Window.Init(Window.java:120)
at com.lwy.Window.(Window.java:37)
at com.lwy.Window.main(Window.java:8)