java 窗体设置背景图片问题?(附上登陆界面代码,我想加个背景图片,求大神帮忙改改)
关注:223 答案:4 手机版
解决时间 2021-01-26 22:09
提问者非莪莫属
2021-01-26 02:59
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中运行
最佳答案
二级知识专家柒夏锦年
2021-01-26 03:39
给你加完了,继承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);
}
}
全部回答
1楼相忘于江湖
2021-01-26 06:44
要使用JPanel类,继承这个类,覆写其中的PaintCompentent方法即可
2楼指间的落寞
2021-01-26 05:12
看不懂。。。。
3楼何必执着
2021-01-26 04:55
a
我要举报
如以上信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
推荐资讯
大家都在看