一. 布局管理器
- 布局器(FlowLayout)
//默认JPanel自带一个FlowLayout的流式布局器
//相当于下方的
LayoutManager layout = new FlowLayout();
root.setLayout(layout);
//默认为从左到右、从上到下的顺序
- 根据像素坐标进行布局
package FrameLearning;
import javax.swing.*;
public class test extends JFrame {
String ID;
public JButton cj;
public JButton stu;
public JPanel root;
public JPanel show;
public JPanel function;
test(String id){
this.ID = id;
this.setTitle("学生页面测试");
this.setBounds(0,100,400,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
this.setVisible(true);
this.setResizable(false);
root = new JPanel();
root.setLayout(null);
root.setSize(400,200);
root.setLocation(0,0);
this.getContentPane().add(root);
show = new JPanel();
show.setLocation(0,0);
show.setSize(200,100);
show.setLayout(null);
cj = new JButton("成绩查询");
cj.setBounds(0,0,80,40);
show.add(cj);
root.add(show);
function = new JPanel();
function.setLayout(null);
function.setLocation(200,0);
function.setSize(200,100);
stu = new JButton("欢迎"+this.ID);
stu.setBounds(0,0,180,40);
function.add(stu);
root.add(function);
}
}
class showFrame{
public static void main(String []args)
{
test myframe = new test("19123048");
}
}
被一个容器如父Frame、根Panel所包含时,
子Panel使用setLocation方法的x,y从根Panel的左上坐标来表示(即根的左上坐标为0,0),而不是由整个页面的左上坐标决定
同理子Panel中的组件如JButton、JTextField对象也是根据子Panel的左上坐标来表示的。
二. JTable
String[][] data = {{"Kundan Kumar Jha","4031","CSE"},{"Anand Jha","6014","IT"}};
String[] columnNames = {"Name","Roll Number","Department"};
//the table is constructed using these data and columnNames
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
//创建滑动窗格,并把Jtable对象添加进去
table.setFillsViewportHeight(true);
//
- 使用BorderLayout布局
package FrameLearning;
import javax.swing.*;
import java.awt.*;
public class myframe extends JFrame {
myframe(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(650,600);
this.setLayout(new BorderLayout(5,20));
((JPanel)this.getContentPane()).setBorder(BorderFactory.createEmptyBorder(5,10,10,10));
JPanel jPanelNorth = new JPanel();
jPanelNorth.setPreferredSize(new Dimension(630,40));
jPanelNorth.setLayout(new FlowLayout(FlowLayout.RIGHT,10,0));
//流式布局
this.add(jPanelNorth,"North");
JLabel jLabelMsg = new JLabel();
jLabelMsg.setPreferredSize(new Dimension(60,40));
Font font = new Font("Dialog",1,12);
Font labelFont = new Font("Dialog",1,14);
jLabelMsg.setFont(labelFont);
jLabelMsg.setText("欢迎您:");
jPanelNorth.add(jLabelMsg);
JLabel jLabelStu = new JLabel();
jLabelStu.setPreferredSize(new Dimension(70,40));
jLabelStu.setText("19123048");
jLabelStu.setFont(labelFont);
jPanelNorth.add(jLabelStu);
JButton jButtonExit = new JButton("退出");
jButtonExit.setPreferredSize(new Dimension(70,35));
jButtonExit.setFont(font);
jPanelNorth.add(jButtonExit);
JPanel jPanelFunction = new JPanel();
jPanelFunction.setPreferredSize(new Dimension(150,540));
jPanelFunction.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
JButton jButtonQuery = new JButton("成绩查询");
jButtonQuery.setPreferredSize(new Dimension(100,40));
jPanelFunction.add(jButtonQuery);
this.add(jPanelFunction,"Center");
String[] col_name = {"course_order", "course_name"};
Object[][] tableData = new Object[0][0];
JTable table = new JTable(tableData,col_name);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(450,540));
this.pack();
this.add(scrollPane,"East");
this.setVisible(true);
}
}
class runframe{
public static void main(String []args){
myframe myframe = new myframe();
}
}
package cjdemo;
import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;
import jdbctest.DBConnector;
import jdbctest.CustomException;
/**
* @description : 显示学生界面,包含成绩查询功能
* @Content : 组件内容:1.Jtable类型的成绩表格cjtable
* 2.JPanel类型的学生信息面板stuPanel
* 2.JPanel类型的功能面板panel_function
* 3.JPanel类型的成绩表格显示面板panel_show
*
*/
public class stuFrame extends JFrame {
int id;
JScrollPane scrollPane;
JPanel panel_show;
/**
* @param ID: 根据学生ID来创建学生页面
* @throws SQLException
* @throws CustomException
*/
public stuFrame(int ID) throws SQLException, CustomException {
id = ID;
this.setResizable(false);
// 调用setBounds方法调整页面的左上角坐标以及页面的大小
// this.setBounds(100, 100, 1000, 800);
this.setTitle(this.id+"学生成绩管理页面");
// 将右上角的[x]与退出程序相关联
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(700,600);
this.setLocation(100,100);
this.setLayout(new BorderLayout(5,20));
((JPanel)this.getContentPane()).setBorder(BorderFactory.createEmptyBorder(5,10,10,10));
/**
* @function: 创建显示欢迎学生的窗格。
*/
JPanel stuPanel = new JPanel();
stuPanel.setPreferredSize(new Dimension(700,80));
stuPanel.setLayout(new FlowLayout(FlowLayout.RIGHT,10,5));
this.add(stuPanel,"North");
JLabel stuInfo = new JLabel();
stuInfo.setPreferredSize(new Dimension(120,60));
Font font = new Font("Dialog",1,12);
Font labelFont = new Font("Dialog",1,14);
stuInfo.setText("欢迎您:"+this.id);
stuInfo.setFont(labelFont);
stuPanel.add(stuInfo);
JButton exitButton = new JButton("安全退出");
exitButton.setPreferredSize(new Dimension(100,60));
exitButton.setFont(font);
stuPanel.add(exitButton);
int[] int_args = new int[2];
String[] str_args = new String[0];
int_args[0] = 3002001;
int_args[1] = 2002001;
DBConnector conn = new DBConnector();
Object[][] tableData = new Object[0][0];
try {
tableData = conn.search("课程成绩查询", int_args, str_args);
} catch (CustomException e) {
System.out.println(e.getMessage());
return;
}
// System.out.println(tableData.length);
String[] col_name = {"course_order", "course_name", "teacher_id", "course_time", "course_credit", "score"};
/**
* @function: 创建成绩显示表格cjtable
*/
JTable cjtable = new JTable(tableData, col_name);
// cjtable.setBounds(0,0,700,300);
cjtable.setSize(700,300);
// cjtable.setPreferredSize(new Dimension(700,300));
// 将表格设置为不可编辑
cjtable.setEnabled(false);
/**
* @function: 创建成绩显示面板,显示学生成绩
*/
// scrollPane = new JScrollPane(cjtable);
// scrollPane.setPreferredSize(new Dimension(800,700));
// scrollPane.setVisible(true);
// this.add(scrollPane,"East");
panel_show = new JPanel();
panel_show.setLayout(new FlowLayout(FlowLayout.RIGHT,0,0));
panel_show.setPreferredSize(new Dimension(500,700));
this.add(panel_show,"East");
panel_show.add(new JScrollPane(cjtable));
panel_show.setVisible(false);
/**
* @function: 创建功能栏面板panel_function,包含成绩查询按钮button组件
*/
JPanel panel_function = new JPanel();
panel_function.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
panel_function.setPreferredSize(new Dimension(100,700));
this.add(panel_function,"Center");
JButton button = new JButton("成绩查询");
button.setFont(font);
button.setPreferredSize(new Dimension(100,60));
/**
* @function: 为【成绩查询】按钮创建监听器,点击后成绩显示窗格状态改为【显示】
* 函数参数采用lambda表达式
*/
button.addActionListener((e) -> {
panel_show.setVisible(true);
});
//将【成绩查询】按钮组件添加到功能栏面板中
panel_function.add(button);
}
}