java练手(供本人观看 如果有人观看 欢迎提出意见 )残版实时更新
2021/7/21 22:22:19
本文主要是介绍java练手(供本人观看 如果有人观看 欢迎提出意见 )残版实时更新,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
无说明 练手小项目 残版 实时更新
package com; 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 nameLabel = new JLabel("DX 成绩管理中心", JLabel.CENTER); SpringLayout springLayout = new SpringLayout(); JPanel centerPanel = new JPanel(springLayout); JLabel userNameLabel = new JLabel("用户名"); JTextField userTxt = new JTextField(); JLabel pwdLabel = new JLabel("密码"); JPasswordField pwdField = new JPasswordField(); JButton loginBtn = new JButton("登录"); JButton resetBtn = new JButton("重置"); SystemTray systemTray; TrayIcon trayIcon; LoginHandler loginHandler; public LoginView() { super("DX 成绩管理中心"); Container contentPane = getContentPane(); loginHandler=new LoginHandler(this); nameLabel.setFont(new Font("华文行楷", Font.PLAIN, 40)); nameLabel.setPreferredSize(new Dimension(0, 80)); Font centerFont = new Font("楷体", Font.PLAIN, 20); userNameLabel.setFont(centerFont); loginBtn.setFont(centerFont); resetBtn.setFont(centerFont); pwdLabel.setFont(centerFont); userTxt.setPreferredSize(new Dimension(200, 30)); pwdField.setPreferredSize(new Dimension(200, 30)); // 把组建加入面板 centerPanel.add(userNameLabel); centerPanel.add(userTxt); centerPanel.add(pwdLabel); centerPanel.add(pwdField); loginBtn.addActionListener(loginHandler); // 增加按键事件 loginBtn.addKeyListener(loginHandler); centerPanel.add(loginBtn); resetBtn.addActionListener(loginHandler); centerPanel.add(resetBtn); // 弹簧布局 layoutCenter(); contentPane.add(nameLabel, BorderLayout.NORTH); contentPane.add(centerPanel, BorderLayout.CENTER); if (SystemTray.isSupported()){ systemTray=SystemTray.getSystemTray(); URL resource = LoginView.class.getResource("/自定义图片/1.jpg"); trayIcon=new TrayIcon(new ImageIcon(resource).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 clickCount = e.getClickCount(); if (clickCount==1){ LoginView.this.setExtendedState(JFrame.NORMAL); } LoginView.this.setVisible(true); } }); } URL resource = LoginView.class.getResource("/自定义图片/1.jpg"); setIconImage(new ImageIcon(resource).getImage()); // 设置默认按钮 getRootPane().setDefaultButton(loginBtn); setLocationRelativeTo(null); // 关闭退出程序 setDefaultCloseOperation(EXIT_ON_CLOSE); // 大小不可改变 setResizable(false); setVisible(true); setSize(600, 400); } // 弹簧布局 private void layoutCenter() { // 开始布局userNameLabel Spring childWidth = Spring.sum(Spring.sum(Spring.width(userNameLabel), Spring.width(userTxt)), Spring.constant(20)); int offsetX = childWidth.getValue() / 2; springLayout.putConstraint(SpringLayout.WEST,userNameLabel,-offsetX,SpringLayout.HORIZONTAL_CENTER,centerPanel); springLayout.putConstraint(SpringLayout.NORTH,userNameLabel,20,SpringLayout.NORTH,centerPanel); // userTxt springLayout.putConstraint(SpringLayout.WEST,userTxt,20,SpringLayout.EAST,userNameLabel); springLayout.putConstraint(SpringLayout.NORTH,userTxt,0,SpringLayout.NORTH,userNameLabel); // pwdLabel springLayout.putConstraint(SpringLayout.EAST,pwdLabel,0,SpringLayout.EAST,userNameLabel); springLayout.putConstraint(SpringLayout.NORTH,pwdLabel,20,SpringLayout.SOUTH,userNameLabel); // pwdField springLayout.putConstraint(SpringLayout.WEST,pwdField,20,SpringLayout.EAST,userNameLabel); springLayout.putConstraint(SpringLayout.NORTH,pwdField,0,SpringLayout.NORTH,pwdLabel); // loginBtn springLayout.putConstraint(SpringLayout.WEST,loginBtn ,-40,SpringLayout.EAST,userNameLabel); springLayout.putConstraint(SpringLayout.NORTH,loginBtn,80,SpringLayout.NORTH,pwdField); // resetBtn springLayout.putConstraint(SpringLayout.WEST,resetBtn,80,SpringLayout.EAST,loginBtn); springLayout.putConstraint(SpringLayout.NORTH,resetBtn,0,SpringLayout.NORTH,loginBtn); } public static void main(String[] args) { new LoginView(); } public JTextField getUserTxt() { return userTxt; } public void setUserTxt(JTextField userTxt) { this.userTxt = userTxt; } public JPasswordField getPwdField() { return pwdField; } } ```java package com; 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.getUserTxt().setText(""); loginView.getPwdField().setText(""); }else if ("登录".equals(text)){ login(); } } private void login() { String user = loginView.getUserTxt().getText(); char[] chars = loginView.getPwdField().getPassword(); String pwd = new String(chars); System.out.println(user + ":" + pwd); // 查询db boolean flag=false; if (flag){ // 验证成功跳转到主界面并且销毁登录界面 }else { JOptionPane.showMessageDialog(loginView,"用户名密码错误"); } } @Override public void keyPressed(KeyEvent e) { if (KeyEvent.VK_ENTER == e.getKeyCode()) { login(); } } }
package com; import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.util.Vector; public class JtableDemo extends JFrame { public JtableDemo() { super("测试Jtable"); Vector<Vector<Object>> data=new Vector<>(); Vector<Object> rowVector1=new Vector<>(); rowVector1.addElement("1"); rowVector1.addElement("张三"); rowVector1.addElement("no1"); rowVector1.addElement("甘肃"); rowVector1.addElement("1"); rowVector1.addElement("2"); rowVector1.addElement("3"); rowVector1.addElement("6"); Vector<Object> rowVector2=new Vector<>(); rowVector2.addElement("1"); rowVector2.addElement("张三"); rowVector2.addElement("no1"); rowVector2.addElement("甘肃"); rowVector2.addElement("1"); rowVector2.addElement("2"); rowVector2.addElement("3"); rowVector2.addElement("6"); Vector<Object> rowVector3=new Vector<>(); rowVector3.addElement("1"); rowVector3.addElement("张三"); rowVector3.addElement("no1"); rowVector3.addElement("甘肃"); rowVector3.addElement("1"); rowVector3.addElement("2"); rowVector3.addElement("3"); rowVector3.addElement("6"); data.addElement(rowVector1); data.addElement(rowVector2); data.addElement(rowVector3); StudentTableModel studentTableModel = StudentTableModel.assembleModel(data); JTable jTable = new JTable(studentTableModel); // 设置表头 JTableHeader tableHeader = jTable.getTableHeader(); tableHeader.setFont(new Font("华文楷体",Font.BOLD,16)); tableHeader.setForeground(Color.RED); // 设置表格体 jTable.setFont(new Font("华文楷体",Font.BOLD,14)); jTable.setForeground(Color.black); jTable.setGridColor(Color.black); jTable.setRowHeight(30); // 设置多行选择 jTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); // 设置表格列的渲染方式 Vector<String> columns = StudentTableModel.getColumns(); StudentCellRender render = new StudentCellRender(); for (int i = 0; i < columns.size(); i++) { TableColumn column = jTable.getColumn(columns.get(i)); column.setCellRenderer(render); if (i==0){ column.setPreferredWidth(50); column.setResizable(false); } } Container contentPane = getContentPane(); // jtable放在jpanel上的话,默认是不展示列头的,需要特殊设置 放在jscrollpane上面 // 默认是展示列头的 JScrollPane jScrollPane=new JScrollPane(jTable); contentPane.add(jScrollPane); setLocationRelativeTo(null); // 关闭退出程序 setDefaultCloseOperation(EXIT_ON_CLOSE); // 大小不可改变 setResizable(false); setVisible(true); setSize(600, 400); } public static void main(String[] args) { new JtableDemo(); } } class StudentCellRender extends DefaultTableCellRenderer { // 在每一行的每一列显示之前都会调用 @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (row %2==0){ setBackground(Color.LIGHT_GRAY); }else { setBackground(Color.white); } setHorizontalAlignment(DefaultTableCellRenderer.CENTER); return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); } } //自定义tablemdel class StudentTableModel extends DefaultTableModel{ static Vector<String> columns =new Vector<>(); static { columns.addElement("编号"); columns.addElement("姓名"); columns.addElement("学号"); columns.addElement("语文"); columns.addElement("数学"); columns.addElement("英语"); columns.addElement("总分"); } private StudentTableModel(){ super(null,columns); } private static StudentTableModel studentTableModel=new StudentTableModel(); public static StudentTableModel assembleModel(Vector<Vector<Object>> data){ studentTableModel.setDataVector(data,columns); return studentTableModel; } public static Vector<String> getColumns() { return columns; } @Override public boolean isCellEditable(int row, int column) { return false; } }
package com; import javax.swing.*; import java.awt.*; public class MainView extends JFrame { public MainView() { JLabel nameLabel = new JLabel("DX 成绩管理中心", JLabel.CENTER); Container contentPane = getContentPane(); setLocationRelativeTo(null); // 关闭退出程序 setDefaultCloseOperation(EXIT_ON_CLOSE); // 大小不可改变 setResizable(false); setVisible(true); setSize(600, 400); } public static void main(String[] args) { new MainView(); } }
这篇关于java练手(供本人观看 如果有人观看 欢迎提出意见 )残版实时更新的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-26大厂数据结构与算法教程:入门级详解
- 2024-12-26大厂算法与数据结构教程:新手入门指南
- 2024-12-26Python编程入门指南
- 2024-12-26数据结构高级教程:新手入门及初级提升指南
- 2024-12-26并查集入门教程:从零开始学会并查集
- 2024-12-26大厂数据结构与算法入门指南
- 2024-12-26大厂算法与数据结构入门教程
- 2024-12-26二叉树入门教程:轻松掌握基础概念与操作
- 2024-12-26初学者指南:轻松掌握链表
- 2024-12-26平衡树入门教程:轻松理解与应用