2014-03-05 69 views
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(); 
} 

} 
}); 
} 
} 

回答

0

這應該是你的代碼:「他爲什麼要使用XSIZE和中減去75或40」

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

也許你會問自己好吧,我把它分成兩部分,所以我們在窗口的一半,現在我們減去文本框寬度的一半,所以我們有一半左右的寬度。

對於用戶的文本,我只是添加JLabel並將其添加到面板。

圖片的背景,我從here

tooked它,我建議你多想一點,你想要什麼,你有什麼,在網上搜索,「原因有大部分的時間回答你的問題。

反正你會得到這樣的事情:

enter image description here

(「用戶」的形象下被隱藏的標籤,評論無效paint方法來得到它顯示,好了,你的背景圖片和標籤,所有缺少的是修復該錯誤)

希望可以幫助