回答
可能通過使用JScrollBar中,這裏只是和未完成的想法,給你
import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Sliders {
private JFrame frame;
private JPanel main;
private JPanel scrollBarPanel = new JPanel();
private JPanel sliderPanel;
public Sliders() {
frame = new JFrame();
main = new JPanel(new GridLayout(2, 1));
final JScrollBar scrollBar = new JScrollBar(JScrollBar.HORIZONTAL, 0, 100, 0, 200);
int height = scrollBar.getPreferredSize().height;
scrollBar.setPreferredSize(new Dimension(175, height));
scrollBarPanel.add(scrollBar);
main.add(scrollBarPanel);
sliderPanel = new JPanel();
final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 255, 128);
slider.setMajorTickSpacing(48);
slider.setMinorTickSpacing(16);
slider.setPaintTicks(true);
sliderPanel.add(slider);
main.add(sliderPanel);
frame.add(main, BorderLayout.CENTER);
scrollBar.addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
System.out.println("JScrollBar's current value = " + scrollBar.getValue());
}
});
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println("JSlider's current value = " + slider.getValue());
}
});
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Sliders sliders = new Sliders();
}
});
}
}
不會 - 你必須執行選擇邏輯(又名:ButtonModel的),以及 – kleopatra
@kleopatra嗯不錯嘛,...,I」迷失的地方,只有不知道這方式http://stackoverflow.com/questions/4218117/sliding-window-under-a-jpanel/7291620#7291620,超過錯覺可能是可能的,不hesitage更新代碼,我張貼, – mKorbel
+1有趣。我會在圖標中使用'JToggleButton',但'BoundedRangeModel'工作在'新的JSlider(JSlider.HORIZONTAL,0,1,0)'中。 – trashgod
只是一個小例子,它可能會幫助你。
public class Popup extends JFrame {
public Popup() {
setBounds(100, 100, 300, 120);
setDefaultCloseOperation(3);
getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING, 10, 10));
getContentPane().add(new JSwitchBox("on", "off"));
getContentPane().add(new JSwitchBox("yes", "no"));
getContentPane().add(new JSwitchBox("true", "false"));
getContentPane().add(new JSwitchBox("on", "off"));
getContentPane().add(new JSwitchBox("yes", "no"));
getContentPane().add(new JSwitchBox("true", "false"));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Popup().setVisible(true);
}
});
}
public class JSwitchBox extends AbstractButton{
private Color colorBright = new Color(220,220,220);
private Color colorDark = new Color(150,150,150);
private Color black = new Color(0,0,0,100);
private Color white = new Color(255,255,255,100);
private Color light = new Color(220,220,220,100);
private Color red = new Color(160,130,130);
private Color green = new Color(130,160,130);
private Font font = new JLabel().getFont();
private int gap = 5;
private int globalWitdh = 0;
private final String trueLabel;
private final String falseLabel;
private Dimension thumbBounds;
private Rectangle2D bounds;
private int max;
public JSwitchBox(String trueLabel, String falseLabel) {
this.trueLabel = trueLabel;
this.falseLabel = falseLabel;
double trueLenth = getFontMetrics(getFont()).getStringBounds(trueLabel, getGraphics()).getWidth();
double falseLenght = getFontMetrics(getFont()).getStringBounds(falseLabel, getGraphics()).getWidth();
max = (int)Math.max(trueLenth, falseLenght);
gap = Math.max(5, 5+(int)Math.abs(trueLenth - falseLenght));
thumbBounds = new Dimension(max+gap*2,20);
globalWitdh = max + thumbBounds.width+gap*2;
setModel(new DefaultButtonModel());
setSelected(false);
addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if(new Rectangle(getPreferredSize()).contains(e.getPoint())) {
setSelected(!isSelected());
}
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(globalWitdh, thumbBounds.height);
}
@Override
public void setSelected(boolean b) {
if(b){
setText(trueLabel);
setBackground(green);
} else {
setBackground(red);
setText(falseLabel);
}
super.setSelected(b);
}
@Override
public void setText(String text) {
super.setText(text);
}
@Override
public int getHeight() {
return getPreferredSize().height;
}
@Override
public int getWidth() {
return getPreferredSize().width;
}
@Override
public Font getFont() {
return font;
}
@Override
protected void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRoundRect(1, 1, getWidth()-2 - 1, getHeight()-2 ,2 ,2);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(black);
g2.drawRoundRect(1, 1, getWidth()-2 - 1, getHeight()-2 - 1, 2,2);
g2.setColor(white);
g2.drawRoundRect(1 + 1, 1 + 1, getWidth()-2 - 3, getHeight()-2 - 3, 2,2);
int x = 0;
int lx = 0;
if(isSelected()) {
lx = thumbBounds.width;
} else {
x = thumbBounds.width;
}
int y = 0;
int w = thumbBounds.width;
int h = thumbBounds.height;
g2.setPaint(new GradientPaint(x, (int)(y-0.1*h), colorDark , x, (int)(y+1.2*h), light));
g2.fillRect(x, y, w, h);
g2.setPaint(new GradientPaint(x, (int)(y+.65*h), light , x, (int)(y+1.3*h), colorDark));
g2.fillRect(x, (int)(y+.65*h), w, (int)(h-.65*h));
if (w>14){
int size = 10;
g2.setColor(colorBright);
g2.fillRect(x+w/2-size/2,y+h/2-size/2, size, size);
g2.setColor(new Color(120,120,120));
g2.fillRect(x+w/2-4,h/2-4, 2, 2);
g2.fillRect(x+w/2-1,h/2-4, 2, 2);
g2.fillRect(x+w/2+2,h/2-4, 2, 2);
g2.setColor(colorDark);
g2.fillRect(x+w/2-4,h/2-2, 2, 6);
g2.fillRect(x+w/2-1,h/2-2, 2, 6);
g2.fillRect(x+w/2+2,h/2-2, 2, 6);
g2.setColor(new Color(170,170,170));
g2.fillRect(x+w/2-4,h/2+2, 2, 2);
g2.fillRect(x+w/2-1,h/2+2, 2, 2);
g2.fillRect(x+w/2+2,h/2+2, 2, 2);
}
g2.setColor(black);
g2.drawRoundRect(x, y, w - 1, h - 1, 2,2);
g2.setColor(white);
g2.drawRoundRect(x + 1, y + 1, w - 3, h - 3, 2,2);
g2.setColor(black.darker());
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setFont(getFont());
g2.drawString(getText(), lx+gap, y+h/2+h/4);
}
}
}
- 1. Flex滑塊 - 如何添加兩個滑塊的相同控件
- 2. Dojo滑塊問題 - 如何添加滑塊數字
- 3. 如何在jqueryUI滑塊中添加PIPS滑塊
- 4. jQuery UI - 滑塊 - 如何添加值
- 5. 如何將滑塊添加到QMenu中?
- 6. 如何添加填充到滑塊?
- 7. jquery滑塊開始加速
- 8. 添加導航的滑塊
- 9. jssor滑塊添加視頻
- 10. 嵌套滑塊添加
- 11. 將DragScroll添加到滑塊
- 12. 添加蜱滑塊UI
- 13. jQuery UI滑塊 - 添加
- 14. 添加interakting滑塊在Magento
- 15. 在滑動時向UIPageViewController添加滑塊
- 16. 如何添加懸停效果,光滑的滑塊箭頭
- 17. 添加柔性活性類裏然後當滑塊前進
- 18. 如何將jquery ui滑塊添加到PrestaShop模塊表單
- 19. 如何將自動滑塊功能添加到我的jQuery滑塊?
- 20. 如何將範圍滑塊的功能添加到常規滑塊中
- 21. GIF滑塊flexslider,如何纔開始的GIF當滑塊
- 22. 撥動滑塊開/關在JavaFX的
- 23. 如何添加關聯到模塊?
- 24. 如何關閉滑動開關
- 25. 如何添加屬性爲HAML塊?
- 26. 如何將滑塊添加到滑塊而不會丟失滑塊分接行爲?
- 27. 將滑塊關聯到Float屬性
- 28. 如何添加疊加懸停在滑塊上?
- 29. 我如何從jquery mobile開啓和關閉滑塊輸入值
- 30. jQuery滑塊想要添加箭頭到我的滑塊
這樣的事情不會在STANDAR擺動庫中存在。嘗試搜索第三方庫或自己創建一個庫。我會使用JCheckBox。 iPhone控制器適用於小型觸摸屏,擺動型適用於帶鼠標的大型桌面屏幕。 –
@simpatico感謝分享+1 – mKorbel