java 窗体设置背景图片问题?(附上登陆界面代码,我想加个背景图片,求大神帮忙改改)
importjava.awt.event.WindowAdapter;importjava.awt.event.ActionListener;importjava.awt...
import java.awt.event.WindowAdapter ;
import java.awt.event.ActionListener ;
import java.awt.event.WindowEvent ;
import java.awt.event.ActionEvent ;
import java.awt.Color ;
import java.awt.Font ;
import javax.swing.JFrame ;
import javax.swing.JButton ;
import javax.swing.JLabel ;
import javax.swing.JTextField ;
import javax.swing.JPasswordField ;
class LoginCheck{
private String name ;
private String password ;
public LoginCheck(String name,String password){
this.name = name ;
this.password = password ;
}
public boolean validate(){
if("mwb".equals(name)&&"mwb".equals(password)){
return true ;
}else{
return false ;
}
}
};
class ActionHandle{
private JFrame frame = new JFrame("Welcome To My FinanceManageSystem") ;
private JButton submit = new JButton("登陆");
private JButton reset = new JButton("重置");
private JLabel nameLab = new JLabel("用户名:") ;
private JLabel passLab = new JLabel("密 码:") ;
private JLabel infoLab = new JLabel("用户登陆系统") ;
private JTextField nameText = new JTextField(10) ;
private JPasswordField passText = new JPasswordField();
public ActionHandle(){
Font fnt = new Font("Serief",Font.ITALIC + Font.BOLD,12) ;
infoLab.setFont(fnt) ; // 设置标签的显示文字
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==submit){
String tname = nameText.getText() ;
String tpass = new String(passText.getPassword()) ;
LoginCheck log = new LoginCheck(tname,tpass) ;
if(log.validate()){
infoLab.setText("登陆成功,欢迎光临!") ;
}else{
infoLab.setText("登陆失败,错误的用户名或密码!") ;
}
}
}
}) ;
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==reset){
nameText.setText("") ;
passText.setText("") ;
infoLab.setText("用户登陆系统") ;
}
}
}) ;
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1) ;
}
}) ; // 加入事件
frame.setLayout(null) ;
nameLab.setBounds(650,300,60,20) ;
passLab.setBounds(650,325,60,20) ;
infoLab.setBounds(650,360,220,30) ;
nameText.setBounds(710,300,100,20) ;
passText.setBounds(710,325,100,20) ;
submit.setBounds(810,300,60,20) ;
reset.setBounds(810,325,60,20) ;
frame.add(nameLab) ;
frame.add(passLab) ;
frame.add(infoLab) ;
frame.add(nameText) ;
frame.add(passText) ;
frame.add(submit) ;
frame.add(reset) ;
frame.setSize(280,130) ;
frame.setBackground(Color.WHITE) ;
frame.setSize(1000,500);
frame.setLocation(200,150) ;
frame.setVisible(true) ;
}
}
public class Login{
public static void main(String args[]){
new ActionHandle() ;
}
}
在eclipse中运行 展开
import java.awt.event.ActionListener ;
import java.awt.event.WindowEvent ;
import java.awt.event.ActionEvent ;
import java.awt.Color ;
import java.awt.Font ;
import javax.swing.JFrame ;
import javax.swing.JButton ;
import javax.swing.JLabel ;
import javax.swing.JTextField ;
import javax.swing.JPasswordField ;
class LoginCheck{
private String name ;
private String password ;
public LoginCheck(String name,String password){
this.name = name ;
this.password = password ;
}
public boolean validate(){
if("mwb".equals(name)&&"mwb".equals(password)){
return true ;
}else{
return false ;
}
}
};
class ActionHandle{
private JFrame frame = new JFrame("Welcome To My FinanceManageSystem") ;
private JButton submit = new JButton("登陆");
private JButton reset = new JButton("重置");
private JLabel nameLab = new JLabel("用户名:") ;
private JLabel passLab = new JLabel("密 码:") ;
private JLabel infoLab = new JLabel("用户登陆系统") ;
private JTextField nameText = new JTextField(10) ;
private JPasswordField passText = new JPasswordField();
public ActionHandle(){
Font fnt = new Font("Serief",Font.ITALIC + Font.BOLD,12) ;
infoLab.setFont(fnt) ; // 设置标签的显示文字
submit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==submit){
String tname = nameText.getText() ;
String tpass = new String(passText.getPassword()) ;
LoginCheck log = new LoginCheck(tname,tpass) ;
if(log.validate()){
infoLab.setText("登陆成功,欢迎光临!") ;
}else{
infoLab.setText("登陆失败,错误的用户名或密码!") ;
}
}
}
}) ;
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==reset){
nameText.setText("") ;
passText.setText("") ;
infoLab.setText("用户登陆系统") ;
}
}
}) ;
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1) ;
}
}) ; // 加入事件
frame.setLayout(null) ;
nameLab.setBounds(650,300,60,20) ;
passLab.setBounds(650,325,60,20) ;
infoLab.setBounds(650,360,220,30) ;
nameText.setBounds(710,300,100,20) ;
passText.setBounds(710,325,100,20) ;
submit.setBounds(810,300,60,20) ;
reset.setBounds(810,325,60,20) ;
frame.add(nameLab) ;
frame.add(passLab) ;
frame.add(infoLab) ;
frame.add(nameText) ;
frame.add(passText) ;
frame.add(submit) ;
frame.add(reset) ;
frame.setSize(280,130) ;
frame.setBackground(Color.WHITE) ;
frame.setSize(1000,500);
frame.setLocation(200,150) ;
frame.setVisible(true) ;
}
}
public class Login{
public static void main(String args[]){
new ActionHandle() ;
}
}
在eclipse中运行 展开
展开全部
给你加完了,继承PANEL类,得写paint或paintComponent方法。
代码如下。
---------------------------------------------------------------------------------------------------
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
class LoginCheck {
private String name;
private String password;
public LoginCheck(String name, String password) {
this.name = name;
this.password = password;
}
public boolean validate() {
if ("mwb".equals(name) && "mwb".equals(password)) {
return true;
} else {
return false;
}
}
};
class ActionHandle {
private JFrame frame = new JFrame("Welcome To My FinanceManageSystem");
private JButton submit = new JButton("登陆");
private JButton reset = new JButton("重置");
private JLabel nameLab = new JLabel("用户名:");
private JLabel passLab = new JLabel("密 码:");
private JLabel infoLab = new JLabel("用户登陆系统");
private JTextField nameText = new JTextField(10);
private JPasswordField passText = new JPasswordField();
public ActionHandle() {
Font fnt = new Font("Serief", Font.ITALIC + Font.BOLD, 12);
infoLab.setFont(fnt); // 设置标签的显示文字
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submit) {
String tname = nameText.getText();
String tpass = new String(passText.getPassword());
LoginCheck log = new LoginCheck(tname, tpass);
if (log.validate()) {
infoLab.setText("登陆成功,欢迎光临!");
} else {
infoLab.setText("登陆失败,错误的用户名或密码!");
}
}
}
});
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == reset) {
nameText.setText("");
passText.setText("");
infoLab.setText("用户登陆系统");
}
}
});
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(1);
}
}); // 加入事件
frame.setLayout(null);
nameLab.setBounds(650, 300, 60, 20);
passLab.setBounds(650, 325, 60, 20);
infoLab.setBounds(650, 360, 220, 30);
nameText.setBounds(710, 300, 100, 20);
passText.setBounds(710, 325, 100, 20);
submit.setBounds(810, 300, 60, 20);
reset.setBounds(810, 325, 60, 20);
frame.add(nameLab);
frame.add(passLab);
frame.add(infoLab);
frame.add(nameText);
frame.add(passText);
frame.add(submit);
frame.add(reset);
frame.setSize(280, 130);
frame.setBackground(Color.WHITE);
frame.setSize(1000, 500);
frame.setLocation(200, 150);
ImagePanel panel = new ImagePanel();
panel.setBounds(0, 0, 600, 400);
frame.add(panel);
frame.setVisible(true);
}
}
public class Login {
public static void main(String args[]) {
new ActionHandle();
}
}
class ImagePanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon = new ImageIcon("D:\\1.jpg");
g.drawImage(icon.getImage(),0,0,null);
}
}
代码如下。
---------------------------------------------------------------------------------------------------
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
class LoginCheck {
private String name;
private String password;
public LoginCheck(String name, String password) {
this.name = name;
this.password = password;
}
public boolean validate() {
if ("mwb".equals(name) && "mwb".equals(password)) {
return true;
} else {
return false;
}
}
};
class ActionHandle {
private JFrame frame = new JFrame("Welcome To My FinanceManageSystem");
private JButton submit = new JButton("登陆");
private JButton reset = new JButton("重置");
private JLabel nameLab = new JLabel("用户名:");
private JLabel passLab = new JLabel("密 码:");
private JLabel infoLab = new JLabel("用户登陆系统");
private JTextField nameText = new JTextField(10);
private JPasswordField passText = new JPasswordField();
public ActionHandle() {
Font fnt = new Font("Serief", Font.ITALIC + Font.BOLD, 12);
infoLab.setFont(fnt); // 设置标签的显示文字
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submit) {
String tname = nameText.getText();
String tpass = new String(passText.getPassword());
LoginCheck log = new LoginCheck(tname, tpass);
if (log.validate()) {
infoLab.setText("登陆成功,欢迎光临!");
} else {
infoLab.setText("登陆失败,错误的用户名或密码!");
}
}
}
});
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == reset) {
nameText.setText("");
passText.setText("");
infoLab.setText("用户登陆系统");
}
}
});
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(1);
}
}); // 加入事件
frame.setLayout(null);
nameLab.setBounds(650, 300, 60, 20);
passLab.setBounds(650, 325, 60, 20);
infoLab.setBounds(650, 360, 220, 30);
nameText.setBounds(710, 300, 100, 20);
passText.setBounds(710, 325, 100, 20);
submit.setBounds(810, 300, 60, 20);
reset.setBounds(810, 325, 60, 20);
frame.add(nameLab);
frame.add(passLab);
frame.add(infoLab);
frame.add(nameText);
frame.add(passText);
frame.add(submit);
frame.add(reset);
frame.setSize(280, 130);
frame.setBackground(Color.WHITE);
frame.setSize(1000, 500);
frame.setLocation(200, 150);
ImagePanel panel = new ImagePanel();
panel.setBounds(0, 0, 600, 400);
frame.add(panel);
frame.setVisible(true);
}
}
public class Login {
public static void main(String args[]) {
new ActionHandle();
}
}
class ImagePanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon = new ImageIcon("D:\\1.jpg");
g.drawImage(icon.getImage(),0,0,null);
}
}
追问
对了,我想让这个背景图片占满整个窗口怎么弄啊
追答
PANEL的大小占满窗口就可以了.
展开全部
要使用JPanel类,继承这个类,覆写其中的PaintCompentent方法即可
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看不懂。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
a
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询