package dormitory_20221550; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class choose { public static void main(String[] args) { new choose1(); } } class choose1 extends JFrame implements ActionListener{ JLabel lbl_p1; JButton btn1; JButton btn2; JButton btn3; JPanel jp1; String path ="src/dormitory_20221550/picture/p1"; public choose1() { jp1=new JPanel(); setTitle("欢迎进入上海电力大学宿舍管理系统"); btn1=new JButton("学生登录"); btn2=new JButton("职工登录"); btn3=new JButton("访客申请"); setLayout(new BorderLayout()); btn1.addActionListener(this); btn2.addActionListener(this); btn3.addActionListener(this); jp1.add(btn1); jp1.add(btn2); jp1.add(btn3); add(jp1,BorderLayout.SOUTH); lbl_p1=new JLabel(new ImageIcon(path+"p1.png")); add(lbl_p1,BorderLayout.CENTER); setBounds(300,150,600,400); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { } }修改插入图片的代码
时间: 2024-01-24 15:02:05 浏览: 120
您需要将代码中的图片路径修改为正确的路径,同时将图片文件放置到指定位置。请将以下代码:
```java
String path ="src/dormitory_20221550/picture/p1";
lbl_p1=new JLabel(new ImageIcon(path+"p1.png"));
```
修改为:
```java
String path ="dormitory_20221550/picture/p1/"; // 图片路径
lbl_p1=new JLabel(new ImageIcon(getClass().getResource(path + "p1.png"))); // 加载图片
```
然后将图片文件放置到项目目录下的 `dormitory_20221550/picture/p1/` 文件夹中。这样就可以插入图片了。
相关问题
import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class DormitoryManagementLogin extends JFrame implements ActionListener { private JLabel titleLabel, usernameLabel, passwordLabel; private JTextField usernameField; private JPasswordField passwordField; private JButton loginButton, resetButton; public DormitoryManagementLogin() { super("Dormitory Management Login"); titleLabel = new JLabel("Dormitory Management Login"); titleLabel.setFont(new Font("Arial", Font.BOLD, 20)); titleLabel.setForeground(Color.BLUE); titleLabel.setHorizontalAlignment(JLabel.CENTER); usernameLabel = new JLabel("Username:"); passwordLabel = new JLabel("Password:"); usernameField = new JTextField(); passwordField = new JPasswordField(); loginButton = new JButton("Login"); resetButton = new JButton("Reset"); loginButton.addActionListener(this); resetButton.addActionListener(this); JPanel panel = new JPanel(new GridLayout(3, 2)); panel.add(usernameLabel); panel.add(usernameField); panel.add(passwordLabel); panel.add(passwordField); panel.add(loginButton); panel.add(resetButton); add(titleLabel, BorderLayout.NORTH); add(panel, BorderLayout.CENTER); setSize(400, 300); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == loginButton) { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); // 在这里添加登录验证逻辑 JOptionPane.showMessageDialog(this, "登录成功!"); // 登录成功后跳转到宿舍管理主界面 new DormitoryManagementMain(); dispose(); } else if (e.getSource() == resetButton) { usernameField.setText(""); passwordField.setText(""); } } public static void main(String[] args) { new DormitoryManagementLogin(); }}
这段代码是一个Java GUI程序,实现了一个宿舍管理系统的登录界面。界面包含一个标题、用户名和密码输入框、登录和重置按钮。当用户点击登录按钮时,会调用actionPerformed方法,该方法会获取用户输入的用户名和密码,并进行登录验证。如果登录成功,会弹出一个消息框提示登录成功,并跳转到宿舍管理系统的主界面。如果用户点击重置按钮,用户名和密码输入框会被清空。主函数中创建了一个DormitoryManagementLogin对象,从而启动了整个程序。
校园宿舍管理系统java+swing+jdbc代码
以下是一个简单的校园宿舍管理系统的Java代码,使用了Swing和JDBC库。请注意,这只是一个简单的示例代码,您需要根据自己的需求进行修改和完善。
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class DormitoryManagementSystem extends JFrame implements ActionListener {
JLabel title, nameLabel, genderLabel, roomNoLabel, dateLabel;
JTextField nameField, roomNoField, dateField;
JComboBox genderBox;
JButton addBtn, viewBtn, clearBtn;
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
public static void main(String[] args) {
new DormitoryManagementSystem();
}
public DormitoryManagementSystem() {
super("校园宿舍管理系统");
Container container = getContentPane();
container.setLayout(new GridLayout(6, 2));
title = new JLabel("宿舍管理系统", JLabel.CENTER);
nameLabel = new JLabel("姓名", JLabel.CENTER);
genderLabel = new JLabel("性别", JLabel.CENTER);
roomNoLabel = new JLabel("房间号", JLabel.CENTER);
dateLabel = new JLabel("入住日期(yyyy-mm-dd)", JLabel.CENTER);
nameField = new JTextField();
genderBox = new JComboBox(new String[] { "男", "女" });
roomNoField = new JTextField();
dateField = new JTextField();
addBtn = new JButton("添加");
viewBtn = new JButton("查看");
clearBtn = new JButton("清空");
addBtn.addActionListener(this);
viewBtn.addActionListener(this);
clearBtn.addActionListener(this);
container.add(title);
container.add(new JLabel());
container.add(nameLabel);
container.add(nameField);
container.add(genderLabel);
container.add(genderBox);
container.add(roomNoLabel);
container.add(roomNoField);
container.add(dateLabel);
container.add(dateField);
container.add(addBtn);
container.add(viewBtn);
container.add(clearBtn);
setSize(400, 250);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dormitory", "root", "password");
} catch (Exception e) {
System.out.println(e);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addBtn) {
String name = nameField.getText();
String gender = genderBox.getSelectedItem().toString();
String roomNo = roomNoField.getText();
String date = dateField.getText();
try {
pst = conn.prepareStatement("insert into student(name, gender, roomNo, date) values(?,?,?,?)");
pst.setString(1, name);
pst.setString(2, gender);
pst.setString(3, roomNo);
pst.setString(4, date);
pst.executeUpdate();
JOptionPane.showMessageDialog(this, "添加成功");
} catch (SQLException ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(this, "添加失败");
}
} else if (e.getSource() == viewBtn) {
try {
pst = conn.prepareStatement("select * from student");
rs = pst.executeQuery();
StringBuilder sb = new StringBuilder();
while (rs.next()) {
sb.append(rs.getString("name")).append("\t");
sb.append(rs.getString("gender")).append("\t");
sb.append(rs.getString("roomNo")).append("\t");
sb.append(rs.getString("date")).append("\n");
}
JOptionPane.showMessageDialog(this, sb.toString());
} catch (SQLException ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(this, "查询失败");
}
} else if (e.getSource() == clearBtn) {
nameField.setText("");
genderBox.setSelectedIndex(0);
roomNoField.setText("");
dateField.setText("");
}
}
}
```
该代码使用了MySQL数据库,需要首先安装MySQL并创建一个名为dormitory的数据库。请将代码中的“password”替换为您的MySQL密码。
该代码实现了一个简单的宿舍管理系统,包括添加学生信息、查看学生信息和清空输入框等功能。您可以根据自己的需求进行修改和扩展。
阅读全文