【问题标题】:how to hide background of JButton (which containt icon image)?如何隐藏 JButton 的背景(包含图标图像)?
【发布时间】:2012-01-12 03:24:18
【问题描述】:

我有这个代码来制作一个带有图标图像的Jbutton,它可以工作。但问题是按钮的边框和背景不会消失。

我只想显示没有边框和按钮背景的图标图像。

我尝试设置setOpaque(false),但所有按钮都消失了!

我的代码有什么问题?

_button  = new JButton("Exit");
_button.setHorizontalTextPosition(SwingConstants.CENTER);
_button.setSize(200,130);
//_button.setContentAreaFilled(false);
_button.setBorderPainted(false);
//_button.setOpaque(false);
_button.setIgnoreRepaint(true);
//_button.setFocusable(false);
_button.setIcon(button_icon);
_button.setBounds(200, 200,200, 170);
_button.setRolloverEnabled(true);
_button.setRolloverIcon(button_icon_hover);
_button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
//_button.addActionListener(this);
_button.setBackground(null);
_button.setFocusable(true);
_button.setFocusPainted(true);
_button.setForeground(Color.WHITE);
_button.setFont(new Font("Times New Roman",Font.BOLD,35));

【问题讨论】:

    标签: java swing jbutton


    【解决方案1】:

    试试这个

    JButton play = new JButton("This",new ImageIcon("src\play.png"));
            play.setBorderPainted(false); 
            play.setContentAreaFilled(false); 
            play.setFocusPainted(false); 
            play.setOpaque(false);
    

    【讨论】:

    • 我使用此代码,但边框和背景消失了,问题是图像不可点击
    • @SanjitKumarMishra 你为什么说部分?
    • @YankiTwizzy:它并没有完全删除背景,所以我只说了一部分。可能我正在使用的 LookAndFeel 限制了这样做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多