复选按钮
package www.qyl.lesson02; import javax.swing.*; public class TestradioBttton { public static void main(String[] args) { JFrame jf = new JFrame(); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jf.setBounds(10,10,500,300); jf.setVisible(true); JPanel jPanel = new JPanel(); JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1"); JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2"); JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3"); jPanel.add(jCheckBox1); jPanel.add(jCheckBox2); jPanel.add(jCheckBox3); jf.add(jPanel); } }
8、列表
下拉框
package www.qyl.lesson02; import javax.swing.*; import java.awt.*; public class TestradioBttton { public static void main(String[] args) { JFrame jf = new JFrame(); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jf.setBounds(10,10,500,300); jf.setVisible(true); JPanel jPanel = new JPanel(); JComboBox jComboBox = new JComboBox(); jComboBox.addItem(null); jComboBox.addItem("1"); jComboBox.addItem("2"); jComboBox.addItem("3"); System.out.println(jComboBox.getSelectedItem().toString().trim()); //获取下拉框选中的值 jPanel.add(jComboBox); jf.add(jPanel); } }
列表框
package www.qyl.lesson02; import javax.swing.*; import java.awt.*; public class TestradioBttton { public static void main(String[] args) { JFrame jf = new JFrame(); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jf.setBounds(10,10,500,300); jf.setVisible(true); JPanel jPanel = new JPanel(); jPanel.setBounds(10,10,300,300); String[] con = {"1","2","3"}; JList jList = new JList(con); jList.setBounds(10,10,100,100); jPanel.add(jList); jf.add(jPanel); } }
9、文本框
package www.qyl.lesson02; import javax.swing.*; import java.awt.*; public class TestradioBttton { public static void main(String[] args) { JFrame jf = new JFrame(); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jf.setBounds(10,10,500,300); jf.setLayout(null); jf.setVisible(true); JPanel jPanel = new JPanel(); jPanel.setBounds(10,10,300,300); JTextField jTextField = new JTextField("hello qyl"); JTextField jTextField2 = new JTextField("hello 秋意临",20); jTextField.setBounds(10,10,60,30); jTextField2.setBounds(10,50,60,30); jPanel.add(jTextField); jPanel.add(jTextField2); jf.add(jPanel); } }
密码框
package www.qyl.lesson02; import javax.swing.*; import java.awt.*; public class TestradioBttton { public static void main(String[] args) { JFrame jf = new JFrame(); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jf.setBounds(10,10,1000,300); jf.setLayout(null); jf.setVisible(true); JPanel jPanel = new JPanel(); jPanel.setBounds(10,10,500,300); JPasswordField jPasswordField1 = new JPasswordField(); jPasswordField1.setBounds(10,10,200,100); jPanel.add(jPasswordField1); jf.add(jPanel); } }
文本域
package www.qyl.lesson02; import javax.swing.*; import java.awt.*; public class TestradioBttton { public static void main(String[] args) { JFrame jf = new JFrame(); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jf.setBounds(10,10,1000,300); jf.setLayout(null); jf.setVisible(true); JPanel jPanel = new JPanel(); jPanel.setBounds(10,10,500,300); JTextArea jTextArea = new JTextArea(30,50); jTextArea.setText("hello秋意临:"); jTextArea.setBounds(10,10,200,100); jPanel.add(jTextArea); jf.add(jPanel); } }
10、菜单
Java菜单提供JmenuBar,Jmenu,Jmenultem,JcheckboxMenultem类来创建和管理。
JcheckboxMenultem:复选框菜单项
关系图
案例练习
package www.qyl.lesson03; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class TestJMenuBar{ public static void main(String[] args) { JFrame jf = new JFrame("菜单"); Container container = jf.getContentPane(); container.setLayout(null); jf.setBounds(10,10,300,300); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jf.setVisible(true); JMenuBar menuBar = new JMenuBar(); //创建菜单栏 menuBar.setBounds(0,0,300,30); container.add(menuBar); //菜单栏放入到JFrame顶层容器 JMenu m1 = new JMenu("系统(n)"); // 设置一级菜单 JMenu m2 = new JMenu("简介"); JMenu m3 = new JMenu("工具"); JMenu m4 = new JMenu("查询"); JMenu m5 = new JMenu("关闭"); m1.setMnemonic('n'); //设置快捷键 menuBar.add(m1); //将一级菜单放入菜单栏中 menuBar.add(m2); menuBar.add(m3); menuBar.add(m4); menuBar.add(m5); JMenu jmi1 = new JMenu("开始导航"); // 设置二级菜单项 JMenu jmi2 = new JMenu("刷新系统"); JMenu jmi3 = new JMenu("退出系统"); m1.add(jmi1); //将二级菜单项放入菜单中 m1.addSeparator(); //分隔线 m1.add(jmi2); m1.add(jmi3); JMenuItem jmi1_1 = new JMenuItem("硬件"); // 设置三级菜单项 JMenuItem jmi1_2 = new JMenuItem("软件"); JMenuItem jmi1_3 = new JMenuItem("杀毒"); JMenuItem jmi1_4 = new JMenuItem("确认"); JMenuItem jmi1_5 = new JMenuItem("取消"); jmi1.add(jmi1_1); //将三级菜单项放入菜单中 jmi1.addSeparator(); //分隔线 jmi1.add(jmi1_2); jmi1.add(jmi1_3); JCheckBoxMenuItem jCheckBoxMenuItem1 = new JCheckBoxMenuItem("复选框菜单项"); //设置三级复选框 jmi1.add(jCheckBoxMenuItem1); jmi3.add(jmi1_4); jmi3.add(jmi1_5); jmi1_4.addActionListener(new MyListener()); //点击确认退出程序 } } class MyListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }
二、连接数据库
1、下载MySQL的驱动.jar包
下载地址:https://dev.mysql.com/downloads/connector/j/
2、IDEA导入连接MySQL的驱动.jar包
3、连接数据库
package www.qyl.lesson03; import java.sql.Connection; import java.sql.DriverManager; import java.sql.*; public class TestMysqlConnection { public static void main(String[] args) { String url = "jdbc:mysql://127.0.0.1:3306/db_student"; String user = "root"; String pass = "000000"; try { // Class.forName("com.mysql.jdbc.Driver"); //旧驱动程序类,不推荐 Class.forName("com.mysql.cj.jdbc.Driver"); //驱动程序通过sql自动注册,驱动程序类 Connection conn = DriverManager.getConnection(url,user,pass); //连接数据库 Statement stmt = conn.createStatement(); //用于将sql语句发送到数据库中 String sql = "select * from t_major"; ResultSet rs = stmt.executeQuery(sql); //执行sql语句 System.out.println(rs.next()); //结果为true while (rs.next()){ // String name = rs.getString("major_name"); //通过字段检索 // System.out.println(name); //输出查询字段 System.out.println(rs.getInt(1) + "\t" + rs.getString(2)); //通过字段的索引检索 } conn.close(); rs.close(); stmt.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException throwables) { throwables.printStackTrace(); } } }
欢迎加入云社区
总结
我是秋意临,欢迎大家一键三连、加入云社区
我们下期再见(⊙o⊙)!!!