如何更改JButton?

发布于 2024-12-10 11:32:38 字数 407 浏览 0 评论 0原文

我想让 JButton 看起来像 HTML 中的链接。它仍然是一个按钮,当我单击它时,它将打开一些新的 JFrame。但我想改变它的样子。我尝试了这个:

JButton button = new JButton();
button.setText("hi");
button.setContentAreaFilled(false);
button.setBorderPainted(false);
button.setOpaque(false);
button.setBackground(Color.WHITE);
button.setCursor(Cursor.getPredefinedCursor(12)); 

但是我的按钮文本周围仍然有一些边框。怎样才能彻底去掉边框呢?

I want to make a JButton look like a link in HTML. It will still be a button and when I click on it it will open some new JFrame. But I want to change what it looks like. I tried this:

JButton button = new JButton();
button.setText("hi");
button.setContentAreaFilled(false);
button.setBorderPainted(false);
button.setOpaque(false);
button.setBackground(Color.WHITE);
button.setCursor(Cursor.getPredefinedCursor(12)); 

But there is still some border around the text of my button. How do we remove the border completely?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江城子 2024-12-17 11:32:38

但是我的按钮文本周围仍然有一些边框

button.setFocusPainted( false );

But there is still some border around the text of my button

button.setFocusPainted( false );
笑咖 2024-12-17 11:32:38

我建议使用 JTextField 而不是 JButton 来进行此用途。

EG

LinkLabel

这就是当鼠标悬停在第一个链接上时的显示方式。

链接标签.java

/* License - LGPL
LinkLabel Using the Desktop Class

There are a lot of link labels.  This one uses the Java 1.6+ 
Desktop class for its functionality, and is thereby suitable 
for applets, applications launched using JWS, and 'standard' 
(non-JWS) desktop applications.

Note that the Desktop class can be used with pre 1.6 JREs. It 
is available through JDIC. 
https://jdic.dev.java.net/nonav/documentation/javadoc/jdic/org/jdesktop/jdic/desktop/Desktop.html
*/

import java.awt.Desktop;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.BorderLayout;
import java.awt.GridLayout;

import java.awt.event.*;

import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JOptionPane;

import javax.swing.border.MatteBorder;
import javax.swing.border.Border;

import java.net.URI;
import java.io.File;

/**
A Java 1.6+ LinkLabel that uses the Desktop class for opening
the document of interest.

The Desktop.browse(URI) method can be invoked from applications,
applets and apps. launched using Java Webstart.  In the latter
two cases, the usual fall-back methods are used for sandboxed apps
(see the JavaDocs for further details).

While called a 'label', this class actually extends JTextField,
to easily allow the component to become focusable using keyboard
navigation.

To successfully browse to a URI for a local File, the file name
must be constructed using a canonical path.

@author Andrew Thompson
@version 2008/08/23
*/
public class LinkLabel
    // we extend a JTextField, to get a focusable component
    extends JTextField
    implements MouseListener, FocusListener, ActionListener {

    /** The target or href of this link. */
    private URI target;

    public Color standardColor = new Color(0,0,255);
    public Color hoverColor = new Color(255,0,0);
    public Color activeColor = new Color(128,0,128);
    public Color transparent = new Color(0,0,0,0);

    public boolean underlineVisible = true;

    private Border activeBorder;
    private Border hoverBorder;
    private Border standardBorder;

    /** Construct a LinkLabel that points to the given target.
    The URI will be used as the link text.*/
    public LinkLabel(URI target) {
        this( target, target.toString() );
    }

    /** Construct a LinkLabel that points to the given target,
    and displays the text to the user. */
    public LinkLabel(URI target, String text) {
        super(text);
        this.target = target;
    }

    /* Set the active color for this link (default is purple). */
    public void setActiveColor(Color active) {
        activeColor = active;
    }

    /* Set the hover/focused color for this link (default is red). */
    public void setHoverColor(Color hover) {
        hoverColor = hover;
    }

    /* Set the standard (non-focused, non-active) color for this
    link (default is blue). */
    public void setStandardColor(Color standard) {
        standardColor = standard;
    }

    /** Determines whether the */
    public void setUnderlineVisible(boolean underlineVisible) {
        this.underlineVisible = underlineVisible;
    }

    /* Add the listeners, configure the field to look and act
    like a link. */
    public void init() {
        this.addMouseListener(this);
        this.addFocusListener(this);
        this.addActionListener(this);
        setToolTipText(target.toString());

        if (underlineVisible) {
            activeBorder = new MatteBorder(0,0,1,0,activeColor);
            hoverBorder = new MatteBorder(0,0,1,0,hoverColor);
            standardBorder = new MatteBorder(0,0,1,0,transparent);
        } else {
            activeBorder = new MatteBorder(0,0,0,0,activeColor);
            hoverBorder = new MatteBorder(0,0,0,0,hoverColor);
            standardBorder = new MatteBorder(0,0,0,0,transparent);
        }

        // make it appear like a label/link
        setEditable(false);
        setForeground(standardColor);
        setBorder(standardBorder);
        setCursor( new Cursor(Cursor.HAND_CURSOR) );
    }

    /** Browse to the target URI using the Desktop.browse(URI)
    method.  For visual indication, change to the active color
    at method start, and return to the standard color once complete.
    This is usually so fast that the active color does not appear,
    but it will take longer if there is a problem finding/loading
    the browser or URI (e.g. for a File). */
    public void browse() {
        setForeground(activeColor);
        setBorder(activeBorder);
        try {
            Desktop.getDesktop().browse(target);
        } catch(Exception e) {
            e.printStackTrace();
        }
        setForeground(standardColor);
        setBorder(standardBorder);
    }

    /** Browse to the target. */
    public void actionPerformed(ActionEvent ae) {
        browse();
    }

    /** Browse to the target. */
    public void mouseClicked(MouseEvent me) {
        browse();
    }

    /** Set the color to the hover color. */
    public void mouseEntered(MouseEvent me) {
        setForeground(hoverColor);
        setBorder(hoverBorder);
    }

    /** Set the color to the standard color. */
    public void mouseExited(MouseEvent me) {
        setForeground(standardColor);
        setBorder(standardBorder);
    }

    public void mouseReleased(MouseEvent me) {}

    public void mousePressed(MouseEvent me) {}

    /** Set the color to the standard color. */
    public void focusLost(FocusEvent fe) {
        setForeground(standardColor);
        setBorder(standardBorder);
    }

    /** Set the color to the hover color. */
    public void focusGained(FocusEvent fe) {
        setForeground(hoverColor);
        setBorder(hoverBorder);
    }

    public static void main(String[] args) throws Exception {
        JPanel p = new JPanel(new GridLayout(0,1));
        File f = new File(".","LinkLabel.java");

        /* Filename must be constructed with a canonical path in
        order to successfully use Desktop.browse(URI)! */
        f = new File(f.getCanonicalPath());

        URI uriFile = f.toURI();

        LinkLabel linkLabelFile = new LinkLabel(uriFile);
        linkLabelFile.init();
        p.add(linkLabelFile);

        LinkLabel linkLabelWeb = new LinkLabel(
            new URI("http://pscode.org/sscce.html"),
            "SSCCE");
        linkLabelWeb.setStandardColor(new Color(0,128,0));
        linkLabelWeb.setHoverColor(new Color(222,128,0));
        linkLabelWeb.init();

        /* This shows a quirk of the LinkLabel class, the
        size of the text field needs to be constrained to
        get the underline to appear properly. */
        p.add(linkLabelWeb);

        LinkLabel linkLabelConstrain = new LinkLabel(
            new URI("http://sdnshare.sun.com/"),
            "SDN Share");
        linkLabelConstrain.init();
        /* ..and this shows one way to constrain the size
        (appropriate for this layout).
        Similar tricks can be used to ensure the underline does
        not drop too far *below* the link (think BorderLayout
        NORTH/SOUTH).
        The same technique can also be nested further to produce
        a NORTH+EAST packing (for example). */
        JPanel labelConstrain = new JPanel(new BorderLayout());
        labelConstrain.add( linkLabelConstrain, BorderLayout.EAST );
        p.add(labelConstrain);

        LinkLabel linkLabelNoUnderline = new LinkLabel(
            new URI("http://java.net/"),
            "java.net");
        // another way to deal with the underline is to remove it
        linkLabelNoUnderline.setUnderlineVisible(false);
        // we can use the methods inherited from JTextField
        linkLabelNoUnderline.
            setHorizontalAlignment(JTextField.CENTER);
        linkLabelNoUnderline.init();
        p.add(linkLabelNoUnderline);

        JOptionPane.showMessageDialog( null, p );
    }
}

I recommend using a JTextField rather than a JButton for this use.

E.G.

LinkLabel

That is how it appears when the mouse is hovering over the first link.

LinkLabel.java

/* License - LGPL
LinkLabel Using the Desktop Class

There are a lot of link labels.  This one uses the Java 1.6+ 
Desktop class for its functionality, and is thereby suitable 
for applets, applications launched using JWS, and 'standard' 
(non-JWS) desktop applications.

Note that the Desktop class can be used with pre 1.6 JREs. It 
is available through JDIC. 
https://jdic.dev.java.net/nonav/documentation/javadoc/jdic/org/jdesktop/jdic/desktop/Desktop.html
*/

import java.awt.Desktop;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.BorderLayout;
import java.awt.GridLayout;

import java.awt.event.*;

import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JOptionPane;

import javax.swing.border.MatteBorder;
import javax.swing.border.Border;

import java.net.URI;
import java.io.File;

/**
A Java 1.6+ LinkLabel that uses the Desktop class for opening
the document of interest.

The Desktop.browse(URI) method can be invoked from applications,
applets and apps. launched using Java Webstart.  In the latter
two cases, the usual fall-back methods are used for sandboxed apps
(see the JavaDocs for further details).

While called a 'label', this class actually extends JTextField,
to easily allow the component to become focusable using keyboard
navigation.

To successfully browse to a URI for a local File, the file name
must be constructed using a canonical path.

@author Andrew Thompson
@version 2008/08/23
*/
public class LinkLabel
    // we extend a JTextField, to get a focusable component
    extends JTextField
    implements MouseListener, FocusListener, ActionListener {

    /** The target or href of this link. */
    private URI target;

    public Color standardColor = new Color(0,0,255);
    public Color hoverColor = new Color(255,0,0);
    public Color activeColor = new Color(128,0,128);
    public Color transparent = new Color(0,0,0,0);

    public boolean underlineVisible = true;

    private Border activeBorder;
    private Border hoverBorder;
    private Border standardBorder;

    /** Construct a LinkLabel that points to the given target.
    The URI will be used as the link text.*/
    public LinkLabel(URI target) {
        this( target, target.toString() );
    }

    /** Construct a LinkLabel that points to the given target,
    and displays the text to the user. */
    public LinkLabel(URI target, String text) {
        super(text);
        this.target = target;
    }

    /* Set the active color for this link (default is purple). */
    public void setActiveColor(Color active) {
        activeColor = active;
    }

    /* Set the hover/focused color for this link (default is red). */
    public void setHoverColor(Color hover) {
        hoverColor = hover;
    }

    /* Set the standard (non-focused, non-active) color for this
    link (default is blue). */
    public void setStandardColor(Color standard) {
        standardColor = standard;
    }

    /** Determines whether the */
    public void setUnderlineVisible(boolean underlineVisible) {
        this.underlineVisible = underlineVisible;
    }

    /* Add the listeners, configure the field to look and act
    like a link. */
    public void init() {
        this.addMouseListener(this);
        this.addFocusListener(this);
        this.addActionListener(this);
        setToolTipText(target.toString());

        if (underlineVisible) {
            activeBorder = new MatteBorder(0,0,1,0,activeColor);
            hoverBorder = new MatteBorder(0,0,1,0,hoverColor);
            standardBorder = new MatteBorder(0,0,1,0,transparent);
        } else {
            activeBorder = new MatteBorder(0,0,0,0,activeColor);
            hoverBorder = new MatteBorder(0,0,0,0,hoverColor);
            standardBorder = new MatteBorder(0,0,0,0,transparent);
        }

        // make it appear like a label/link
        setEditable(false);
        setForeground(standardColor);
        setBorder(standardBorder);
        setCursor( new Cursor(Cursor.HAND_CURSOR) );
    }

    /** Browse to the target URI using the Desktop.browse(URI)
    method.  For visual indication, change to the active color
    at method start, and return to the standard color once complete.
    This is usually so fast that the active color does not appear,
    but it will take longer if there is a problem finding/loading
    the browser or URI (e.g. for a File). */
    public void browse() {
        setForeground(activeColor);
        setBorder(activeBorder);
        try {
            Desktop.getDesktop().browse(target);
        } catch(Exception e) {
            e.printStackTrace();
        }
        setForeground(standardColor);
        setBorder(standardBorder);
    }

    /** Browse to the target. */
    public void actionPerformed(ActionEvent ae) {
        browse();
    }

    /** Browse to the target. */
    public void mouseClicked(MouseEvent me) {
        browse();
    }

    /** Set the color to the hover color. */
    public void mouseEntered(MouseEvent me) {
        setForeground(hoverColor);
        setBorder(hoverBorder);
    }

    /** Set the color to the standard color. */
    public void mouseExited(MouseEvent me) {
        setForeground(standardColor);
        setBorder(standardBorder);
    }

    public void mouseReleased(MouseEvent me) {}

    public void mousePressed(MouseEvent me) {}

    /** Set the color to the standard color. */
    public void focusLost(FocusEvent fe) {
        setForeground(standardColor);
        setBorder(standardBorder);
    }

    /** Set the color to the hover color. */
    public void focusGained(FocusEvent fe) {
        setForeground(hoverColor);
        setBorder(hoverBorder);
    }

    public static void main(String[] args) throws Exception {
        JPanel p = new JPanel(new GridLayout(0,1));
        File f = new File(".","LinkLabel.java");

        /* Filename must be constructed with a canonical path in
        order to successfully use Desktop.browse(URI)! */
        f = new File(f.getCanonicalPath());

        URI uriFile = f.toURI();

        LinkLabel linkLabelFile = new LinkLabel(uriFile);
        linkLabelFile.init();
        p.add(linkLabelFile);

        LinkLabel linkLabelWeb = new LinkLabel(
            new URI("http://pscode.org/sscce.html"),
            "SSCCE");
        linkLabelWeb.setStandardColor(new Color(0,128,0));
        linkLabelWeb.setHoverColor(new Color(222,128,0));
        linkLabelWeb.init();

        /* This shows a quirk of the LinkLabel class, the
        size of the text field needs to be constrained to
        get the underline to appear properly. */
        p.add(linkLabelWeb);

        LinkLabel linkLabelConstrain = new LinkLabel(
            new URI("http://sdnshare.sun.com/"),
            "SDN Share");
        linkLabelConstrain.init();
        /* ..and this shows one way to constrain the size
        (appropriate for this layout).
        Similar tricks can be used to ensure the underline does
        not drop too far *below* the link (think BorderLayout
        NORTH/SOUTH).
        The same technique can also be nested further to produce
        a NORTH+EAST packing (for example). */
        JPanel labelConstrain = new JPanel(new BorderLayout());
        labelConstrain.add( linkLabelConstrain, BorderLayout.EAST );
        p.add(labelConstrain);

        LinkLabel linkLabelNoUnderline = new LinkLabel(
            new URI("http://java.net/"),
            "java.net");
        // another way to deal with the underline is to remove it
        linkLabelNoUnderline.setUnderlineVisible(false);
        // we can use the methods inherited from JTextField
        linkLabelNoUnderline.
            setHorizontalAlignment(JTextField.CENTER);
        linkLabelNoUnderline.init();
        p.add(linkLabelNoUnderline);

        JOptionPane.showMessageDialog( null, p );
    }
}
仅此而已 2024-12-17 11:32:38

试试这个,它应该强制它设置无边框:

setBorder(BorderFactory.createEmptyBorder());

try this, it should force it to set no border :

setBorder(BorderFactory.createEmptyBorder());
黑凤梨 2024-12-17 11:32:38

只有一种正确的方法,改变外观和感觉

或简单的MetalButtonUI 的示例仅限 Windows 操作系统

there are only one correct way, to change Look and Feel

or simple example for MetalButtonUI and Windows OS only

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文