英文:
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:
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:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论