简体   繁体   中英

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. I also tried to repaint l1 whenever mouse is clicked, but, it doesn't work. 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));

Swing doesn't support backgrounds with transparency properly. If a component is opaque then Swing expects the background to be opaque, not transparent.

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()); 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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