swing的设置背景问题
怎么把login和password以及后面的文本框移下去

package test;
import java.awt.Container;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
class BackgroundFrame extends JFrame{
public BackgroundFrame(){
super.setTitle("test background");
container = this.getContentPane();
textField = new JTextField(30);
passwordField = new JPasswordField();
imgLabel = new JLabel(image);
this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
label1 = new JLabel("login");
label2 = new JLabel("password");
imgLabel.setBounds(0, 0, image.getIconWidth(), image.getIconWidth());
label1.setBounds(10, 10, 120, 20);
label2.setBounds(10, 35, 120, 20);
textField.setBounds(135, 10, 120, 20);
passwordField.setBounds(135, 35, 120, 20);
this.setLayout(null);
container.add(label1);
container.add(label2);
container.add(passwordField);
container.add(textField);
container.add(imgLabel);
((JPanel)container).setOpaque(false);//将container设置成透明
super.setSize(image.getIconWidth(), image.getIconHeight());
super.setLocation(200, 100);
super.setVisible(true);
}
private JTextField textField;
private JLabel imgLabel;
private JLabel label1;
private JLabel label2;
private JPasswordField passwordField;
private Container container;
private String picPath = "c:" +File.separator + "background1.jpg";
private Icon image = new ImageIcon(picPath);
}
public class testBackground {
public static void main(String[] args) {
new BackgroundFrame();
}
}

package test;
import java.awt.Container;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
class BackgroundFrame extends JFrame{
public BackgroundFrame(){
super.setTitle("test background");
container = this.getContentPane();
textField = new JTextField(30);
passwordField = new JPasswordField();
imgLabel = new JLabel(image);
this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
label1 = new JLabel("login");
label2 = new JLabel("password");
imgLabel.setBounds(0, 0, image.getIconWidth(), image.getIconWidth());
label1.setBounds(10, 10, 120, 20);
label2.setBounds(10, 35, 120, 20);
textField.setBounds(135, 10, 120, 20);
passwordField.setBounds(135, 35, 120, 20);
this.setLayout(null);
container.add(label1);
container.add(label2);
container.add(passwordField);
container.add(textField);
container.add(imgLabel);
((JPanel)container).setOpaque(false);//将container设置成透明
super.setSize(image.getIconWidth(), image.getIconHeight());
super.setLocation(200, 100);
super.setVisible(true);
}
private JTextField textField;
private JLabel imgLabel;
private JLabel label1;
private JLabel label2;
private JPasswordField passwordField;
private Container container;
private String picPath = "c:" +File.separator + "background1.jpg";
private Icon image = new ImageIcon(picPath);
}
public class testBackground {
public static void main(String[] args) {
new BackgroundFrame();
}
}
...全文
相关推荐
Swing控件(JPanel,Container等)设置背景图片
Swing提供了丰富的功能,包括允许开发者为这些组件设置自定义的背景图片,以提升应用的视觉效果。这篇博文主要讨论了如何在JPanel和Container上设置背景图片,下面将详细解释这个过程。
java设置背景图片源代码
Swing中的JFrame类是用于创建窗口的基础,而我们可以通过设置JFrame的contentPane的背景图片来实现背景图片的设置。1.
java 如何在窗体背景图片上加控件
在Java代码的开头,加入如下导入语句:```javaimport javax.swing.*;import java.awt.*;```1. **设置窗体背景图片**: - 首先,加载背景图片。
在jframe中将图像设置为背景图像
在Java Swing中,设置图像作为JFrame的背景是一项常见的任务,可以增强用户界面的美观性。JFrame是Java图形用户界面(GUI)的基础组件,它提供了窗口的基本结构。
JFrame设置背景色注意的事项
在处理`JFrame`的背景色时,可能会遇到一些问题,尤其是在需要设置透明背景的情况下。本文将详细解释如何正确地设置`JFrame`的背景色,并确保其效果符合预期。


Java SE
