如何反转 JSpinner 布局
How to invert JSpinner layout
我有一个 JSpinner:
Spinner
我希望 up/down 按钮位于左侧,文本也位于左侧。
有什么简单的方法可以做到这一点吗?谢谢
JSpinner spinner = new JSpinner();
您可以覆盖 BasicSpinnerUI 中的方法来创建下一个和上一个按钮:
自定义微调器UI:
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicSpinnerUI;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.BorderLayout;
public class MyJSpinnerUI extends BasicSpinnerUI{
public static ComponentUI createUI(JComponent c) {
return new MyJSpinnerUI();
}
@Override
protected Component createNextButton(){
// change arrow direction: up becomes right
Component c = createArrowButton(SwingConstants.EAST);
c.setName("Spinner.nextButton");
c.setSize(new Dimension(5, 5));
installNextButtonListeners(c);
return c;
}
@Override
protected Component createPreviousButton() {
// change arrow direction: down becomes left
Component c = createArrowButton(SwingConstants.WEST);
c.setName("Spinner.previousButton");
c.setSize(new Dimension(5, 5));
installPreviousButtonListeners(c);
return c;
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
c.removeAll();
c.setLayout(new BorderLayout());
// here you can play with their positioning
c.add(createPreviousButton(), BorderLayout.WEST);
c.add(createEditor(), BorderLayout.CENTER);
c.add(createNextButton(), BorderLayout.EAST);
}
// private method inside BasicSpinnerUI
private Component createArrowButton(int direction) {
JButton b = new BasicArrowButton(direction);
Border buttonBorder = UIManager.getBorder("Spinner.arrowButtonBorder");
if (buttonBorder instanceof UIResource) {
// Wrap the border to avoid having the UIResource be replaced by
// the ButtonUI. This is the opposite of using BorderUIResource.
b.setBorder(new CompoundBorder(buttonBorder, null));
} else {
b.setBorder(buttonBorder);
}
b.setInheritsPopupMenu(true);
return b;
}
}
演示:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import java.awt.Dimension;
import java.awt.FlowLayout;
public class JSpinnerTest{
public static void main(String[] args) {
JFrame frame = new JFrame("JSpinner changed arrows");
JPanel panel = new JPanel();
JSpinner jSpinner = new JSpinner();
// apply your custom Spinner UI
jSpinner.setUI(new MyJSpinnerUI());
jSpinner.setPreferredSize(new Dimension(100, 40));
panel.setLayout(new FlowLayout());
panel.add(jSpinner);
frame.add(panel);
frame.setVisible(true);
frame.setSize(1000, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
我有一个 JSpinner:
Spinner
我希望 up/down 按钮位于左侧,文本也位于左侧。
有什么简单的方法可以做到这一点吗?谢谢
JSpinner spinner = new JSpinner();
您可以覆盖 BasicSpinnerUI 中的方法来创建下一个和上一个按钮:
自定义微调器UI:
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicSpinnerUI;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.BorderLayout;
public class MyJSpinnerUI extends BasicSpinnerUI{
public static ComponentUI createUI(JComponent c) {
return new MyJSpinnerUI();
}
@Override
protected Component createNextButton(){
// change arrow direction: up becomes right
Component c = createArrowButton(SwingConstants.EAST);
c.setName("Spinner.nextButton");
c.setSize(new Dimension(5, 5));
installNextButtonListeners(c);
return c;
}
@Override
protected Component createPreviousButton() {
// change arrow direction: down becomes left
Component c = createArrowButton(SwingConstants.WEST);
c.setName("Spinner.previousButton");
c.setSize(new Dimension(5, 5));
installPreviousButtonListeners(c);
return c;
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
c.removeAll();
c.setLayout(new BorderLayout());
// here you can play with their positioning
c.add(createPreviousButton(), BorderLayout.WEST);
c.add(createEditor(), BorderLayout.CENTER);
c.add(createNextButton(), BorderLayout.EAST);
}
// private method inside BasicSpinnerUI
private Component createArrowButton(int direction) {
JButton b = new BasicArrowButton(direction);
Border buttonBorder = UIManager.getBorder("Spinner.arrowButtonBorder");
if (buttonBorder instanceof UIResource) {
// Wrap the border to avoid having the UIResource be replaced by
// the ButtonUI. This is the opposite of using BorderUIResource.
b.setBorder(new CompoundBorder(buttonBorder, null));
} else {
b.setBorder(buttonBorder);
}
b.setInheritsPopupMenu(true);
return b;
}
}
演示:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import java.awt.Dimension;
import java.awt.FlowLayout;
public class JSpinnerTest{
public static void main(String[] args) {
JFrame frame = new JFrame("JSpinner changed arrows");
JPanel panel = new JPanel();
JSpinner jSpinner = new JSpinner();
// apply your custom Spinner UI
jSpinner.setUI(new MyJSpinnerUI());
jSpinner.setPreferredSize(new Dimension(100, 40));
panel.setLayout(new FlowLayout());
panel.add(jSpinner);
frame.add(panel);
frame.setVisible(true);
frame.setSize(1000, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}