我是 Java Swing开发的新手,我有以下问题。
我必须创建一个具有背景图像的 JFrame 窗口。
所以我执行了以下操作:
1)我创建了一个名为JPanelWithBackground的类,它扩展了JPanel:
package com.test.login;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class JPanelWithBackground extends JPanel {
private Image backgroundImage;
// Some code to initialize the background image.
// Here, we use the constructor to load the image. This
// can vary depending on the use case of the panel.
public JPanelWithBackground(String fileName) throws IOException {
backgroundImage = ImageIO.read(new File(fileName));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw the background image.
// g.drawImage(backgroundImage, 0, 0, this);
g.drawImage(backgroundImage, 0, 0, 550, 230, this);
}
}
如您所见,此类读取图像文件并将其引用放入名为backgroundImage的 Image 对象中,然后将其绘制在Graphics对象上。
2)然后我创建了一个名为LoginFrame2的类:
package com.test.login;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.Dimension;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu.Separator;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import net.miginfocom.swt.MigLayout;
import org.jdesktop.application.SingleFrameApplication;
public class LoginFrame2 extends SingleFrameApplication {
private static final int FIXED_WIDTH = 550;
private static final Dimension INITAL_SIZE = new Dimension(FIXED_WIDTH, 230);
public static void main(String[] args) {
System.out.println("DENTRO: LoginFrame() ---> main()");
launch(LoginFrame2.class, args);
}
@Override
protected void startup() {
// TODO Auto-generated method stub
System.out.println("Inside startup()");
JFrame mainFrame = this.getMainFrame(); // main JFrame that represents the Windows
mainFrame.setTitle("Chilli Login");
mainFrame.setPreferredSize(INITAL_SIZE);
mainFrame.setResizable(false);
Container mainContainer = mainFrame.getContentPane(); // main Container into the main JFrame
// JPanel creation and settings of the MigLayout on it:
// JPanel externalPanel = new JPanel();
JPanelWithBackground externalPanel = null;
try {
externalPanel = new JPanelWithBackground("/home/andrea/Immagini/logo2.jpg");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
externalPanel.setLayout(new net.miginfocom.swing.MigLayout("fill"));
externalPanel.add(new JLabel("Username"), "w 50%, wrap");
JTextField userNameTextField = new JTextField(20);
externalPanel.add(userNameTextField, "w 90%, wrap");
externalPanel.add(new JLabel("Password"), "w 50%, wrap");
JTextField pswdTextField = new JTextField(20);
externalPanel.add(pswdTextField, "w 90%, wrap");
JButton loginButton = new JButton("Login");
externalPanel.add(loginButton, "w 25%, wrap");
mainContainer.add(externalPanel);
//mainFrame.add(mainContainer);
show(mainFrame);
}
}
这个类扩展了JDesktop Swin 框架的SingleFrameApplication抽象类,它为我提供了一个JFrame istance。
此类只需按以下行创建JPanelWithBackground对象:
externalPanel = new JPanelWithBackground("/home/andrea/Immagini/logo2.jpg");
并在这个对象中放入一些 Swing 组件。
这是我的结果:
我的疑问/问题是:如您所见,显示字符串Password的JLabel位于图像的红色部分(我的辣椒徽标),因此可读性不强。
我该怎么做才能使其更具可读性?例如,我可以以某种方式设置我的JLabel的背景必须是白色的吗?
肿瘤坏死因子
安德烈亚