如何使用Java Swing创建无边框箭头按钮?

huangapple go评论101阅读模式
英文:

How to make non-border arrow buttons with Java Swing?

问题

I've translated the code-related part of your text:

我正在使用Java Swing来创建用户界面想要使它看起来像本机操作系统在这种情况下是Windows)。我已经使用了以下代码

setLookAndFeel(getSystemLookAndFeelClassName());

到目前为止一切都很好除了一个小细节我尝试使用箭头按钮来隐藏/显示一个JTextArea就像[MS的文档][1]中描述的那种即没有边框的那种如下所示

我已经尝试使用BasicArrowButton如下所示

BasicArrowButton arrowButton = new BasicArrowButton(EAST);
arrowButton.addActionListener((e) -> {
    if (textArea.isVisible()) {
        textArea.setVisible(false);
        arrowButton.setDirection(EAST);
    } else {
        textArea.setVisible(true);
        arrowButton.setDirection(SOUTH);
    }
});

但是我只能得到带边框的箭头按钮如下所示

我已经尝试过调整边框和背景但没有成功

有没有一种简洁的方法可以让它工作

谢谢

[1]: https://learn.microsoft.com/en-us/windows/win32/uxguide/ctrl-progressive-disclosure-controls

Please note that I've retained the original code for reference in the translation.

英文:

I'm using Java Swing to make a UI and the idea is to make it look like the native OS (in this case, Windows). I've used:

setLookAndFeel(getSystemLookAndFeelClassName());

And everything is good so far, except for one little detail. I'm trying to use the arrow buttons to hide/show a JTextArea like the ones described in MS's documentation, the ones with no border as shown here:

如何使用Java Swing创建无边框箭头按钮?

I've tried using BasicArrowButton as shown below:

BasicArrowButton arrowButton = new BasicArrowButton(EAST);
        arrowButton.addActionListener((e) -> {
            if (textArea.isVisible()) {
                textArea.setVisible(false);
                arrowButton.setDirection(EAST);
            } else {
                textArea.setVisible(true);
                arrowButton.setDirection(SOUTH);
            }
        });

But I only get the ones with borders, as shown here:

如何使用Java Swing创建无边框箭头按钮?

I've already tried playing around with borders and backgrounds but had no luck.

Is there a neat way to get this working?

Thanks.

答案1

得分: 4

使用 setBorderPainted 方法。

您不需要使用 BasicArrowButton;您可以使用常规的 JButton,并遵循当前的外观和感觉:

JButton leftButton = new JButton("\u25c2");
JButton rightButton = new JButton("\u25b8");
JButton upButton = new JButton("\u25b4");
JButton downButton = new JButton("\u25be");

leftButton.setBorderPainted(false);
rightButton.setBorderPainted(false);
upButton.setBorderPainted(false);
downButton.setBorderPainted(false);

(这些字符来自 Unicode 规范的 Geometric Shapes 区块中的 "small arrow" 字符。)

您可能还想隐藏焦点轮廓并启用鼠标悬停效果:

leftButton.setFocusPainted(false);
rightButton.setFocusPainted(false);
upButton.setFocusPainted(false);
downButton.setFocusPainted(false);

leftButton.setRolloverEnabled(true);
rightButton.setRolloverEnabled(true);
upButton.setRolloverEnabled(true);
downButton.setRolloverEnabled(true);

或者,您可以将每个按钮添加到 JToolBar 中,这将启用鼠标悬停效果,并在某些外观和感觉下,除非鼠标悬停在按钮上,否则将按钮保持透明。

英文:

Use the setBorderPainted method.

You don’t need to use BasicArrowButton; you can use a regular JButton, and thus adhere to the current look-and-feel:

JButton leftButton = new JButton("\u25c2");
JButton rightButton = new JButton("\u25b8");
JButton upButton = new JButton("\u25b4");
JButton downButton = new JButton("\u25be");

leftButton.setBorderPainted(false);
rightButton.setBorderPainted(false);
upButton.setBorderPainted(false);
downButton.setBorderPainted(false);

(Those characters are the “small arrow” characters from the Geometric Shapes block of the Unicode specification.)

You probably also want to hide the focus outline and enable rollover:

leftButton.setFocusPainted(false);
rightButton.setFocusPainted(false);
upButton.setFocusPainted(false);
downButton.setFocusPainted(false);

leftButton.setRolloverEnabled(true);
rightButton.setRolloverEnabled(true);
upButton.setRolloverEnabled(true);
downButton.setRolloverEnabled(true);

Alternatively, instead of calling setRolloverEnabled, you may want to add each button to a JToolBar, which will enable rollover, and in some look-and-feels, will leave the button transparent unless the mouse is rolled over it.

huangapple
  • 本文由 发表于 2020年8月12日 21:21:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63377445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定