Java+Swing+Mysql学生成绩管理系统(DAY 9)
问题引入
- 主界面需要哪些组件,如何布局各个组件
- 如何响应LoginView界面的登录按钮,跳转到下一个页面(主界面)
(暂时不绑定数据库,不实现用户密码登录,登录按钮无需输入直接响应跳转到主界面)
- 成绩页面的大小如何设置,布满整个电脑屏幕且不覆盖任务栏
界面效果
逻辑关系
LoginView
package view;
import handler.LoginHandler;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
public class LoginView extends JFrame {
JLabel titleNameLabel=new JLabel("学生成绩管理系统",JLabel.CENTER);
SpringLayout spLayout=new SpringLayout();
JPanel centerPanel=new JPanel(spLayout);
JLabel userNameLabel=new JLabel("用户名:");
JLabel userPwdLabel=new JLabel("登录密码:");
JTextField nameTxd=new JTextField();
JPasswordField pwdFiled=new JPasswordField();
JButton loginBtn=new JButton("登录");
JButton resetBtn=new JButton("重置");
SystemTray systemTray;
TrayIcon trayIcon;
LoginHandler loginHandler;
public LoginView(){
super("学生成绩管理系统");
loginHandler=new LoginHandler(this);
Container contentPane =getContentPane();
titleNameLabel.setFont(new Font("华文行楷",Font.PLAIN,40));
titleNameLabel.setPreferredSize(new Dimension(0,80));
Font centerFont=new Font("华文行楷",Font.PLAIN,20);
userNameLabel.setFont(centerFont);
nameTxd.setPreferredSize(new Dimension(200,30));
userPwdLabel.setFont(centerFont);
pwdFiled.setPreferredSize(new Dimension(200,30));
loginBtn.setFont(centerFont);
resetBtn.setFont(centerFont);
contentPane.setBackground(Color.pink);
centerPanel.add(userNameLabel);
centerPanel.add(userPwdLabel);
centerPanel.add(nameTxd);
centerPanel.add(pwdFiled);
centerPanel.add(loginBtn);
centerPanel.add(resetBtn);
loginBtn.addActionListener(loginHandler);
loginBtn.addKeyListener(loginHandler);
resetBtn.addActionListener(loginHandler);
contentPane.add(titleNameLabel,BorderLayout.NORTH);
contentPane.add(centerPanel,BorderLayout.CENTER);
layoutCenter();
if(SystemTray.isSupported()){
systemTray=SystemTray.getSystemTray();
URL imgUrl = LoginView.class.getClassLoader().getResource("LoginView.jpg");
trayIcon=new TrayIcon(new ImageIcon(imgUrl).getImage());
trayIcon.setImageAutoSize(true);
try {
systemTray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
this.addWindowListener(new WindowAdapter() {
@Override
public void windowIconified(WindowEvent e) {
LoginView.this.dispose();
}
});
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int clickCont=e.getClickCount();
if(clickCont==1){
LoginView.this.setExtendedState(JFrame.NORMAL);
}
LoginView.this.setVisible(true);
}
});
}
getRootPane().setDefaultButton(loginBtn);
URL imgUrl=LoginView.class.getClassLoader().getResource("LoginView.jpg");
setIconImage(new ImageIcon(imgUrl).getImage());
setSize(600,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
private void layoutCenter() {
Spring titleLabelWidth=Spring.width(userNameLabel);
Spring titleTextWidth=Spring.width(nameTxd);
Spring spaceWidth=Spring.constant(20);
Spring totalWidth=Spring.sum(Spring.sum(titleLabelWidth,titleTextWidth),spaceWidth);
int offSetX=totalWidth.getValue()/2;
SpringLayout.Constraints titleLabelCon=spLayout.getConstraints(userNameLabel);
spLayout.putConstraint(SpringLayout.WEST,userNameLabel,-offSetX,SpringLayout.HORIZONTAL_CENTER,centerPanel);
spLayout.putConstraint(SpringLayout.NORTH,userNameLabel,20,SpringLayout.NORTH,centerPanel);
spLayout.putConstraint(SpringLayout.WEST,nameTxd,20,SpringLayout.EAST,userNameLabel);
spLayout.putConstraint(SpringLayout.NORTH,nameTxd,0,SpringLayout.NORTH,userNameLabel);
spLayout.putConstraint(SpringLayout.NORTH,userPwdLabel,20,SpringLayout.SOUTH,userNameLabel);
spLayout.putConstraint(SpringLayout.EAST,userPwdLabel,0,SpringLayout.EAST,userNameLabel);
spLayout.putConstraint(SpringLayout.WEST,pwdFiled,20,SpringLayout.EAST,userPwdLabel);
spLayout.putConstraint(SpringLayout.NORTH,pwdFiled,0,SpringLayout.NORTH,userPwdLabel);
spLayout.putConstraint(SpringLayout.EAST,loginBtn,-20,SpringLayout.HORIZONTAL_CENTER,centerPanel);
spLayout.putConstraint(SpringLayout.NORTH,loginBtn,50,SpringLayout.SOUTH,pwdFiled);
spLayout.putConstraint(SpringLayout.WEST,resetBtn,20,SpringLayout.HORIZONTAL_CENTER,centerPanel);
spLayout.putConstraint(SpringLayout.NORTH,resetBtn,50,SpringLayout.SOUTH,pwdFiled);
}
public static void main(String[] args){
new LoginView();
}
public JTextField getNameTxd() {
return nameTxd;
}
public JPasswordField getPwdFiled() {
return pwdFiled;
}
}
LoginHandler
package handler;
import view.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class LoginHandler extends KeyAdapter implements ActionListener{
private LoginView loginView;
public LoginHandler(LoginView loginView){
this.loginView=loginView;
}
@Override
public void actionPerformed(ActionEvent e){
JButton jButton=(JButton) e.getSource();
String text=jButton.getText();
if("重置".equals(text)){
loginView.getNameTxd().setText("");
loginView.getPwdFiled().setText("");
}else if("登录".equals(text)){
login();
}
}
private void login() {
String user=loginView.getNameTxd().getText();
char []chars=loginView.getPwdFiled().getPassword();
String pwd=new String(chars);
System.out.println(user+":"+pwd);
boolean flag=true;
if(flag){
new MainView();
loginView.dispose();
}else{
JOptionPane.showMessageDialog(loginView,"用户名密码错误");
}
}
@Override
public void keyPressed(KeyEvent e) {
if(KeyEvent.VK_ENTER==e.getKeyCode()) {
login();
}
}
}
MainTable
package view;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.net.URL;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import ext.MainViewTable;
import ext.MainViewTableModel;
import util.DimensionUtil;
public class MainView extends JFrame{
JPanel northPanel=new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton addBtn=new JButton("增加");
JButton updataBtn=new JButton("修改");
JButton delBtn=new JButton("删除");
JTextField searchTxt=new JTextField(15);
JButton searchBtn=new JButton("查询");
JPanel southPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton preBtn=new JButton("上一页");
JButton nextBtn =new JButton("下一页");
MainViewTable mianViewTable=new MainViewTable();
public MainView() {
super("主界面-学生成绩管理系统");
Container contentPane=getContentPane();
layoutNorth(contentPane);
layoutCenter(contentPane);
layoutSouth(contentPane);
init();
}
private void layoutCenter(Container contentPane) {
Vector<Vector<Object>> data = new Vector<>();
Vector<Object> rowVector1 = new Vector<>();
rowVector1.addElement("1");
rowVector1.addElement("张三");
rowVector1.addElement("1");
rowVector1.addElement("湖南");
Vector<Object> rowVector2 = new Vector<>();
rowVector2.addElement("2");
rowVector2.addElement("李四");
rowVector2.addElement("2");
rowVector2.addElement("北京");
Vector<Object> rowVector3 = new Vector<>();
rowVector3.addElement("3");
rowVector3.addElement("王五");
rowVector3.addElement("3");
rowVector3.addElement("上海");
data.addElement(rowVector1);
data.addElement(rowVector2);
data.addElement(rowVector3);
MainViewTableModel mainViewTableModel=MainViewTableModel.assembleModel(data);
mianViewTable.setModel(mainViewTableModel);
mianViewTable.renderRule();
JScrollPane jScrollPane = new JScrollPane(mianViewTable);
contentPane.add(jScrollPane,BorderLayout.CENTER);
}
private void layoutSouth(Container contentPane) {
southPanel.add(preBtn);
southPanel.add(nextBtn);
contentPane.add(southPanel,BorderLayout.SOUTH);
}
private void layoutNorth(Container contentPane) {
northPanel.add(addBtn);
northPanel.add(updataBtn);
northPanel.add(delBtn);
northPanel.add(searchTxt);
northPanel.add(searchBtn);
contentPane.add(northPanel,BorderLayout.NORTH);
}
private void init() {
URL imgUrl=LoginView.class.getClassLoader().getResource("LoginView.jpg");
setIconImage(new ImageIcon(imgUrl).getImage());
setBounds(DimensionUtil.getBounds());
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(true);
setVisible(true);
}
public static void main(String[] args) {
}
}
MainViewTable
package ext;
import java.awt.Color;
import java.awt.Font;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
public class MainViewTable extends JTable{
public MainViewTable() {
JTableHeader tableHeader = getTableHeader();
tableHeader.setFont(new Font(null, Font.BOLD, 16));
tableHeader.setBackground(Color.pink);
setFont(new Font(null, Font.PLAIN, 14));
setBackground(Color.WHITE);
setGridColor(Color.BLACK);
setRowHeight(30);
getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
public void renderRule() {
Vector<String> columns=MainViewTableModel.getColumns();
MainViewCellRender render =new MainViewCellRender();
for(int i=0;i<columns.size();i++){
TableColumn column=getColumn(columns.get(i));
column.setCellRenderer(render);
if(i==0){
column.setPreferredWidth(30);
column.setResizable(false);
}
}
}
public static void main(String[] args) {
}
}
MainViewCellRender
package ext;
import java.awt.Color;
import java.awt.Font;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
public class MainViewTable extends JTable{
public MainViewTable() {
JTableHeader tableHeader = getTableHeader();
tableHeader.setFont(new Font(null, Font.BOLD, 16));
tableHeader.setBackground(Color.pink);
setFont(new Font(null, Font.PLAIN, 14));
setBackground(Color.WHITE);
setGridColor(Color.BLACK);
setRowHeight(30);
getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
}
public void renderRule() {
Vector<String> columns=MainViewTableModel.getColumns();
MainViewCellRender render =new MainViewCellRender();
for(int i=0;i<columns.size();i++){
TableColumn column=getColumn(columns.get(i));
column.setCellRenderer(render);
if(i==0){
column.setPreferredWidth(30);
column.setResizable(false);
}
}
}
public static void main(String[] args) {
}
}
MainViewTableModel
package ext;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
public class MainViewTableModel extends DefaultTableModel{
static Vector<String> columns=new Vector<>();
static{
columns.addElement("编号");
columns.addElement("姓名");
columns.addElement("学号");
columns.addElement("家乡");
}
private MainViewTableModel(){
super(null,columns);
}
private static MainViewTableModel mainViewTableModel=new MainViewTableModel();
public static MainViewTableModel assembleModel(Vector<Vector<Object>> data){
mainViewTableModel.setDataVector(data,columns);
return mainViewTableModel;
}
public static Vector<String> getColumns() {
return columns;
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
}
DimensionUtil
package util;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class DimensionUtil {
public static Rectangle getBounds() {
Dimension screenSize =Toolkit.getDefaultToolkit().getScreenSize();
Insets screenInset=Toolkit.getDefaultToolkit().
getScreenInsets(new JFrame().getGraphicsConfiguration());
Rectangle rectangle= new Rectangle(screenInset.left,screenInset.top,
screenSize.width-screenInset.left-screenInset.right,
screenSize.height-screenInset.top-screenInset.bottom);
return rectangle;
}
public static void main(String[] args) {
}
}