java 窗体设置背景图片问题?(附上登陆界面代码,我想加个背景图片,求大神帮忙改改)
publicclassdengluextendsJFrame{privateJLabellab_zhanghao=newJLabel("账号:");privateJTex...
public class denglu extends JFrame {
private JLabel lab_zhanghao = new JLabel("账号:");
private JTextField jta_text = new JTextField();
Font lab = new Font("楷体",1,30);
private JLabel lat_password = new JLabel("密码:");
Font lat = new Font("楷体",1,30);
private JTextField jtb_text = new JTextField();
private JButton btn_register = new JButton("注册");
private JButton btn_land = new JButton("登陆");
Font btn = new Font("黑体",2,30);
public static int pd = 0;
public static String ak1, ak2;
private Container contentPane = this.getContentPane();
public denglu () {
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setTitle("登陆");
this.setResizable(false);
btn_register.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pd = 1;
ak1 = jta_text .getText();
ak2 = jtb_text .getText();
String fileName = "E:\\users.dat";
try {
Writer writer = new FileWriter(fileName);
} catch (IOException e1) {
e1.printStackTrace();
}
jta_text.setText("");
jtb_text.setText("");
JOptionPane.showMessageDialog(null,"注册成功! 您的用户名是"+ak1+"密码是"+ ak2);
}
});
btn_land.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(pd==1) {
if(ak1.equals(jta_text.getText()) && ak2.equals(jtb_text.getText())) {
JOptionPane.showMessageDialog(null, "登录成功!");
} else {
JOptionPane.showMessageDialog(null,"登陆失败!检查账号密码是否正确",null,
JOptionPane.ERROR_MESSAGE, null);
jta_text .setText("");
jtb_text .setText("");
}
}
}
});
init();
contentPane.add(lab_zhanghao);
contentPane.add(lat_password);
contentPane.add(jta_text);
contentPane.add(jtb_text);
contentPane.add(btn_register);
contentPane.add(btn_land);
contentPane.setLayout(null);
}
private void init() {
lab_zhanghao.setSize(200,100);
lab_zhanghao. setLocation(50,20);
lab_zhanghao.setFont(lab);
lat_password.setSize(200,100);
lat_password.setLocation(50,80);
lat_password.setFont(lat);
jta_text.setSize(190,30);
jta_text.setLocation(160,55);
jtb_text.setSize(190,30);
jtb_text.setLocation(160,115);
btn_register.setSize(120, 40);
btn_register.setLocation(60, 180);
btn_land.setSize(120, 40);
btn_land.setLocation(230, 180);
btn_register.setFont(btn);
btn_land.setFont(btn);
}
public static void main(String[] args)
throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
new denglu().setVisible(true);
}
} 展开
private JLabel lab_zhanghao = new JLabel("账号:");
private JTextField jta_text = new JTextField();
Font lab = new Font("楷体",1,30);
private JLabel lat_password = new JLabel("密码:");
Font lat = new Font("楷体",1,30);
private JTextField jtb_text = new JTextField();
private JButton btn_register = new JButton("注册");
private JButton btn_land = new JButton("登陆");
Font btn = new Font("黑体",2,30);
public static int pd = 0;
public static String ak1, ak2;
private Container contentPane = this.getContentPane();
public denglu () {
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setTitle("登陆");
this.setResizable(false);
btn_register.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pd = 1;
ak1 = jta_text .getText();
ak2 = jtb_text .getText();
String fileName = "E:\\users.dat";
try {
Writer writer = new FileWriter(fileName);
} catch (IOException e1) {
e1.printStackTrace();
}
jta_text.setText("");
jtb_text.setText("");
JOptionPane.showMessageDialog(null,"注册成功! 您的用户名是"+ak1+"密码是"+ ak2);
}
});
btn_land.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(pd==1) {
if(ak1.equals(jta_text.getText()) && ak2.equals(jtb_text.getText())) {
JOptionPane.showMessageDialog(null, "登录成功!");
} else {
JOptionPane.showMessageDialog(null,"登陆失败!检查账号密码是否正确",null,
JOptionPane.ERROR_MESSAGE, null);
jta_text .setText("");
jtb_text .setText("");
}
}
}
});
init();
contentPane.add(lab_zhanghao);
contentPane.add(lat_password);
contentPane.add(jta_text);
contentPane.add(jtb_text);
contentPane.add(btn_register);
contentPane.add(btn_land);
contentPane.setLayout(null);
}
private void init() {
lab_zhanghao.setSize(200,100);
lab_zhanghao. setLocation(50,20);
lab_zhanghao.setFont(lab);
lat_password.setSize(200,100);
lat_password.setLocation(50,80);
lat_password.setFont(lat);
jta_text.setSize(190,30);
jta_text.setLocation(160,55);
jtb_text.setSize(190,30);
jtb_text.setLocation(160,115);
btn_register.setSize(120, 40);
btn_register.setLocation(60, 180);
btn_land.setSize(120, 40);
btn_land.setLocation(230, 180);
btn_register.setFont(btn);
btn_land.setFont(btn);
}
public static void main(String[] args)
throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
new denglu().setVisible(true);
}
} 展开
2个回答
展开全部
lz 你好
代码修改如下:
(有注释)
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
//自定义一个面板类
class MyPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
//绘制一张背景图片 view.jpg是图片的路径 lz自己设定为自己想要添加的图片
Image image = new ImageIcon("view.jpg").getImage();
g.drawImage(image, 0, 0, this);
}
}
public class denglu extends JFrame {
//设置一个含有背景图片的面板
private MyPanel panel;
private JLabel lab_zhanghao = new JLabel("账号:");
private JTextField jta_text = new JTextField();
Font lab = new Font("楷体",1,30);
private JLabel lat_password = new JLabel("密码:");
Font lat = new Font("楷体",1,30);
private JTextField jtb_text = new JTextField();
private JButton btn_register = new JButton("注册");
private JButton btn_land = new JButton("登陆");
Font btn = new Font("黑体",2,30);
public static int pd = 0;
public static String ak1, ak2;
private Container contentPane = this.getContentPane();
public denglu () {
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setTitle("登陆");
this.setResizable(false);
btn_register.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pd = 1;
ak1 = jta_text .getText();
ak2 = jtb_text .getText();
String fileName = "E:\\users.dat";
try {
Writer writer = new FileWriter(fileName);
} catch (IOException e1) {
e1.printStackTrace();
}
jta_text.setText("");
jtb_text.setText("");
JOptionPane.showMessageDialog(null,"注册成功! 您的用户名是"+ak1+"密码是"+ ak2);
}
});
btn_land.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(pd==1) {
if(ak1.equals(jta_text.getText()) && ak2.equals(jtb_text.getText())) {
JOptionPane.showMessageDialog(null, "登录成功!");
} else {
JOptionPane.showMessageDialog(null,"登陆失败!检查账号密码是否正确",null,
JOptionPane.ERROR_MESSAGE, null);
jta_text .setText("");
jtb_text .setText("");
}
}
}
});
init();
panel = new MyPanel();
//将所有的组件添加到panel面板中
panel.add(lab_zhanghao);
panel.add(lat_password);
panel.add(jta_text);
panel.add(jtb_text);
panel.add(btn_register);
panel.add(btn_land);
panel.setLayout(null);
getContentPane().add(panel);
}
private void init() {
lab_zhanghao.setSize(200,100);
lab_zhanghao. setLocation(50,20);
lab_zhanghao.setFont(lab);
lat_password.setSize(200,100);
lat_password.setLocation(50,80);
lat_password.setFont(lat);
jta_text.setSize(190,30);
jta_text.setLocation(160,55);
jtb_text.setSize(190,30);
jtb_text.setLocation(160,115);
btn_register.setSize(120, 40);
btn_register.setLocation(60, 180);
btn_land.setSize(120, 40);
btn_land.setLocation(230, 180);
btn_register.setFont(btn);
btn_land.setFont(btn);
}
public static void main(String[] args)
throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
new denglu().setVisible(true);
}
}
我这儿的运行效果:
希望能帮助你哈
展开全部
Class class1 = denglu.class;//图片与本类在同一个包里面,获取路径
URL url = class1.getResource("1.png");
ImageIcon icon = new ImageIcon(url);//构造图片对象
JLabel jLabel = new JLabel(icon);//构造一个图片的jlabel
contentPanel.add(jLabel,new Integer(Integer.MIN_VALUE));//在登陆界面的控制panel上添加jLabel为最底层
URL url = class1.getResource("1.png");
ImageIcon icon = new ImageIcon(url);//构造图片对象
JLabel jLabel = new JLabel(icon);//构造一个图片的jlabel
contentPanel.add(jLabel,new Integer(Integer.MIN_VALUE));//在登陆界面的控制panel上添加jLabel为最底层
追问
我怎么不是很懂喃??求详情、、、、谢谢!!!!输入进切报错
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询