java panel设置背景图片
在原代码基础上随便添加一张图片为背景importjava.awt.Button;importjava.awt.Dimension;importjava.awt.Graph...
在原代码基础上随便添加一张图片为背景
import java.awt.Button;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ceshi2 implements ActionListener {
JFrame jf = new JFrame("Example");
JPanel panel = new JPanel();
JRadioButton rb1 = new JRadioButton("用户");
JRadioButton rb2 = new JRadioButton("管理员");
Button btok= new Button("登陆");
ButtonGroup group = new ButtonGroup ();
JTextField text = new JTextField(10);
JTextField text2= new JTextField(10);
JTextField text3= new JTextField(10);
Image img;
ceshi2(){
rb1.setBounds(10, 10, 50, 25);
rb2.setBounds(10, 30, 50, 25);
text.setBounds(10, 70, 100, 30);
text2.setBounds(10, 110, 100, 30);
text3.setBounds(10, 150, 100, 30);
btok.setBounds(10, 190, 100, 30);
panel.setLocation(0, 0);
panel.setSize(200, 200);
jf.add(panel);
group.add(rb1);
group.add(rb2);
panel.add(rb1);
panel.add(rb2);
panel.add(text);
panel.add(text2);
panel.add(text3);
panel.add(btok);
rb1.addActionListener(this);
rb2.addActionListener(this);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(500,500);
jf.setLayout(null);
panel.setLayout(null);
jf.setVisible(true);
jf.setResizable(false);
text.setVisible(false);
btok.addActionListener(new ActionListener() {
//@Override
public void actionPerformed(ActionEvent arg0) {
String name=text2.getText();
String password=text3.getText();
String compefence=text.getText();
if (new Login().verify(name,password,compefence)) {
yonghu.getWindows();
new yonghu();
}
else { JOptionPane jop=new JOptionPane();
jop.showMessageDialog(null,"用户名或密码错误","信息提示!",-1);}
}
});
}
public static void main(String[] args) {
ceshi2 t = new ceshi2();
}
@Override
public void actionPerformed(ActionEvent e) {
if(group.getSelection()==rb1.getModel()){
text.setText("0");
}
if(group.getSelection()==rb2.getModel()){
text.setText("1");
}
}
} 展开
import java.awt.Button;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ceshi2 implements ActionListener {
JFrame jf = new JFrame("Example");
JPanel panel = new JPanel();
JRadioButton rb1 = new JRadioButton("用户");
JRadioButton rb2 = new JRadioButton("管理员");
Button btok= new Button("登陆");
ButtonGroup group = new ButtonGroup ();
JTextField text = new JTextField(10);
JTextField text2= new JTextField(10);
JTextField text3= new JTextField(10);
Image img;
ceshi2(){
rb1.setBounds(10, 10, 50, 25);
rb2.setBounds(10, 30, 50, 25);
text.setBounds(10, 70, 100, 30);
text2.setBounds(10, 110, 100, 30);
text3.setBounds(10, 150, 100, 30);
btok.setBounds(10, 190, 100, 30);
panel.setLocation(0, 0);
panel.setSize(200, 200);
jf.add(panel);
group.add(rb1);
group.add(rb2);
panel.add(rb1);
panel.add(rb2);
panel.add(text);
panel.add(text2);
panel.add(text3);
panel.add(btok);
rb1.addActionListener(this);
rb2.addActionListener(this);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(500,500);
jf.setLayout(null);
panel.setLayout(null);
jf.setVisible(true);
jf.setResizable(false);
text.setVisible(false);
btok.addActionListener(new ActionListener() {
//@Override
public void actionPerformed(ActionEvent arg0) {
String name=text2.getText();
String password=text3.getText();
String compefence=text.getText();
if (new Login().verify(name,password,compefence)) {
yonghu.getWindows();
new yonghu();
}
else { JOptionPane jop=new JOptionPane();
jop.showMessageDialog(null,"用户名或密码错误","信息提示!",-1);}
}
});
}
public static void main(String[] args) {
ceshi2 t = new ceshi2();
}
@Override
public void actionPerformed(ActionEvent e) {
if(group.getSelection()==rb1.getModel()){
text.setText("0");
}
if(group.getSelection()==rb2.getModel()){
text.setText("1");
}
}
} 展开
2个回答
展开全部
要在panel上面加图片,需要重写paint方法,那样就麻烦了,JLabel有现成的设置方法;
JLabel l=new JLabel();
Icon icon=new ImageIcon("D:\\a.jpg"); //在此直接创建对象
l.setIcon(icon);
然后把你原来放在panel上的东西放在JLabel上面,再把JLabel放在panel上面就可以了
JLabel l=new JLabel();
Icon icon=new ImageIcon("D:\\a.jpg"); //在此直接创建对象
l.setIcon(icon);
然后把你原来放在panel上的东西放在JLabel上面,再把JLabel放在panel上面就可以了
追问
可以帮忙在源代码修改下吗?我自己改了下 还是不行 把panel全部覆盖掉了什么也显示不出来
追答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询