在 Swing 中是否有提供 GUI 切换开关的标准实现或库?我知道 Swing 提供了一个切换按钮,但 UX 有一些不足之处。我正在寻找这个:
此外,这种类型的控制是否有规范的术语?Apple HIG 将其称为 UISwitch。我也尝试搜索“拨动开关”,但运气不佳。(大量的 JavaScript 结果,但没有原生的。)
在 Swing 中是否有提供 GUI 切换开关的标准实现或库?我知道 Swing 提供了一个切换按钮,但 UX 有一些不足之处。我正在寻找这个:
此外,这种类型的控制是否有规范的术语?Apple HIG 将其称为 UISwitch。我也尝试搜索“拨动开关”,但运气不佳。(大量的 JavaScript 结果,但没有原生的。)
您可以通过使用两个图标来表示开和关来模仿它,然后将它们设置为JToggleButton
.
顺便说一句,用户希望看到代表“最不意外的路径”的逻辑和一致的 GUI,是开发人员认为用户想要一个“漂亮、聪明”的 GUI(并且他们可以设计一个)。为什么他们想要对标准切换按钮进行这样的控制?
我不知道标准的,但是创建 Steel Series 组件的Gerrit Grunwald创建了一个他称之为Steel Checkbox的实现
Swing 没有像您描述的那样的标准开关。如果找不到第三方,最好的选择就是简单地写一个。我接近它的方式是这样一个简单的结构:
• JLabel
• 覆盖paintComponent
• 检查状态,例如isOn()
• 添加一个MouseListener
来切换状态。
• 定制绘画将考虑标签值和尺寸。
我可以给你发一个我前段时间写的,但你可能对你想要什么有一个非常具体的想法,所以可能值得花半个小时来构建它。
猜猜我迟到了 6 年,但对于那些仍在寻找简单解决方案的人来说:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.Cursor;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class ToggleSwitch extends JPanel {
private boolean activated = false;
private Color switchColor = new Color(200, 200, 200), buttonColor = new Color(255, 255, 255), borderColor = new Color(50, 50, 50);
private Color activeSwitch = new Color(0, 125, 255);
private BufferedImage puffer;
private int borderRadius = 10;
private Graphics2D g;
public ToggleSwitch() {
super();
setVisible(true);
addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent arg0) {
activated = !activated;
repaint();
}
});
setCursor(new Cursor(Cursor.HAND_CURSOR));
setBounds(0, 0, 41, 21);
}
@Override
public void paint(Graphics gr) {
if(g == null || puffer.getWidth() != getWidth() || puffer.getHeight() != getHeight()) {
puffer = (BufferedImage) createImage(getWidth(), getHeight());
g = (Graphics2D)puffer.getGraphics();
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHints(rh);
}
g.setColor(activated?activeSwitch:switchColor);
g.fillRoundRect(0, 0, this.getWidth()-1,getHeight()-1, 5, borderRadius);
g.setColor(borderColor);
g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 5, borderRadius);
g.setColor(buttonColor);
if(activated) {
g.fillRoundRect(getWidth()/2, 1, (getWidth()-1)/2 -2, (getHeight()-1) - 2, borderRadius, borderRadius);
g.setColor(borderColor);
g.drawRoundRect((getWidth()-1)/2, 0, (getWidth()-1)/2, (getHeight()-1), borderRadius, borderRadius);
}
else {
g.fillRoundRect(1, 1, (getWidth()-1)/2 -2, (getHeight()-1) - 2, borderRadius, borderRadius);
g.setColor(borderColor);
g.drawRoundRect(0, 0, (getWidth()-1)/2, (getHeight()-1), borderRadius, borderRadius);
}
gr.drawImage(puffer, 0, 0, null);
}
public boolean isActivated() {
return activated;
}
public void setActivated(boolean activated) {
this.activated = activated;
}
public Color getSwitchColor() {
return switchColor;
}
/**
* Unactivated Background Color of switch
*/
public void setSwitchColor(Color switchColor) {
this.switchColor = switchColor;
}
public Color getButtonColor() {
return buttonColor;
}
/**
* Switch-Button color
*/
public void setButtonColor(Color buttonColor) {
this.buttonColor = buttonColor;
}
public Color getBorderColor() {
return borderColor;
}
/**
* Border-color of whole switch and switch-button
*/
public void setBorderColor(Color borderColor) {
this.borderColor = borderColor;
}
public Color getActiveSwitch() {
return activeSwitch;
}
public void setActiveSwitch(Color activeSwitch) {
this.activeSwitch = activeSwitch;
}
/**
* @return the borderRadius
*/
public int getBorderRadius() {
return borderRadius;
}
/**
* @param borderRadius the borderRadius to set
*/
public void setBorderRadius(int borderRadius) {
this.borderRadius = borderRadius;
}
}
只需将其复制到 ToggleSwitch.java 中即可。您可以像这样将它添加到您的 JFrame 中:
ToggleSwitch ts = new ToggleSwitch();
ts.setLocation(5, 135);
frame.add(ts);