java背景图片添加问题
packagezzuli.edu.view;importjava.awt.Container;importjava.awt.Dimension;importjava.aw...
package zzuli.edu.view;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import zzuli.edu.dao.UserDAO;
import zzuli.edu.data.TimeData;
import zzuli.edu.data.UserData;
public class Login extends JFrame
{
private JTextField userText;
private JPasswordField passText;
private JButton button;
private UserData userData;
private JLabel labelx;
private DataLinkDialog linkDialog;
public Login()
{
super("汽配管理系统登录界面");
Container container=this.getContentPane();
container.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
JPanel topPanel=new JPanel();
topPanel.setPreferredSize(new Dimension(300,10));
container.add(topPanel);
JLabel userLabel=new JLabel("账号");
container.add(userLabel);
userText=new JTextField();
userText.setPreferredSize(new Dimension(150,30));
container.add(userText);
JLabel label=new JLabel("");
label.setPreferredSize(new Dimension(60,30));
container.add(label);
JLabel passLabel=new JLabel("密码");
container.add(passLabel);
passText=new JPasswordField();
passText.setPreferredSize(new Dimension(150,30));
container.add(passText);
button=new JButton("登录");
button.setPreferredSize(new Dimension(60,30));
button.addActionListener(new buttonListener());
//确定按钮为回车键 "ENTER "
button.registerKeyboardAction(new buttonListener(),
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
求大侠帮我在里面加个背景图片的语句 我的qq是762320137 展开
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
import zzuli.edu.dao.UserDAO;
import zzuli.edu.data.TimeData;
import zzuli.edu.data.UserData;
public class Login extends JFrame
{
private JTextField userText;
private JPasswordField passText;
private JButton button;
private UserData userData;
private JLabel labelx;
private DataLinkDialog linkDialog;
public Login()
{
super("汽配管理系统登录界面");
Container container=this.getContentPane();
container.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
JPanel topPanel=new JPanel();
topPanel.setPreferredSize(new Dimension(300,10));
container.add(topPanel);
JLabel userLabel=new JLabel("账号");
container.add(userLabel);
userText=new JTextField();
userText.setPreferredSize(new Dimension(150,30));
container.add(userText);
JLabel label=new JLabel("");
label.setPreferredSize(new Dimension(60,30));
container.add(label);
JLabel passLabel=new JLabel("密码");
container.add(passLabel);
passText=new JPasswordField();
passText.setPreferredSize(new Dimension(150,30));
container.add(passText);
button=new JButton("登录");
button.setPreferredSize(new Dimension(60,30));
button.addActionListener(new buttonListener());
//确定按钮为回车键 "ENTER "
button.registerKeyboardAction(new buttonListener(),
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
求大侠帮我在里面加个背景图片的语句 我的qq是762320137 展开
4个回答
展开全部
public void setBak(){
((JPanel)this.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("images/login.png"); //添加图片
JLabel background = new JLabel(img);this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
}
再在你上面那个类里面用一下这个类就OK了的
或者你也可以说 用一个大的图片控件 放到背景上也成
((JPanel)this.getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("images/login.png"); //添加图片
JLabel background = new JLabel(img);this.getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
}
再在你上面那个类里面用一下这个类就OK了的
或者你也可以说 用一个大的图片控件 放到背景上也成
展开全部
给你一个加背景的例子
你的那些控件也要加在topPanel上。
------------------------------------------------------------------------------------------------------------
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageApp extends JFrame {
public ImageApp() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
getContentPane().setLayout(null);
JPanel panel = new ImagePanel();
panel.setBounds(0, 0, 400, 300);
getContentPane().add(panel);
setVisible(true);
}
public static void main(String[] args) {
new ImageApp();
}
class ImagePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
ImageIcon icon = new ImageIcon("D:\\1.jpg");
g.drawImage(icon.getImage(), 0, 0, 400, 300, this);
}
}
}
你的那些控件也要加在topPanel上。
------------------------------------------------------------------------------------------------------------
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageApp extends JFrame {
public ImageApp() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
getContentPane().setLayout(null);
JPanel panel = new ImagePanel();
panel.setBounds(0, 0, 400, 300);
getContentPane().add(panel);
setVisible(true);
}
public static void main(String[] args) {
new ImageApp();
}
class ImagePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
ImageIcon icon = new ImageIcon("D:\\1.jpg");
g.drawImage(icon.getImage(), 0, 0, 400, 300, this);
}
}
}
展开全部
在JFrame上用JLabel添加图片,把JLabel覆盖整个JFrame上就可以了!
ImageIcon im = new ImageIcon(imagepath);
JLabel jLabel = new JLabel();
jLabel.setIcon(im);
jLabel.setBounds(new Rectangle(x, y, width, height));
修改修改吧
ImageIcon im = new ImageIcon(imagepath);
JLabel jLabel = new JLabel();
jLabel.setIcon(im);
jLabel.setBounds(new Rectangle(x, y, width, height));
修改修改吧
展开全部
public JPanel getImg(){
if(null==img){
img=new JPanel();
JLabel l=new JLabel();
Icon icon=new ImageIcon("D:\\3.png"); //在此直接创建对象
l.setIcon(icon);
l.setBounds(0, 0, icon.getIconWidth(),icon.getIconHeight());
img.add(l);
}
return img;
}
if(null==img){
img=new JPanel();
JLabel l=new JLabel();
Icon icon=new ImageIcon("D:\\3.png"); //在此直接创建对象
l.setIcon(icon);
l.setBounds(0, 0, icon.getIconWidth(),icon.getIconHeight());
img.add(l);
}
return img;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询