import javax.swing.*;
import java.awt.event.*;
import java.util.Scanner;
import java.awt.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
//////////////////////////////////
// 3nriched Games Presents: //
// MIPS The Mouse!! //
//////////////////////////////////
public class mipsMouseGUI extends JFrame implements ActionListener
{
private static String ThePDub = ("mouse"); //the password
JPasswordField pass;
JPanel panel;
JButton btnEnter;
JLabel lblpdub;
public mipsMouseGUI()
{
BufferedImage image = null;
try { //attempts to read picture from the folder
image = ImageIO.read(getClass().getResource("/mousepics/mousepic.png"));
} catch (IOException e) { //catches exceptions
e.printStackTrace();
}
setIconImage(image); //sets icon picture
setTitle("Mips The Mouse Login");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(); //creates a panel
ImagePanel panel = new ImagePanel(new ImageIcon("/mousepics/backgroundspacepic.jpeg").getImage());
pass = new JPasswordField(5); //sets password length to 5
pass.setEchoChar('@'); //hide characters as @ symbol
pass.addActionListener(this); //adds action listener
add(panel); //adds panel to frame
btnEnter = new JButton("Enter"); //creates a button
btnEnter.addActionListener(this);// Register the action listener.
lblpdub = new JLabel(" Your Password: "); // label that says enter password
panel.add(lblpdub, BorderLayout.CENTER);// adds label and inputbox
panel.add(pass, BorderLayout.CENTER); // to panel and sets location
panel.add(btnEnter, BorderLayout.CENTER); //adds button to panel
pack(); // packs controls and
setLocationRelativeTo(null); // Implicit "this" if inside JFrame constructor.
setVisible(true); // makes them visible (duh)
}
public void actionPerformed(ActionEvent a)
{
Object source = a.getSource();
//char array that holds password
char[] passy = pass.getPassword();
//characters array to string
String p = new String(passy);
//determines if user entered correct password
if(p.equals(ThePDub))
{
JOptionPane.showMessageDialog(null, "Welcome beta user: USERNAME.");
}
else
JOptionPane.showMessageDialog(null, "You have enter an incorrect password. Please try again.");
}
class ImagePanel extends JPanel {
private Image img;
public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
}
答案 0 :(得分:1)
图像对我来说很好。您是否添加了任何调试代码以显示图像的首选大小,以确保正确读取它。
此外,一旦图像正常工作,您将看不到实际添加到图像面板的组件。你知道下面的代码行是做什么的,或者你只是从某个地方复制它?摆脱它:
setLayout(null);
为什么要创建一个空面板然后创建一个ImagePanel?
panel = new JPanel(); //creates a panel
ImagePanel panel = new ImagePanel(new ImageIcon("/mousepics/backgroundspacepic.jpeg").getImage());