JAVA窗体添加背景图片
这是我自己编写的一段登陆界面的代码,当时没加背景图片,在想加加不上了,网上的方法也试过,不行!也可能跟我的图片有关系!请高手帮我加加!我的q是295826804,我给你们...
这是我自己编写的一段登陆界面的代码,当时没加背景图片,在想加加不上了,网上的方法也试过,不行!也可能跟我的图片有关系!请高手帮我加加!我的q是295826804,我给你们图片,你们顺便看看是不是我图片的问题,谢了!
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class DengLu extends JFrame{
public JLabel name=new JLabel("用户名");
public JLabel pass=new JLabel("密 码");
public JTextField userName=new JTextField();
public JPasswordField passWord=new JPasswordField();
public Button bok=new Button("登陆");
public Button bexit=new Button("取消");
public DengLu(){
setTitle("欢迎使用学生成绩管理系统");
setLayout(null);
setSize(500,400);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension scr=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm=this.getSize();
setLocation((scr.width-frm.width)/2,(scr.height-frm.height)/2-18);
name.setBounds(70, 260, 120, 20);
userName.setBounds(120,260,120,27);
pass.setBounds(70, 300, 120, 20);
passWord.setBounds(120,300,120,27);
passWord.setEchoChar('*');
bok.setBounds(340,260,100,28);
bexit.setBounds(340,300,100,28);
add(name);
add(userName);
add(pass);
add(passWord);
add(bok);
add(bexit);
setVisible(true);
bexit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//dispose();
System.exit(0);
}
});
bok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(userName.getText().equals("")){
JOptionPane.showMessageDialog(null,"用户名不能为空!");
}
else if(passWord.getText().equals("")){
JOptionPane.showMessageDialog(null,"密码不能为空!");
}
else{
if(userName.getText().equals("admin")&& passWord.getText().equals("admin")){
dispose();
new MainFrame();
}else{
JOptionPane.showMessageDialog(null, "密码错误");
userName.setText(null);
passWord.setText(null);
}
}
}
});
}
public static void main(String args[]){
new DengLu();
}
} 展开
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class DengLu extends JFrame{
public JLabel name=new JLabel("用户名");
public JLabel pass=new JLabel("密 码");
public JTextField userName=new JTextField();
public JPasswordField passWord=new JPasswordField();
public Button bok=new Button("登陆");
public Button bexit=new Button("取消");
public DengLu(){
setTitle("欢迎使用学生成绩管理系统");
setLayout(null);
setSize(500,400);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension scr=Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm=this.getSize();
setLocation((scr.width-frm.width)/2,(scr.height-frm.height)/2-18);
name.setBounds(70, 260, 120, 20);
userName.setBounds(120,260,120,27);
pass.setBounds(70, 300, 120, 20);
passWord.setBounds(120,300,120,27);
passWord.setEchoChar('*');
bok.setBounds(340,260,100,28);
bexit.setBounds(340,300,100,28);
add(name);
add(userName);
add(pass);
add(passWord);
add(bok);
add(bexit);
setVisible(true);
bexit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//dispose();
System.exit(0);
}
});
bok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(userName.getText().equals("")){
JOptionPane.showMessageDialog(null,"用户名不能为空!");
}
else if(passWord.getText().equals("")){
JOptionPane.showMessageDialog(null,"密码不能为空!");
}
else{
if(userName.getText().equals("admin")&& passWord.getText().equals("admin")){
dispose();
new MainFrame();
}else{
JOptionPane.showMessageDialog(null, "密码错误");
userName.setText(null);
passWord.setText(null);
}
}
}
});
}
public static void main(String args[]){
new DengLu();
}
} 展开
4个回答
展开全部
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class DengLu extends JFrame {
public JLabel name = new JLabel("用户名");
public JLabel pass = new JLabel("密 码");
public JTextField userName = new JTextField();
public JPasswordField passWord = new JPasswordField();
public Button bok = new Button("登陆");
public Button bexit = new Button("取消");
public DengLu() {
this.setContentPane(new MyPanel());
setTitle("欢迎使用学生成绩管理系统");
setLayout(null);
setSize(500, 400);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
setLocation( (scr.width - frm.width) / 2,
(scr.height - frm.height) / 2 - 18);
name.setBounds(70, 260, 120, 20);
userName.setBounds(120, 260, 120, 27);
pass.setBounds(70, 300, 120, 20);
passWord.setBounds(120, 300, 120, 27);
passWord.setEchoChar('*');
bok.setBounds(340, 260, 100, 28);
bexit.setBounds(340, 300, 100, 28);
add(name);
add(userName);
add(pass);
add(passWord);
add(bok);
add(bexit);
setVisible(true);
bexit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//dispose();
System.exit(0);
}
});
bok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (userName.getText().equals("")) {
JOptionPane.showMessageDialog(null, "用户名不能为空!");
} else if (passWord.getText().equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空!");
} else {
if (userName.getText().equals("admin") &&
passWord.getText().equals("admin")) {
dispose();
// new MainFrame();
} else {
JOptionPane.showMessageDialog(null, "密码错误");
userName.setText(null);
passWord.setText(null);
}
}
}
});
}
public static void main(String args[]) {
new DengLu();
}
private class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g);
Image img = Toolkit.getDefaultToolkit().getImage("zsjm.jpg");
g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
}
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
public class DengLu extends JFrame {
public JLabel name = new JLabel("用户名");
public JLabel pass = new JLabel("密 码");
public JTextField userName = new JTextField();
public JPasswordField passWord = new JPasswordField();
public Button bok = new Button("登陆");
public Button bexit = new Button("取消");
public DengLu() {
this.setContentPane(new MyPanel());
setTitle("欢迎使用学生成绩管理系统");
setLayout(null);
setSize(500, 400);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
setLocation( (scr.width - frm.width) / 2,
(scr.height - frm.height) / 2 - 18);
name.setBounds(70, 260, 120, 20);
userName.setBounds(120, 260, 120, 27);
pass.setBounds(70, 300, 120, 20);
passWord.setBounds(120, 300, 120, 27);
passWord.setEchoChar('*');
bok.setBounds(340, 260, 100, 28);
bexit.setBounds(340, 300, 100, 28);
add(name);
add(userName);
add(pass);
add(passWord);
add(bok);
add(bexit);
setVisible(true);
bexit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//dispose();
System.exit(0);
}
});
bok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (userName.getText().equals("")) {
JOptionPane.showMessageDialog(null, "用户名不能为空!");
} else if (passWord.getText().equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空!");
} else {
if (userName.getText().equals("admin") &&
passWord.getText().equals("admin")) {
dispose();
// new MainFrame();
} else {
JOptionPane.showMessageDialog(null, "密码错误");
userName.setText(null);
passWord.setText(null);
}
}
}
});
}
public static void main(String args[]) {
new DengLu();
}
private class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g);
Image img = Toolkit.getDefaultToolkit().getImage("zsjm.jpg");
g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package image;
import bufferedImage.ImageOps;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
public class DengLu extends JFrame {
public JLabel name = new JLabel("用户名");
public JLabel pass = new JLabel("密 码");
public JTextField userName = new JTextField();
public JPasswordField passWord = new JPasswordField();
public Button bok = new Button("登陆");
public Button bexit = new Button("取消");
public void paint(Graphics g) {
super.paint(g);
BufferedImage image = null;
try {
image = ImageIO.read(new File("cover.gif"));
} catch (IOException ex) {
Logger.getLogger(ImageOps.class.getName()).log(Level.SEVERE, null, ex);
}
g.drawImage(image, 0, 0, null);
}
public DengLu() {
setTitle("欢迎使用学生成绩管理系统");
setLayout(null);
setSize(500, 400);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
setLocation((scr.width - frm.width) / 2, (scr.height - frm.height) / 2 - 18);
name.setBounds(70, 260, 120, 20);
userName.setBounds(120, 260, 120, 27);
pass.setBounds(70, 300, 120, 20);
passWord.setBounds(120, 300, 120, 27);
passWord.setEchoChar('*');
bok.setBounds(340, 260, 100, 28);
bexit.setBounds(340, 300, 100, 28);
add(name);
add(userName);
add(pass);
add(passWord);
add(bok);
add(bexit);
setVisible(true);
bexit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//dispose();
System.exit(0);
}
});
bok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (userName.getText().equals("")) {
JOptionPane.showMessageDialog(null, "用户名不能为空!");
} else if (passWord.getText().equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空!");
} else {
if (userName.getText().equals("admin") && passWord.getText().equals("admin")) {
dispose();
// new MainFrame();
} else {
JOptionPane.showMessageDialog(null, "密码错误");
userName.setText(null);
passWord.setText(null);
}
}
}
});
}
public static void main(String args[]) {
new DengLu();
}
}
就增加了一个paint()方法画图。画图的详细说你去看api吧。Graphics 和Graphics2D这两个类有具体的说明。那个读取图片的也请看api的说明了。
关于工具推荐使用netBeans,关于界面的设计在该ide里可以像vb一样拖放位置。而且在代码的格式和语法的错误上他也可以帮你纠正。
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package image;
import bufferedImage.ImageOps;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
public class DengLu extends JFrame {
public JLabel name = new JLabel("用户名");
public JLabel pass = new JLabel("密 码");
public JTextField userName = new JTextField();
public JPasswordField passWord = new JPasswordField();
public Button bok = new Button("登陆");
public Button bexit = new Button("取消");
public void paint(Graphics g) {
super.paint(g);
BufferedImage image = null;
try {
image = ImageIO.read(new File("cover.gif"));
} catch (IOException ex) {
Logger.getLogger(ImageOps.class.getName()).log(Level.SEVERE, null, ex);
}
g.drawImage(image, 0, 0, null);
}
public DengLu() {
setTitle("欢迎使用学生成绩管理系统");
setLayout(null);
setSize(500, 400);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
setLocation((scr.width - frm.width) / 2, (scr.height - frm.height) / 2 - 18);
name.setBounds(70, 260, 120, 20);
userName.setBounds(120, 260, 120, 27);
pass.setBounds(70, 300, 120, 20);
passWord.setBounds(120, 300, 120, 27);
passWord.setEchoChar('*');
bok.setBounds(340, 260, 100, 28);
bexit.setBounds(340, 300, 100, 28);
add(name);
add(userName);
add(pass);
add(passWord);
add(bok);
add(bexit);
setVisible(true);
bexit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//dispose();
System.exit(0);
}
});
bok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (userName.getText().equals("")) {
JOptionPane.showMessageDialog(null, "用户名不能为空!");
} else if (passWord.getText().equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空!");
} else {
if (userName.getText().equals("admin") && passWord.getText().equals("admin")) {
dispose();
// new MainFrame();
} else {
JOptionPane.showMessageDialog(null, "密码错误");
userName.setText(null);
passWord.setText(null);
}
}
}
});
}
public static void main(String args[]) {
new DengLu();
}
}
就增加了一个paint()方法画图。画图的详细说你去看api吧。Graphics 和Graphics2D这两个类有具体的说明。那个读取图片的也请看api的说明了。
关于工具推荐使用netBeans,关于界面的设计在该ide里可以像vb一样拖放位置。而且在代码的格式和语法的错误上他也可以帮你纠正。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.net.URL;
public class Login extends JFrame {
public JLabel name = new JLabel("用户名");
public JLabel pass = new JLabel("密 码");
public JTextField userName = new JTextField();
public JPasswordField passWord = new JPasswordField();
public Button bok = new Button("登陆");
public Button bexit = new Button("取消");
public void setBack() {
((JPanel) this.getContentPane()).setOpaque(false);
// Winter.jpg这个图片的位置要跟当前这个类是同一个包下
URL url = Test.class.getResource("Winter.jpg");
ImageIcon img = new ImageIcon(url);
JLabel background = new JLabel(img);
this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
}
public Login() {
setTitle("欢迎使用学生成绩管理系统");
setLayout(null);
setSize(500, 400);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
setLocation((scr.width - frm.width) / 2,
(scr.height - frm.height) / 2 - 18);
setBack();
name.setBounds(70, 260, 120, 20);
userName.setBounds(120, 260, 120, 27);
pass.setBounds(70, 300, 120, 20);
passWord.setBounds(120, 300, 120, 27);
passWord.setEchoChar('*');
bok.setBounds(340, 260, 100, 28);
bexit.setBounds(340, 300, 100, 28);
add(name);
add(userName);
add(pass);
add(passWord);
add(bok);
add(bexit);
setVisible(true);
bexit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// dispose();
System.exit(0);
}
});
bok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (userName.getText().equals("")) {
JOptionPane.showMessageDialog(null, "用户名不能为空!");
} else if (passWord.getText().equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空!");
} else {
if (userName.getText().equals("admin")
&& passWord.getText().equals("admin")) {
dispose();
// new MainFrame();
} else {
JOptionPane.showMessageDialog(null, "密码错误");
userName.setText(null);
passWord.setText(null);
}
}
}
});
}
public static void main(String args[]) {
new Login();
}
}
import java.awt.event.*;
import java.awt.*;
import java.net.URL;
public class Login extends JFrame {
public JLabel name = new JLabel("用户名");
public JLabel pass = new JLabel("密 码");
public JTextField userName = new JTextField();
public JPasswordField passWord = new JPasswordField();
public Button bok = new Button("登陆");
public Button bexit = new Button("取消");
public void setBack() {
((JPanel) this.getContentPane()).setOpaque(false);
// Winter.jpg这个图片的位置要跟当前这个类是同一个包下
URL url = Test.class.getResource("Winter.jpg");
ImageIcon img = new ImageIcon(url);
JLabel background = new JLabel(img);
this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
}
public Login() {
setTitle("欢迎使用学生成绩管理系统");
setLayout(null);
setSize(500, 400);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
setLocation((scr.width - frm.width) / 2,
(scr.height - frm.height) / 2 - 18);
setBack();
name.setBounds(70, 260, 120, 20);
userName.setBounds(120, 260, 120, 27);
pass.setBounds(70, 300, 120, 20);
passWord.setBounds(120, 300, 120, 27);
passWord.setEchoChar('*');
bok.setBounds(340, 260, 100, 28);
bexit.setBounds(340, 300, 100, 28);
add(name);
add(userName);
add(pass);
add(passWord);
add(bok);
add(bexit);
setVisible(true);
bexit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// dispose();
System.exit(0);
}
});
bok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (userName.getText().equals("")) {
JOptionPane.showMessageDialog(null, "用户名不能为空!");
} else if (passWord.getText().equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空!");
} else {
if (userName.getText().equals("admin")
&& passWord.getText().equals("admin")) {
dispose();
// new MainFrame();
} else {
JOptionPane.showMessageDialog(null, "密码错误");
userName.setText(null);
passWord.setText(null);
}
}
}
});
}
public static void main(String args[]) {
new Login();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询