public class eventtwo extends JFrame implements ActionListener { private JLabel usJlable,upJlable; private JTextField usTextFiled; private JPasswordField upTextFiled; private JButton loginButton; public eventtwo(){ usJlable =new JLabel("用户名:"); upJlable =new JLabel("密码:"); usTextFiled =new JTextField(15); upTextFiled=new JPasswordField( 15); loginButton =new JButton("登录"); setLayout(new FlowLayout()); this.add(usJlable); add(usTextFiled); add(upJlable); add(upTextFiled); add(loginButton); loginButton.addActionListener(this); setSize(400,400); setVisible(true); setLocationRelativeTo(null); } public static void main(String [] args) { new eventtwo(); } public void actionPerformed(ActionEvent e) { useOpea s=new useOpea(); Object [] a=new String[2]; a[0]=usTextFiled.getText(); a[1]=String.valueOf(upTextFiled.getPassword()); try { int n=s.query("select * from user where userName=? and userPass=?",a); if(n<=0) { JOptionPane.showMessageDialog(null, "登录失败"); }else { JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(loginButton); frame.dispose(); new eventfive(); } System.out.println(n); }catch(SQLException t) { t.printStackTrace(); } } }在该代码中为窗口设置背景图片
时间: 2024-03-16 10:45:59 浏览: 71
可以在构造方法中添加以下代码来为窗口设置背景图片:
```
// 加载背景图片
ImageIcon background = new ImageIcon("background.jpg");
// 创建一个标签,并将背景图设置为标签的图标
JLabel label = new JLabel(background);
// 设置标签的位置和大小
label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
// 将标签添加到窗口中
this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
// 将窗口设置为透明
((JPanel) this.getContentPane()).setOpaque(false);
// 设置窗口大小
this.setSize(background.getIconWidth(), background.getIconHeight());
```
其中,"background.jpg" 是背景图片的文件路径,可以根据实际情况修改。需要注意的是,添加背景图片后,窗口中原有的组件可能会被遮挡,可以通过设置组件的 z-order(Z轴顺序)来调整显示顺序。例如,将上面代码中的登录按钮的 z-order 设置为比标签更高的值,即可让登录按钮显示在标签之上。
阅读全文