一、组件
1.1、概念简述
包含容器组件(jframe, jpanel, jiscrollpane…)与非容器组件(jbutton, jlgbel, jitextfield…)。
容器组件 是用来装 非容器组件 的 组件
1.2、JFrame的一些方法的简介
public static void main(String[] args) {
JFrame jFrame = new JFrame("标题");
jFrame.setIconImage(new ImageIcon(App.class.getClassLoader().getResource("icon.png")).getImage());//获取图标
jFrame.setSize(500, 800);
jFrame.setLocationRelativeTo(null);//居中
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭界面直接退出程序
jFrame.setResizable(false);//大小不可以改变
jFrame.setVisible(true);//是否可见
}
二、布局方式
2.1、BorderLayout边界布局
边界布局的特点:尽可能充满整个所在容器。故有时候一些属性不起作用,比如下图按钮,其高度就无法设置
用途:常用于根布局,设置每块区域的面板尺寸
//设置布局管理器(默认边界布局方式)
container.setLayout(new BorderLayout());
JRadioButton westRadiobtn = new JRadioButton("男");
JTextArea eastArea = new JTextArea("输入内容", 10, 20);
JButton centerBtn = new JButton("中间的按钮");
container.add(westRadiobtn,BorderLayout.WEST);
container.add(eastArea,BorderLayout.EAST);
centerBtn.setPreferredSize(new Dimension(500,200));
container.add(centerBtn,BorderLayout.CENTER);
2.2、FlowLayout流布局
流布局会受到组件大小的影响
JButton jb1 = new JButton("测试按钮1");
JButton jb2 = new JButton("测试按钮2");
JButton jb3 = new JButton("测试按钮3333333333333");
JButton jb4 = new JButton("测试按钮4");
JButton jb5 = new JButton("测试按钮5");
//jPanel默认流布局
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,50,20));// 对齐方式 水平间距 垂直间距
jPanel.add(jb1);
jPanel.add(jb2);
jPanel.add(jb3);
jPanel.add(jb4);
jPanel.add(jb5);
container.add(jPanel);
2.3、SpringLayout弹簧布局
就是先定位好一 个组件,其他组件都可以参照已经定位好的组件来进行相对定位
弹簧布局有三个重要的类:
- SpringLayout:布局管埋器
- SpringLayout.Constraints:使用弹舒布局的容器单面的组件的布局约束,每个组件对应一个
- Spring:可以理解为一个能够进行四则运算的整数
JLabel titleLabel = new JLabel("文章标题:");
JTextField titleTxt = new JTextField();
JLabel authorLabel = new JLabel("作者:");
JTextField authorTxt = new JTextField();
JLabel contLabel = new JLabel("请输入内容:");
JTextArea contArea = new JTextArea();
//jPanel弹簧布局
SpringLayout springLayout = new SpringLayout();
JPanel jPanel = new JPanel(springLayout);
jPanel.add(titleLabel);
titleTxt.setPreferredSize(new Dimension(200,20));
jPanel.add(titleTxt);
jPanel.add(authorLabel);
authorTxt.setPreferredSize(new Dimension(200,20));
jPanel.add(authorTxt);
jPanel.add(contLabel);
contArea.setPreferredSize(new Dimension(200,50));
contArea.setLineWrap(true);//设置换行
jPanel.add(contArea);
/*水平居中*/
Spring titleLabelWidth=Spring.width(titleLabel);
Spring titleTxtWidth = Spring.width(titleTxt);
Spring spaceWidth = Spring.constant(20);
Spring childWidth = Spring.sum(Spring.sum(titleLabelWidth, titleTxtWidth), spaceWidth);
int offsetX = childWidth.getValue() / 2;
SpringLayout.Constraints titleLabelC = springLayout.getConstraints(titleLabel);
springLayout.putConstraint(SpringLayout.WEST, titleLabel, -offsetX,
SpringLayout.HORIZONTAL_CENTER, jPanel);
titleLabelC.setY(Spring.constant(50));
/*
设置约束的第一种写法(复杂)
*/
//设置标题文本低: titleTxt 西边距离titleLabel的东边20px,北边相同
SpringLayout.Constraints titleTxtC = springLayout.getConstraints(titleTxt);
// edgeName: 东南西北 s:伯
Spring titleLabelEastSpring = titleLabelC.getConstraint(SpringLayout.EAST);
titleTxtC.setConstraint(SpringLayout.WEST, Spring.sum(titleLabelEastSpring, Spring.constant(20)));
titleTxtC.setConstraint(SpringLayout.NORTH, titleLabelC.getConstraint(SpringLayout.NORTH));
/*
设置约束的第二种写法(简单)
*/
// e1:要设置组件的哪个边界(东南西北)
// c1:要设置的组件
// pad:距离值
// e2:参照的组件的边界名
// c2:参考物(组件)
springLayout.putConstraint(SpringLayout.EAST, authorLabel, 0, SpringLayout.EAST, titleLabel);
springLayout.putConstraint(SpringLayout.NORTH, authorLabel, 40, SpringLayout.NORTH, titleLabel);
springLayout.putConstraint(SpringLayout.WEST, authorTxt, 20, SpringLayout.EAST, authorLabel);
springLayout.putConstraint(SpringLayout.NORTH, authorTxt, 0, SpringLayout.NORTH, authorLabel);
springLayout.putConstraint(SpringLayout.EAST, contLabel, 0, SpringLayout.EAST, authorLabel);
springLayout.putConstraint(SpringLayout.NORTH, contLabel, 40, SpringLayout.NORTH, authorLabel);
springLayout.putConstraint(SpringLayout.WEST, contArea, 20, SpringLayout.EAST, contLabel);
springLayout.putConstraint(SpringLayout.NORTH, contArea, 0, SpringLayout.NORTH, contLabel);
// contArea的南边和乐边参照jPanel 响应式
springLayout.putConstraint(SpringLayout.SOUTH, contArea, -80, SpringLayout.SOUTH, jPanel);
springLayout.putConstraint(SpringLayout.EAST, contArea, -20, SpringLayout.EAST, jPanel);
三、Jtable
3.1、设置参数
public class MyTable extends JTable {
public MyTable() {
//设置表头
JTableHeader tableHeader = getTableHeader();
tableHeader.setFont(new Font(null, Font.BOLD, 16));
tableHeader.setForeground(Color.RED);
//设置表格体
setFont(new Font(null, Font.PLAIN, 14));
setForeground(Color.black);
setGridColor(Color.BLACK);
setRowHeight(30);
//设置多行选择
getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// this.renderRule();
}
}
3.2、添加数据
private MyTable myTable = new MyTable();
public TableView() {
Vector<Vector<Object>> data = new Vector<>();
Vector<Object> row1 = new Vector<>();
row1.addElement("1");
row1.addElement("张三");
row1.addElement("123456");
row1.addElement("山东菏泽曹县");
row1.addElement("125");
row1.addElement("135");
row1.addElement("98");
row1.addElement("358");
for (int i = 0; i < 50; i++) {
row1.set(0, i + "");
data.addElement(row1);
}
// tableModel: 和jTable关联后,之后只需要更新model就能把数据的变化反应到jTable中
// jTable table关联
this.myTable.setDataModel(MyTableModel.assembleModel(data));
// jTable放在jPanel上的话,默认是不展示列头的,需要特殊设置,jTable放在JScrollPane上的话,默认是展示列头的
//add(this.myTable);//此方法不显示表格
setViewportView(this.myTable);