我正在尝试为我的应用程序创建登录页面并且我做得很好但是我无法将图像添加到我的JFrame中,这是我的登录页面代码....
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.*;
public class log extends JFrame {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://sqldatabase.com/databasename";
// Database credentials
static final String USER = "usernamr";
static final String PASS = "pass";
public static void main(String[] args) throws IOException {
log frameTabel = new log();
}
JButton blogin = new JButton("Login");
JPanel panel = new JPanel();
JLabel label = new JLabel();
JTextField txuser = new JTextField(15);
JPasswordField pass = new JPasswordField(15);
log() throws IOException{
super("Login Autentification");
setSize(500,500);
setLocation(300,280);
panel.setLayout (null);
//ImageIcon image = new ImageIcon("image.jpeg");
//JLabel hangman = new JLabel(new ImageIcon(urlOfImageFile));
//panel.add(image, BorderLayout.NORTH);
//Image image = ImageIO.read(new File("F:\\IModubytes\\Images\\1.jpg"));
//JLabel picLabel = new JLabel(new ImageIcon(image));
//panel.add(picLabel);
//panel.repaint();
txuser.setBounds(300,100,150,20);
pass.setBounds(300,135,150,20);
blogin.setBounds(380,170,80,20);
panel.add(blogin);
panel.add(txuser);
panel.add(pass);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
actionlogin();
}
public void actionlogin(){
blogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String puname = txuser.getText();
String ppaswd = pass.getText();
String sql = "SELECT * FROM EmpDetails WHERE id="+puname;
System.out.println(puname);
ResultSet rs = stmt.executeQuery(sql);
String pw = null;
while(rs.next()) {
pw = rs.getString("pass");
}
rs.close();
//if(puname.equals("test") && ppaswd.equals(pw)) {
if(ppaswd.equals(pw)) {
newFrame regFace =new newFrame();
regFace.setVisible(true);
dispose();
} else {
JOptionPane.showMessageDialog(null,"Wrong Password / Username");
txuser.setText("");
pass.setText("");
txuser.requestFocus();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
任何人都可以帮我解决如何在此添加图片....
答案 0 :(得分:3)
您已经拥有加载图片的代码并将标签添加到相框中。
因此,下一步是将组件添加到标签,然后将标签添加到内容窗格,而不是将组件添加到面板并将面板添加到内容窗格。
另外,不要使用空布局! Swing旨在与布局管理器一起使用。阅读Layout Managers上Swing教程中的部分,了解更多信息和工作示例。