try{ appearance++; String lookAndFeel=null; switch (appearance){ case 1://windows lookAndFeel="com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; lb_appearance.setText("<html>当前外观:<br>Windows</html>"); break; case 2: //Nimbus风格 lookAndFeel="com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"; lb_appearance.setText("<html>当前外观:<br>Nimbus</html>"); break; case 3: //Windows Classic风格 lookAndFeel="com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"; lb_appearance.setText("<html>当前外观:<br>Windows Classic</html>"); break; case 4://Motif风格 lookAndFeel="com.sun.java.swing.plaf.motif.MotifLookAndFeel"; lb_appearance.setText("<html>当前外观:<br>Motif</html>"); break; case 5: //默认风格 lookAndFeel="javax.swing.plaf.metal.MetalLookAndFeel"; lb_appearance.setText("<html>当前外观:<br>默认</html>"); break; } UIManager.setLookAndFeel(lookAndFeel); SwingUtilities.updateComponentTreeUI(mainFrame); if (appearance==5) appearance=0; }catch(Exception ex) { System.out.println(ex); }
时间: 2024-02-10 16:33:48 浏览: 28
这代码是用于在Java Swing界面中切换不同的外观(Look and Feel)。它通过增加一个名为“appearance”的整数变量来实现循环切换不同的外观样式。具体来说,它使用了不同的类来设置不同的外观,例如Windows、Nimbus、Windows Classic和Motif等。最后,它通过调用SwingUtilities.updateComponentTreeUI(mainFrame)来更新界面组件的UI。如果出现异常,则会将异常信息输出到控制台。
相关问题
public+boolean+delete(int+id)+( Connection+conn+.+null; statement+stmt+.+null; ResultSet+rs+=+nul
根据提供的引用内容,以下是一个public boolean delete(int id)方法的例子,其中包含了Connection、Statement和ResultSet的使用:
```java
public boolean delete(int id) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/users", "username", "password");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
String sql = "DELETE FROM users WHERE id = " + id;
int rows = stmt.executeUpdate(sql);
if (rows > 0) {
return true;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
```
注释一下这段代码: public static boolean check(String name, String password) { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } boolean result = false; try (Connection c = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Test", "kcsj001", "Ljj3934639559!"); Statement s = c.createStatement(); ) { String sql = "select * from 用户信息 where name = '" + name +"' and password = '" + password+"'"; ResultSet rs = s.executeQuery(sql); if(rs.next()) result = true; } catch (SQLException e) { e.printStackTrace(); } return result; }
好的,下面是对该段代码的注释:
```
public static boolean check(String name, String password) { // 定义一个静态方法 check,该方法接收两个参数 name 和 password
try {
Class.forName("com.mysql.jdbc.Driver"); // 加载 MySQL JDBC 驱动程序
} catch (ClassNotFoundException e) { // 捕获 ClassNotFoundException 异常
e.printStackTrace(); // 打印异常堆栈信息
}
boolean result = false; // 定义一个布尔类型的变量 result 并初始化为 false
try (Connection c = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Test", // 连接到本地 SQL Server 数据库,数据库名称为 Test
"kcsj001", // 数据库用户名
"Ljj3934639559!"); // 数据库密码
Statement s = c.createStatement(); // 创建 Statement 对象,用于向数据库发送 SQL 语句
) {
String sql = "select * from 用户信息 where name = '" + name +"' and password = '" + password+"'"; // 定义查询语句
ResultSet rs = s.executeQuery(sql); // 执行查询语句并返回结果集
if(rs.next()) // 如果结果集中有下一行数据,则返回 true
result = true;
} catch (SQLException e) { // 捕获 SQLException 异常
e.printStackTrace(); // 打印异常堆栈信息
}
return result; // 返回查询结果
}
```
需要注意的是,该段代码存在 SQL 注入漏洞,建议使用 PreparedStatement 对象来代替 Statement 对象以提高代码安全性。