java窗口背景添加无法显示
packagesrc;importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;classDengLuext...
package src;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class DengLu extends JFrame implements ActionListener
{
private JLabel label1,label2,label3,backing;
//private JTextField text1,text2,text3;
private JTextField text1,text3;
private JButton button1,button2;
private JPasswordField psw;//密码设置
private ImageIcon img;
private Icon imgIcon;
DengLu(String s)
{
super(s);
Container c=this.getContentPane();
c.setLayout(null);
img=new ImageIcon("img/001.jpj");
imgIcon=img;
backing=new JLabel(imgIcon );//添加图片
backing.setBounds(0,0,300,200);
label1=new JLabel("用户名:");
label2=new JLabel("密 码:");
label3=new JLabel("显 示:");
text1=new JTextField(10);
psw=new JPasswordField(10);
text3=new JTextField(10);
button1=new JButton("确认");
button2=new JButton("重置");
label1.setBounds(60,20,60,20);
label2.setBounds(60,50,60,20);
label3.setBounds(60,80,60,20);
text1.setBounds(120,20,80,20);
psw.setBounds(120,50,80,20);
text3.setBounds(120,80,80,20);
button1.setBounds(60,120,60,20);
button2.setBounds(130,120,60,20);
c.add(label1);
c.add(label2);
c.add(label3);
c.add(text1);
c.add(psw);
c.add(text3);
c.add( button1);
c.add( button2);
c.add(backing);
button1.addActionListener(this);
button2.addActionListener(this);
//text2.setEchoChar('*');
text3.setEditable(false);
this.setSize(300,200);
this.setLocation(200,200);
this.setResizable(false);//控制窗口的大小不变化
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
String word=text1.getText();
int number=Integer.parseInt(psw.getText());
if(word.equals("computer")&&number==123456)
{
text3.setText("welcome");
}
else
{
text3.setText("密码错误");
}
}
else
{
text1.setText(null);
psw.setText(null);
text3.setText(null);
}
}
}
public class Windowtest
{
public static void main (String[] args)
{
new DengLu("登录界面");
}
} 展开
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class DengLu extends JFrame implements ActionListener
{
private JLabel label1,label2,label3,backing;
//private JTextField text1,text2,text3;
private JTextField text1,text3;
private JButton button1,button2;
private JPasswordField psw;//密码设置
private ImageIcon img;
private Icon imgIcon;
DengLu(String s)
{
super(s);
Container c=this.getContentPane();
c.setLayout(null);
img=new ImageIcon("img/001.jpj");
imgIcon=img;
backing=new JLabel(imgIcon );//添加图片
backing.setBounds(0,0,300,200);
label1=new JLabel("用户名:");
label2=new JLabel("密 码:");
label3=new JLabel("显 示:");
text1=new JTextField(10);
psw=new JPasswordField(10);
text3=new JTextField(10);
button1=new JButton("确认");
button2=new JButton("重置");
label1.setBounds(60,20,60,20);
label2.setBounds(60,50,60,20);
label3.setBounds(60,80,60,20);
text1.setBounds(120,20,80,20);
psw.setBounds(120,50,80,20);
text3.setBounds(120,80,80,20);
button1.setBounds(60,120,60,20);
button2.setBounds(130,120,60,20);
c.add(label1);
c.add(label2);
c.add(label3);
c.add(text1);
c.add(psw);
c.add(text3);
c.add( button1);
c.add( button2);
c.add(backing);
button1.addActionListener(this);
button2.addActionListener(this);
//text2.setEchoChar('*');
text3.setEditable(false);
this.setSize(300,200);
this.setLocation(200,200);
this.setResizable(false);//控制窗口的大小不变化
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
String word=text1.getText();
int number=Integer.parseInt(psw.getText());
if(word.equals("computer")&&number==123456)
{
text3.setText("welcome");
}
else
{
text3.setText("密码错误");
}
}
else
{
text1.setText(null);
psw.setText(null);
text3.setText(null);
}
}
}
public class Windowtest
{
public static void main (String[] args)
{
new DengLu("登录界面");
}
} 展开
2个回答
展开全部
img=new ImageIcon("img/001.jpj");这句文件名"img/001.jpj"应该改成"img/001.jpg",即j改成g
更多追问追答
追问
嗯,但是改了还是不显示
追答
你确定,把001.jpg文件存储在当前Project目录下的img目录下吗?注意Project目录是指你建Project时建的目录,比如你的项目叫F,你的Project目录就是C:\tmp\F。换个说法,Project目录是你的src和bin目录的上一级目录。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询