[英]JLabel background distorts while using JXDatePicker
一旦我單擊JXDatePicker
(在我的程序代碼中命名為j),添加它的JLabel
(在我的程序代碼中命名為l1)背景就會失真。 每當單擊鼠標時,我也嘗試重繪l1,但是,它不起作用。 任何幫助將不勝感激。
程序代碼:
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() );
}
}
無失真
失真之后
l1.setBackground(new Color(100,100,100,70));
Swing不正確支持透明背景。 如果組件是不透明的,則Swing希望背景是不透明的,而不是透明的。
如果您想要透明的背景,則需要自定義繪畫。
請查看具有透明度的背景,以獲取更多信息和解決方案。
首先嘗試像這樣設置背景顏色:
l1.setBackground(new Color(100, 100, 100, 255));
l1.setOpaque(false);
給它測試,現在行得通嗎?
如果不起作用,則還要在代碼的開頭刪除以下UIManager行UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
然后再次進行測試。 通常,某些外觀不會允許您正確使用透明度。
如果現在可以使用,則您知道您將需要放棄外觀,或者保留它並按照camickr的建議使用自定義繪畫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.