自qt以来见到了更强(菜)的做界面语言! 首先记得写各种变量并且统一加private 然后把这些东西拍在构造函数开头: 1 2 3 4 5 6 super ("title" ); this .setSize(800 ,300 ); this .setDefaultCloseOperation(); this .setLocationRelativeTo(null ); this .setLayout(new xxxLayout()); this .setVisible(true );
为JPanel加小标题: 1 2 Border titleBorder = BorderFactory.createTitledBorder("数据操作" ); panel_operate.setBorder(titleBorder);
int/String转换: 1 2 Integer.parseInt(String s) edge.toString()
拆文件中字符串: 1 2 String arr[4 ],s="11 45 14 1919810" ; arr=s.spilt(" " );
对话框: 消息对话框
1 2 JOptionPane.showMessageDialog(this (填一个组件),"主体显示的内容" ); JOptionPane.showMessageDialog(this ,"neirong" ,"haoye" , JOptionPane.INFORMATION_MESSAGE);
JOptionPane.INFORMATION_MESSAGE
:
JOptionPane.PLAIN_MESSAGE
:
JOptionPane.ERROR_MESSAGE
:
JOptionPane.WARNING_MESSAGE:
JOptionPane.QUESTION_MESSAGE
: 诶 这个挺可爱的
Component Container Jcomponent Window JFrame 最好使用this.getContentPane().add()
添加组件
JPanel JDialog JLabel 一般就是显示一行文本(idea里面中文会乱码注意)
JTextField 一个文本输入区,构造函数的参数设置输入区宽度(如果不写就只有一点点大)
JTextArea 记得button.addActionListener(this)
JCheckBox 复选按钮
单选按钮
一组按钮才能实现单选
1 2 3 4 ButtonGroup bg=new ButtonGroup(); bg.add(rb_male=new JRadioButton("男" )); bg.add(rb_female=new JRadioButton("女" )); rb_male.setSelected(true );
JList&DefaultListModel 1 2 model=new DefaultListModel<>(); list=new JList<>(this .model);
其他方法在课本P165 常用
JTable 太智障了,问就智障
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 JTable jt; DefaultTableModel model; model=new DefaultTableModel(title,20 ); jt=new JTable(model);protected void query (String name) { String sql=null ; while (model.getRowCount()!=0 ) model.removeRow(0 ); if (name==null ){ sql="SELECT * FROM mydb.goods" ; } else { sql= "SELECT * FROM mydb.goods WHERE type LIKE '%" +name+"%'" ; } try { st= stmt.executeQuery(sql); while (st.next()){ String[] s=new String[]{st.getString(1 ),st.getString(2 ), st.getString(3 ),st.getString(4 ), st.getString(5 )}; model.addRow(s); } } catch (SQLException e) { e.printStackTrace(); } }
JComboBox 组合框
1 2 JComboBox jcbox=new JComboBox<>(new String[]{"品牌" ,"型号" ,"颜色" ,"马力" });int val=jcbox.getSelectedIndex();
布局类:
BorderLayout 边布局,有东西南北的
FlowLayout 流布局,一直往后加
GridLayout 网格布局,两个参数rows行 cols列
中间容器: 带有滚动条的视图容器,为其他组件提供可滚动视图,只要组件内容超过视图大小就会自动显示水平或垂直滚动条
1 panel1.add(new JScrollPane(list));
JSpiltPane 分割窗格
java-sql server从入门到完全放弃到mySQL真香
DriverManager驱动管理
负责注册驱动 static void registerDriver(Driver driver)
Class.forName(“com.mysql.jdbc.Driver”);//加载对应包中的Driver类,使用registerDriver方法
但由于jdbc包已经自动注册了驱动所以可以不写,并且会有如下报错:
1 Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver' . The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
获取数据库连接 static Connection getConnection(String url,String user,String password)
url:指定连接路径jdbc:mysql://localhost:3306(ip地址:端口号)/MyDB(数据库名)
在hexo中这个还是有点熟悉
Connection
获取执行sql的对象
CreateStatement() 创建Statement对象,用于将sql语句发送到数据库
prepareStatement(String sql)
管理事务
开启事务 setAutoCommit(boolean autoCommit) 参数false即关闭自动提交,开启事务
提交事务 commit()
回滚事务 rollback()
Statement 执行静态sql并返回结果对象
执行sql
boolean execute(String sql) 执行任意语句 true:第一个结果是ResultSet;false:更新计数或没有结果(?)
int executeUpdate(String sql) 执行DML (insert update delete) & DDL(create alter drop) (不常用);返回值:影响的行数;DDL语句没有返回结果(返回0)
Result executeQuery(String sql) 执行DQL(select)
ResultSet 结果集对象,用类似游标读取
boolean next():游标向下移一行(默认在第0行,向下一行之后才能获取表中数据)(若此行为空,返回false)
以及类似next的 详见p321
getxxx(参数):获取数据 (getString 获取String类型数据);参数:int(table列号,从1开始 )or String(table列名)
PreparedStatement(Statement的子类) 执行动态(预编译)sql并返回
导包:C:\Program Files (x86)\MySQL\Connector J 8.0
idea:复制这个文件到目录下,右键lib包 add as library
eclipse:https://www.cnblogs.com/fnng/archive/2011/07/18/2110023.html
工程文件夹右键->build path->add external archives选择包路径导入
草,好奇幻,我从我写的另一个文件里复制过去会自动import,从这就不行。。
类开头写: 1 2 3 Connection connection=null ; Statement stmt = null ; ResultSet st=null ;
连接数据库: 1 2 3 4 5 6 7 8 try { Class.forName("com.mysql.jdbc.Driver" ); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/MyDB" , "root" , "zaq123456" ); stmt = connection.createStatement(); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); }
一般insert: 1 2 3 4 5 6 7 8 protected void insert (Person per) { String sql="INSERT INTO mydb.person VALUES(" +per.toString()+")" ; try { stmt.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } }
一般query: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 protected void query (String name) { model.removeAllElements(); String sql=null ; if (name==null ){ sql="SELECT * FROM mydb.person" ; } else { sql= "SELECT * FROM mydb.person WHERE name LIKE '%" +name+"%'" ; } try { st= stmt.executeQuery(sql); while (st.next()){ String s=st.getString(1 )+"," +st.getString(2 )+"," + st.getString(3 )+"," +st.getString(4 )+"," + st.getString(5 )+"," +st.getString(6 ); model.addElement(s); } } catch (SQLException e) { e.printStackTrace(); } }
释放资源: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 private void setDefaultCloseOperation () { if (st!=null ){ try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if (stmt!=null ){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection!=null ){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } setDefaultCloseOperation(EXIT_ON_CLOSE); }
线程:也写在这算了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 if (e.getSource()==button_count){ if (thread.getState()!=Thread.State.NEW) { thread=new Thread(this ); } thread.start(); button_count.setEnabled(false ); button_stop.setEnabled(true ); } if (e.getSource()==button_stop){ thread.interrupt(); utton_stop.setEnabled(false ); button_count.setEnabled(true ); } @Override public void run () { double i=0 ; while (true ){ try { label_thread.setText(String.format("%.1f" ,i)); i+=0.1 ; Thread.sleep(100 ); } catch (InterruptedException ex) { break ; } } }
字节&字符流:这绝对是最没有格式的一篇 字节输入(即读取)
1 2 3 4 5 6 7 8 9 10 11 12 FileInputStream fis= null ;int len; String ans="" ;try { fis = new FileInputStream("src\\JavaExam\\re2017\\types.txt" ); while ((len=fis.read())!=-1 ){ ans+=(char )len; } System.out.println(ans); }catch (IOException e) { e.printStackTrace(); }
字节输出(即写入)
1 2 3 4 5 6 7 8 9 10 11 12 try { fos =new FileOutputStream("src\\javaExam\\re2018\\out.txt" ,true ); for (int i=0 ;i< model.size();i++){ String s=model.getElementAt(i)+'\n' ; fos.write(s.getBytes(StandardCharsets.UTF_8)); } } catch (IOException ex) { JOptionPane.showMessageDialog(this ,"未找到文件" ); ex.printStackTrace(); }