0
我在C盤上獲得了一個背景,但即使代碼看起來不錯,它也沒有顯示出來。它可以運行,但有幾個問題。未出現Java背景
1:背景犯規秀(你可以下載一個隨機bg.jpg文件,看看它是否工作)
2:文本框不居中,可悲......
3:我不能得到文本顯示在文本框旁邊,如「User/PW」或「Welcome」等。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Log extends JFrame {
JButton b1;
JLabel l1;
public static void main(String[] args) {
Log frameTabel = new Log();
}
JButton blogin = new JButton("Login");
JPanel panel = new JPanel();
JTextField txuser = new JTextField(15);
JPasswordField pass = new JPasswordField(15);
Log(){
super("Login Autentification");
Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());
setSize(xSize,ySize);
setLocationRelativeTo(null);
panel.setLayout (null);
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("C:\\bg.jpg"));
add(background);
background.setLayout(new FlowLayout());
txuser.setBounds(70,30,150,20);
pass.setBounds(70,65,150,20);
blogin.setBounds(110,100,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) {
String puname = txuser.getText();
String ppaswd = pass.getText();
if(puname.equals("test") && ppaswd.equals("12345")) {
newframe regFace =new newframe();
regFace.setVisible(true);
dispose();
} else {
JOptionPane.showMessageDialog(null,"Wrong Password/Username");
txuser.setText("");
pass.setText("");
txuser.requestFocus();
}
}
});
}
}