gpt4 book ai didi

java - Swing 中JLabel 在JPanel 的背景图像上时的一些可读性问题,如何解决?

转载 作者:行者123 更新时间:2023-11-29 05:34:54 25 4
gpt4 key购买 nike

我是 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 实例。

此类简单地通过以下行创建 JPanelWithBackground 对象:

externalPanel = new JPanelWithBackground("/home/andrea/Immagini/logo2.jpg");

并在此对象中放入一些 Swing 组件。

这是我的结果:

enter image description here

我的疑问/问题是:如您所见,显示字符串 PasswordJLabel 位于图像(我的辣椒 Logo )的红色部分,因此不是很可读。

我可以做些什么来让它更具可读性?例如,我可以通过某种方式将我的 JLabel 的背景设置为白色吗?

谢谢

安德里亚

最佳答案

您可以设置 JLabel 的背景,但这看起来很难看。我建议只将密码标签的前景更改为白色。也可能增加两个标签的字体大小以提高可读性。

JLabel password = new JLabel("Password");
password.setForeground(Color.WHITE);

更极端的 mods:将扩展标签以输出黑色轮廓的白色文本,或者使用图像作为具有透明度的漂亮字体中的文字。

编辑:这是将背景设置为白色的方法。

password.setBackground(Color.WHITE);
password.setOpaque(true);

关于java - Swing 中JLabel 在JPanel 的背景图像上时的一些可读性问题,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19866638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com