Java Swing的基本页面搭建

之前每次上手都要花好长时间将基本页面弄出来,太折磨人了,所以先贴一个基本页面慢慢再修改

import javax.swing.*;
import java.awt.*;

public class mainPage extends JFrame{
    public static void main(String[] args) {
        JFrame frame = new JFrame("购买商品页面");
        frame.setBounds(50,50,500, 550);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        JPanel jp=new JPanel();
        jp.setVisible(true);
        jp.setLayout(null);


        JLabel jl1=new JLabel("学号");
        JLabel jl2=new JLabel("成绩");
        JTextField jtf1=new JTextField();
        JTextField jtf2=new JTextField();
        JButton jb1=new JButton("确定");
        JButton jb2=new JButton("取消");

        jl1.setBounds(25,50,150,50);
        jl2.setBounds(25,150,150,50);
        jtf1.setBounds(75,50,150,50);
        jtf2.setBounds(75,150,150,50);
        jb1.setBounds(25,240,100,35);
        jb2.setBounds(140,240,100,35);
        jp.add(jl1);
        jp.add(jtf1);
        jp.add(jl2);
        jp.add(jtf2);
        jp.add(jb1);
        jp.add(jb2);

        jp.validate();

        frame.add(jp);
        frame.add(jp,BorderLayout.CENTER);
        frame.validate();
    }
}

表格的基本页面

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class totalThings extends JDialog{
    public static JTable table=new JTable();
    public totalThings(JFrame frame)
    {
        super(frame,"购买汇总页面",true);
        setBounds(50,50,500, 550);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setLayout(new FlowLayout());

        Container container=getContentPane();  //创建一个容器

        fillTable(table);
        JScrollPane scroll=new JScrollPane(table);
        container.add(scroll);
        container.validate();

        setVisible(true);
        validate();
    }

    public static void fillTable(JTable table)
    {
        DefaultTableModel tableModel=(DefaultTableModel) table.getModel();    //获得表格模型
        tableModel.setRowCount(0);    //清空表格中的数据
        tableModel.setColumnIdentifiers(new Object[]{"ID","商品名称","商品单价","商品数量"});    //设置表头
        tableModel.addRow(new Object[]{"001","pencil","13.3","10"});    //增加列
        table.setRowHeight(30);
        table.setModel(tableModel);
    }
}

你可能感兴趣的:(java,开发语言,后端)