Java 一些可读性问题当JLabel位于Swing中JPanel的背景中时,如何解决?

Java 一些可读性问题当JLabel位于Swing中JPanel的背景中时,如何解决?,java,swing,jframe,jpanel,jlabel,Java,Swing,Jframe,Jpanel,Jlabel,我对JavaSwing开发非常陌生,我有以下问题 我必须创建一个具有背景图像的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.imag

我对JavaSwing开发非常陌生,我有以下问题

我必须创建一个具有背景图像的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);
    }
}
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);


    }

}
如您所见,该类读取一个Image文件,并将其引用放入名为backgroundImage的图像对象中,然后在图形对象上绘制它

2) 然后,我创建了一个名为LoginFrame2的类:

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);
    }
}
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);


    }

}
该类扩展了JDesktopSwin框架的SingleFrameApplication抽象类,该类为我提供了一个JFrameistance

此类只需通过以下行创建JPanelWithBackground对象:

externalPanel = new JPanelWithBackground("/home/andrea/Immagini/logo2.jpg");
在这个物体里放一些摆动的部件

这是我的结果:

我的疑问/问题是:正如您看到的JLabel所示,显示字符串密码的位于immage(我的辣椒标志)的红色部分,因此可读性不强

我能做些什么使它更具可读性?例如,我能否以某种方式设置我的JLabel的背景必须为白色

Tnx


Andrea

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

JLabel password = new JLabel("Password");
password.setForeground(Color.WHITE);
更极端的mods:将扩展标签以输出黑色轮廓的白色文本,或使用具有透明度的漂亮字体的文字图像

编辑: 下面是如何将背景设置为白色

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

您可以更改背景图像的不透明度

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D)g.create();

    // Draw the background image.
    // g.drawImage(backgroundImage, 0, 0, this);
    g2d.setComposite(AlphaComposite.SrcOvr.derive(0.5f));
    g2d.drawImage(backgroundImage, 0, 0, 550, 230, this);
    g2d.dispose();
}

不,可能是另一种字体或图像。
例如,我可以用某种方式设置我的JLabel的背景必须是白色吗?
-这很容易做到,但你似乎不太感谢你在所有其他问题中得到的帮助,所以我将传递这个。请你告诉我如何设置JLabel的背景?因为我们的程序的Windows版本(即用C#编写)使用这种方式…因此这可能是一个好主意。我只是在我的答案中添加了这一点,但这确实是基本的东西,我建议阅读一些教程。很好,而且…最后一件事…我可以设置百分比的不透明度吗?@AndreaNobili这个问题已经回答,您还没有接受它。你不明白论坛是如何运作的,因为你还没有接受其他帖子的回复<代码>我可以用百分比设置不透明度吗?当你甚至不能做一些简单的事情,比如在获得帮助时接受答案时,人们为什么还要继续提供帮助。@AndreaNobili“我可以用百分比设置不透明度吗?”不容易。一般来说,Swing要么不透明,要么透明。您可以使用一些“技巧”,但它们只会使布局和应用程序复杂化