一旦我点击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() );
}
}
无失真
失真后
答案 0 :(得分:4)
l1.setBackground(new Color(100,100,100,70));
Swing不支持正确透明的背景。如果一个组件是不透明的,那么Swing希望背景是不透明的,而不是透明的。
如果你想要一个透明的背景,那么你需要做自定义绘画。
查看Backgrounds With Transparency以获取更多信息和解决方案。
答案 1 :(得分:1)
首先尝试设置背景颜色:
l1.setBackground(new Color(100, 100, 100, 255));
l1.setOpaque(false);
给它一个测试,现在有效吗?
如果它不起作用,那么还要在代码UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
的开头删除以下UIManager行,然后再次进行测试。通常一些外观和感觉不允许您正确使用透明度。
如果它现在有效,那么你知道你要么放弃外观,要么保留它并使用自定义绘画作为camickr建议。