java swing 我是想把图片作为登录界面的背景,并且需要背景图片能够随窗口大小缩放。。。最好是用标签法
请指教啊。。。最好给段代码。。。importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publiccla...
请指教啊。。。最好给段代码。。。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BgFrame extends JFrame {
public BgFrame() {
super("窗口背景");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setup();
setSize(300,300);
setLocationRelativeTo(null);
setVisible(true);
}
void setup()
{
JPanel content = new JPanel();
content.setLayout(new GridLayout(3,1));
content.setOpaque(false);//设置透明
JLabel label1 = new JLabel("用户名:");
label1.setForeground(Color.blue);
JTextField field = new JTextField(15);
JPanel a=new JPanel();
a.setLayout(new FlowLayout());
a.add(label1);
a.add(field);
a.setOpaque(false);
content.add(a);
JLabel label2 = new JLabel("密 码:");
label2.setForeground(Color.blue);
JPasswordField fieldPass = new JPasswordField(15);
JPanel b=new JPanel();
b.setLayout(new FlowLayout());
b.add(label2);
b.add(fieldPass);
b.setOpaque(false);
content.add(b);
JButton loginBtn = new JButton("Login");
JButton cancelBtn = new JButton("Cancel");
JPanel c=new JPanel();
c.setLayout(new FlowLayout());
c.add(loginBtn);
c.add(cancelBtn);
c.setOpaque(false);
content.add(c);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(content);
((JPanel)getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("100ZB1A-3.jpg");
JLabel backlabel = new JLabel(img);
getLayeredPane().add(backlabel, new Integer(Integer.MIN_VALUE));
backlabel.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
}
public static void main(String[] args) {
new BgFrame();
}
}
我的代码是这样的。。。但可是却背景图片不能随窗口大小缩放。。。求解救啊啊啊。。谢谢啦。 展开
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BgFrame extends JFrame {
public BgFrame() {
super("窗口背景");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setup();
setSize(300,300);
setLocationRelativeTo(null);
setVisible(true);
}
void setup()
{
JPanel content = new JPanel();
content.setLayout(new GridLayout(3,1));
content.setOpaque(false);//设置透明
JLabel label1 = new JLabel("用户名:");
label1.setForeground(Color.blue);
JTextField field = new JTextField(15);
JPanel a=new JPanel();
a.setLayout(new FlowLayout());
a.add(label1);
a.add(field);
a.setOpaque(false);
content.add(a);
JLabel label2 = new JLabel("密 码:");
label2.setForeground(Color.blue);
JPasswordField fieldPass = new JPasswordField(15);
JPanel b=new JPanel();
b.setLayout(new FlowLayout());
b.add(label2);
b.add(fieldPass);
b.setOpaque(false);
content.add(b);
JButton loginBtn = new JButton("Login");
JButton cancelBtn = new JButton("Cancel");
JPanel c=new JPanel();
c.setLayout(new FlowLayout());
c.add(loginBtn);
c.add(cancelBtn);
c.setOpaque(false);
content.add(c);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(content);
((JPanel)getContentPane()).setOpaque(false);
ImageIcon img = new ImageIcon("100ZB1A-3.jpg");
JLabel backlabel = new JLabel(img);
getLayeredPane().add(backlabel, new Integer(Integer.MIN_VALUE));
backlabel.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
}
public static void main(String[] args) {
new BgFrame();
}
}
我的代码是这样的。。。但可是却背景图片不能随窗口大小缩放。。。求解救啊啊啊。。谢谢啦。 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询