Java JLabel背景在使用JXDatePicker时失真
只要我点击Java JLabel背景在使用JXDatePicker时失真,java,swing,user-interface,Java,Swing,User Interface,只要我点击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
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() );
}
}
import java.util.HashMap;
导入java.awt.*;
导入java.awt.event.*;
导入java.awt.font.texttribute;
导入javax.swing.border.*;
导入org.jdesktop.swingx.JXDatePicker;
导入org.jdesktop.swingx.prompt.PromptSupport;
导入javax.swing.*;
公开课注册
{
JFrame stu_reg;
JLabel back、徽标、标题、l1、l2、l3;
JButton sub,cls;
jtextfieldt1,t2;
JFormattedTextField ft1;
连接c;
注册()
{
尝试
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
捕获(例外情况除外)
{
例如printStackTrace();
}
stu_reg=新JFrame(“学生登记表”);
学生注册设置尺寸(1366740);
stu_reg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
stu注册设置布局(空);
stu_reg.setVisible(真);
stu_reg.可设置大小(假);
标题=新的JLabel(“XYZ”);
标题.设置前景(颜色.红色);
HashMap属性=新建HashMap();
attribute.put(texttribute.UNDERLINE,texttribute.UNDERLINE\ON);
header.setFont(新字体(“A.C.M.E.Secret Agent”),字体粗体,30.deriveFont(属性));
收割台.立根(350,0,800,75);
stu注册添加(标题);
ImageIcon image1=新的ImageIcon(Registration.class.getProtectionDomain().getCodeSource().getLocation().getPath()+“xyz.jpg”);
back=new JLabel(新的ImageIcon(image1.getImage().getScaledInstance(1366730,Image.SCALE_-SMOOTH));
后退。后退(0,01366730);
学生注册添加(返回);
ImageIcon image=新的ImageIcon(Registration.class.getProtectionDomain().getCodeSource().getLocation().getPath()+“R2.gif”);
logo=new JLabel(新的图像图标(image.getImage().getScaledInstance(200200,image.SCALE_SMOOTH));
标志.立根(1100,0,200,200);
背面。添加(徽标);
l1=新的JLabel();
l1.挫折背景(新颜色(100、100、100、70));
l1.设置不透明(真);
l1.立根(150、80、420、590);
LineBorder line=新的LineBorder(Color.blue,2,true);
Font f=新字体(“置乱”,字体.PLAIN,25)。派生字体(属性);
TitledBorder title=新的TitledBorder(行,“寄存器”,TitledBorder.LEFT,TitledBorder.TOP,f,新颜色(225,80,0));
l1.订单(标题);
返回。添加(l1);
l2=新的JLabel(“全名”);
l2.setFont(新字体(“,Font.BOLD,14));
l2.立根(27,50,100,35);
l1.添加(l2);
t1=新的JTextField(100);
t1.立根(25,80,175,35);
setPrompt(“名字”,t1);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT,t1);
l1.添加(t1);
t2=新的JTextField(100);
t2.立根(220,80,175,35);
setPrompt(“姓氏”,t2);
PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT,t2);
l1.添加(t2);
l3=新的JLabel(“出生日期”);
l3.setFont(新字体(“,Font.BOLD,14));
l3.立根(27、130、175、35);
l1.添加(l3);
JXDatePicker j=新的JXDatePicker();
j、 挫折(25、160、175、33);
j、 addMouseListener(新的MouseAdapter(){
公共无效mouseClicked(MouseEvent e){
l1.重新绘制();
}
});
l1.加入(j);
ToolTimManager.sharedInstance().setEnabled(false);
}
公共静态void main(字符串参数[])
{
调用器(()->new Registration());
}
}
无失真
失真后
l1.挫折背景(新颜色(100、100、100、70))
Swing不支持具有适当透明度的背景。如果组件是不透明的,那么Swing希望背景是不透明的,而不是透明的
如果你想要一个透明的背景,那么你需要做自定义绘画
查看更多信息和解决方案
l1.挫折背景(新颜色(100、100、100、70))
Swing不支持具有适当透明度的背景。如果组件是不透明的,那么Swing希望背景是不透明的,而不是透明的
如果你想要一个透明的背景,那么你需要做自定义绘画
查看以了解更多信息和解决方案。首先尝试如下设置背景颜色:
l1.setBackground(new Color(100, 100, 100, 255));
l1.setOpaque(false);
测试一下,现在可以了吗
如果它不起作用,那么也删除代码开头的以下UIManager行UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())代码>然后再次对其进行测试。通常,某些外观不允许您正确使用透明度
如果它现在起作用,那么您知道您要么需要放弃外观和感觉,要么保留它并按照camickr的建议使用自定义绘画 首先尝试设置背景色,如下所示:
l1.setBackground(new Color(100, 100, 100, 255));
l1.setOpaque(false);
测试一下,现在可以了吗
如果它不起作用,那么也删除代码开头的以下UIManager行UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())代码>然后再次对其进行测试。通常,某些外观不允许您正确使用透明度
如果它现在起作用,那么您知道您要么需要放弃外观和感觉,要么保留它并按照camickr的建议使用自定义绘画 标识符,如l1
,可能相当神秘;使用时,它们通常应该具有尽可能窄的范围。使用null
布局是alm