简体   繁体   English

使用JXDatePicker时JLabel背景失真

[英]JLabel background distorts while using JXDatePicker

As soon as I click on JXDatePicker (named j in my program code), the JLabel (named l1 in my program code) background to which it is added gets distorted. 一旦我单击JXDatePicker (在我的程序代码中命名为j),添加它的JLabel (在我的程序代码中命名为l1)背景就会失真。 I also tried to repaint l1 whenever mouse is clicked, but, it doesn't work. 每当单击鼠标时,我也尝试重绘l1,但是,它不起作用。 Any help will be appreciated. 任何帮助将不胜感激。

Program Code : 程序代码:

import java.util.HashMap; 
import java.awt.*;
import java.awt.event.*;
import java.awt.font.TextAttribute;
import javax.swing.border.*;
import org.jdesktop.swingx.JXDatePicker;
import org.jdesktop.swingx.prompt.PromptSupport;
import javax.swing.*;


public class Registration
{
    JFrame stu_reg;
    JLabel back, logo, header, l1, l2, l3;
    JButton sub, cls;
    JTextField t1, t2;
    JFormattedTextField ft1;

    Connection c;

    Registration()
    {   
        try 
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } 
        catch (Exception ex) 
        {
            ex.printStackTrace();
        }

        stu_reg = new JFrame("Student Registration Form");
        stu_reg.setSize(1366,740);
        stu_reg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        stu_reg.setLayout(null);
        stu_reg.setVisible(true);
        stu_reg.setResizable(false);


        header = new JLabel("XYZ");
        header.setForeground(Color.RED);
        HashMap<TextAttribute, Object>  attribute = new HashMap<TextAttribute, Object>();
        attribute.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
        header.setFont(new Font("A.C.M.E. Secret Agent",Font.BOLD,30).deriveFont(attribute));
        header.setBounds(350, 0, 800, 75);
        stu_reg.add(header);

        ImageIcon image1 = new ImageIcon(Registration.class.getProtectionDomain().getCodeSource().getLocation().getPath() + "xyz.jpg");
        back = new JLabel(new ImageIcon(image1.getImage().getScaledInstance(1366, 730, Image.SCALE_SMOOTH)));
        back.setBounds(0, 0, 1366, 730);
        stu_reg.add(back);

        ImageIcon image = new ImageIcon(Registration.class.getProtectionDomain().getCodeSource().getLocation().getPath() + "R2.gif");
        logo = new JLabel(new ImageIcon(image.getImage().getScaledInstance(200, 200, Image.SCALE_SMOOTH)));
        logo.setBounds(1100, 0, 200, 200);
        back.add(logo);

        l1 = new JLabel();
        l1.setBackground(new Color(100, 100, 100, 70));
        l1.setOpaque(true);
        l1.setBounds(150, 80, 420, 590);
        LineBorder line = new LineBorder(Color.blue, 2, true);
        Font f = new Font("Scramble",Font.PLAIN,25).deriveFont(attribute);
        TitledBorder title = new TitledBorder(line, "Register", TitledBorder.LEFT, TitledBorder.TOP, f, new Color(225,80,0));
        l1.setBorder(title);
        back.add(l1);

        l2 = new JLabel("FULL NAME");
        l2.setFont(new Font("",Font.BOLD,14));
        l2.setBounds(27, 50, 100, 35);
        l1.add(l2);

        t1 = new JTextField(100);
        t1.setBounds(25, 80, 175, 35);
        PromptSupport.setPrompt("First Name", t1);
        PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT, t1);
        l1.add(t1);

        t2 = new JTextField(100);
        t2.setBounds(220, 80, 175, 35);
        PromptSupport.setPrompt("Last Name", t2);
        PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT, t2);
        l1.add(t2);

        l3 = new JLabel("DATE OF BIRTH");
        l3.setFont(new Font("",Font.BOLD,14));
        l3.setBounds(27, 130, 175, 35);
        l1.add(l3);

        JXDatePicker j=new JXDatePicker();
        j.setBounds(25, 160, 175, 33);
        j.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
            l1.repaint();
            }
        });
        l1.add(j);


        ToolTipManager.sharedInstance().setEnabled(false);


    }

    public static void main(String args[])
    {   
        SwingUtilities.invokeLater( ()->new Registration() );
    }
}

Without Distortion 无失真

无失真

After Distortion 失真之后

失真之后

l1.setBackground(new Color(100, 100, 100, 70)); l1.setBackground(new Color(100,100,100,70));

Swing doesn't support backgrounds with transparency properly. Swing不正确支持透明背景。 If a component is opaque then Swing expects the background to be opaque, not transparent. 如果组件是不透明的,则Swing希望背景是不透明的,而不是透明的。

If you want a transparent background then you need to do custom painting. 如果您想要透明的背景,则需要自定义绘画。

Check out Backgrounds With Transparency for more information and solutions. 请查看具有透明度的背景,以获取更多信息和解决方案。

First try setting the background color like so: 首先尝试像这样设置背景颜色:

l1.setBackground(new Color(100, 100, 100, 255));
l1.setOpaque(false);

Give it a test, does that work now? 给它测试,现在行得通吗?

If it doesnt work then also remove the following UIManager line at the start of your code UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 如果不起作用,则还要在代码的开头删除以下UIManager行UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); and then give it a test again. 然后再次进行测试。 Often some look&feel's will not allow you to use transparency correctly. 通常,某些外观不会允许您正确使用透明度。

If it works now, then you know that you will either need to abandon the look and feel, or keep it and use custom painting as camickr suggested. 如果现在可以使用,则您知道您将需要放弃外观,或者保留它并按照camickr的建议使用自定义绘画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM