1
我回来了又一个非常愚蠢的问题。我已经阅读了一些其他的stackoverflow帖子,并试图按照建议,但无济于事。我无法让JPanel背景更改为图像!我试图将图像移动到我的c:\,所以不是这样。设置JPanel/JFrame背景图像,我的第一次
请帮助和提前致谢!
登录代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class login implements ActionListener{
JTextField gusername;
JTextField gpassword;
static String username;
static String password;
void logini() throws IOException {
JFrame window = new JFrame("Login");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300, 250);
window.setResizable(false);
window.setVisible(true);
JPanel mainp = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
window.add(mainp);
BufferedImage myPicture = ImageIO.read(new File("c:\bgd.png"));
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
mainp.add(picLabel);
c.gridx = 0;
c.gridy = 1;
gusername = new JTextField();
gusername.setText("Username");
mainp.add(gusername, c);
c.gridx = 0;
c.gridy = 2;
gpassword = new JTextField();
gpassword.setText(" password ");
mainp.add(gpassword, c);
c.gridx = 0;
c.gridy = 3;
JButton login = new JButton("Login");
mainp.add(login, c);
login.addActionListener(this);
login.setActionCommand("ok");
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("ok")){
try {
this.username = (gusername.getText());
this.password = (gpassword.getText());
System.out.println("0");
}
catch(NumberFormatException ex){
System.out.println("ERROR: Could not preform function: 7424");
}
}
}
}
错误:
Exception in thread "main" javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at login.logini(login.java:34)
at Main.main(Main.java:10)
非常感谢的人! :) – 2012-07-06 18:16:43
另一个建议是从您的应用程序中读取字符串,并使用该字符串代替硬编码您的位置。确保你将程序移动到哪里,移动图片的位置,否则它将无法工作。 – 2012-07-06 18:17:35
@Erick Robertson现在图像已经启动,为什么我的其他组件不出现? – 2012-07-06 18:19:08