Java背景没有出现

时间:2014-03-05 16:11:05

标签: java image swing background

我有一个关于C盘的背景但是它没有出现,即使代码看起来很好。它可以运行,但有几个问题。

1:后台没有显示(你可以下载一个随机的bg.jpg文件来查看它是否有效)

2:文本框不居中,可悲......

3:我无法在文本框旁边显示文本,例如" User / PW"或者"欢迎"什么的。

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();
}

}
});
}
}

1 个答案:

答案 0 :(得分:0)

这应该是你的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.imageio.*;
import java.awt.Graphics;

public class Log extends JFrame {
JButton b1;
JLabel l1;
Image bgImage;
JLabel user = new JLabel("User");

JButton blogin = new JButton("Login");
JPanel panel = new JPanel();
JTextField txuser = new JTextField(15);
JPasswordField pass = new JPasswordField(15);
public static void main(String[] args) {
    Log frameTabel = new Log("bg.jpg");
}

Log(String bgImg){
    super("Login Autentification");
    Toolkit tk = Toolkit.getDefaultToolkit();  
    int xSize = ((int) tk.getScreenSize().getWidth());  
    int ySize = ((int) tk.getScreenSize().getHeight());  
    try{
        bgImage = ImageIO.read(new File(bgImg));
    }
    catch(IOException e){}

    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(Math.round(xSize/2) - 75,30,150,20);
    pass.setBounds(Math.round(xSize/2) - 75,65,150,20);
    blogin.setBounds(Math.round(xSize/2) - 40,100,80,20);

    user.setBounds(Math.round(xSize/2) - 150, 30, 50, 20);

    panel.add(user);
    panel.add(blogin);
    panel.add(txuser);
    panel.add(pass);

    getContentPane().add(panel);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}
public void paint(Graphics g){
    super.paint(g);
    g.drawImage(bgImage, 0, 0, panel);
}
}

也许你问自己“他为什么使用xSize并减去75或40?”好吧,我把它分成两半,所以我们在窗口的一半,现在我们减去文本框宽度的一半,所以我们左边有一半宽,右边有一半。

对于User的文本,我刚刚添加了JLabel并将其添加到面板中。

图片背景我从here

中提取了它

我建议你多考虑一下你想要的和你拥有的东西,在网上搜索,因为大多数情况下你的问题都有答案。

无论如何你会得到这样的东西:

enter image description here

(“user”的标签隐藏在图片下面,注释void paint方法让它显示,好吧,你得到背景图片和标签,所有缺失都是修复那个bug)

希望有所帮助