Java 增加JSpinner箭头按钮的宽度
我想增加Java 增加JSpinner箭头按钮的宽度,java,swing,Java,Swing,我想增加JSpinner的宽度。我在网上查了一下,找到了密码。我应用了它,但宽度没有增加。下面是代码 jsHR = new javax.swing.JSpinner(); Component c = jsHR.getComponent(0); if (c instanceof BasicArrowButton) { JButton b = (JButton) c; //b.setBounds(190, 2, 0, 0); b.setPref
JSpinner
的宽度。我在网上查了一下,找到了密码。我应用了它,但宽度没有增加。下面是代码
jsHR = new javax.swing.JSpinner();
Component c = jsHR.getComponent(0);
if (c instanceof BasicArrowButton) {
JButton b = (JButton) c;
//b.setBounds(190, 2, 0, 0);
b.setPreferredSize(new Dimension(100, 100));
b.setBorder(new LineBorder(Color.RED));
}
jpTimePanel.add(jsHR);
jpTimePanel
具有网格布局
我做错了什么?如何增加
JSpinner
上的宽度或箭头按钮?我对几个LookAndFeel
中的每一个都尝试了以下方法
Spinder0
UIManager.put(“Spinner.arrowButtonSize”,新维度(130,0))代码>
喷丝头1
JButton#设置首选尺寸(新尺寸(40,…)
喷丝头2
JButton#设置首选尺寸(新尺寸(50,…)
JSpinner#setFont(spinner2.getFont().deriveFont(32f))
喷丝头3
JSpinner#setLayout(…)
int按钮宽度=100;//最大值(下一个宽度,上一个宽度)代码>
MetalLookAndFeel
NimbusLookAndFeel
WindowsLookAndFeel
import java.awt.*;
导入java.util.stream.stream;
导入javax.swing.*;
导入javax.swing.plaf.basic.BasicSpinnerUI;
公共最终类spinnerRowButtonSizeTest{
私有组件makeUI(){
喷丝头型号=新喷丝头型号(5,0,10,1);
JSpinner喷丝头0=新JSpinner(型号);
JSpinner喷丝头1=新JSpinner(型号);
流(喷丝头1)
.filter(JButton.class::isInstance).map(JButton.class::cast)
.forEach(b->{
维度d=b.getPreferredSize();
d、 宽度=40;
b、 设置首选大小(d);
});
JSpinner喷丝头2=新JSpinner(型号);
spinner2.setFont(spinner2.getFont().deriveFont(32f));
流(喷丝头2)
.filter(JButton.class::isInstance).map(JButton.class::cast)
.forEach(b->{
维度d=b.getPreferredSize();
d、 宽度=50;
b、 设置首选大小(d);
});
JSpinner喷丝头3=新JSpinner(型号){
@覆盖公共void setLayout(布局管理器管理器){
super.setLayout(新喷丝头布局());
}
};
Box=Box.createVerticalBox();
添加(喷丝头0);
框。添加(框。创建垂直支柱(10));
添加(喷丝头1);
框。添加(框。创建垂直支柱(10));
添加(喷丝头2);
框。添加(框。创建垂直支柱(10));
添加(喷丝头3);
JPanel p=newjpanel(newborderlayout());
p、 添加(框,边框布局。北);
p、 setBorder(BorderFactory.createEmptyByOrder(10,10,10,10));
返回p;
}
私有静态流(容器父级){
返回Stream.of(parent.getComponents())
.filter(Container.class::isInstance).map(c->stream(Container.class.cast(c)))
.reduce(Stream.of(parent),Stream::concat);
}
公共静态void main(字符串[]args){
EventQueue.invokeLater(()->{
试一试{
UIManager.setLookAndFeel(“javax.swing.plaf.nimbus.NimbusLookAndFeel”);
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化异常
|IllegalAccessException |不受支持的LookandFeelException ex){
例如printStackTrace();
}
UIManager.put(“微调器.箭头按钮化”,新尺寸(130,0));
//UIManager.put(“微调器.箭头按钮集”,新插图(1,1,1,1));
put(“Spinner.disableOnBoundaryValues”,Boolean.TRUE);
JFrame f=新的JFrame();
f、 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f、 getContentPane().add(新的spinnerRowButtonSizeTest().makeUI());
f、 设置大小(320240);
f、 setLocationRelativeTo(空);
f、 setVisible(真);
});
}
}
//@see javax/swing/plaf/basic/basicpinnerui.java
类SpinnerLayout实现LayoutManager{
私有组件nextButton=null;
私有组件previousButton=null;
私有组件编辑器=null;
@重写公共void addLayoutComponent(字符串名称,组件c){
如果(“下一步”。等于(名称)){
nextButton=c;
}else if(“Previous”.equals(name)){
previousButton=c;
}else if(“Editor”.equals(name)){
编辑器=c;
}
}
@覆盖公共void removeLayoutComponent(组件c){
if(c==nextButton){
nextButton=null;
}else if(c==上一个按钮){
previousButton=null;
}else if(c==编辑器){
编辑器=null;
}
}
专用维度preferredSize(组件c){
返回值(c==null)?新维度():c.getPreferredSize();
}
@替代公共维度preferredLayoutSize(容器父级){
尺寸nextD=首选尺寸(nextButton);
维度previousD=preferredSize(previousButton);
维度编辑器=首选大小(编辑器);
/*强制编辑器高度为2的倍数
*/
编辑命令高度=((编辑命令高度+1)/2)*2;
尺寸尺寸=新尺寸(编辑字宽度、编辑字高度);
size.width+=数学最大值(下一个宽度,上一个宽度);
Insets Insets=parent.getInsets();
size.width+=插图左+插图右;
尺寸.高度+=插图.顶部+插图.底部;
返回大小;
}
@替代公共维度minimumLayoutSize(容器父级){
返回preferredLayoutSize(父级);
}
专用空心立根(构件c、int x、int y、int宽度、int高度){
如果(c!=null){
c、 立根(x、y、宽度、高度);
}
}
@覆盖公共无效布局容器(容器父级){
int width=parent.getWidth();
int height=parent.getHeight();
Insets Insets=parent.getInsets();
if(nextButton==null&&previousButton==null){
收进边界(编辑器,insets.left,insets.top,width-insets.left-insets.right,
高度-插图。顶部-插图。底部);
返回;
}
迪曼西奥
import java.awt.*;
import java.util.stream.Stream;
import javax.swing.*;
import javax.swing.plaf.basic.BasicSpinnerUI;
public final class SpinnerArrowButtonSizeTest {
private Component makeUI() {
SpinnerModel model = new SpinnerNumberModel(5, 0, 10, 1);
JSpinner spinner0 = new JSpinner(model);
JSpinner spinner1 = new JSpinner(model);
stream(spinner1)
.filter(JButton.class::isInstance).map(JButton.class::cast)
.forEach(b -> {
Dimension d = b.getPreferredSize();
d.width = 40;
b.setPreferredSize(d);
});
JSpinner spinner2 = new JSpinner(model);
spinner2.setFont(spinner2.getFont().deriveFont(32f));
stream(spinner2)
.filter(JButton.class::isInstance).map(JButton.class::cast)
.forEach(b -> {
Dimension d = b.getPreferredSize();
d.width = 50;
b.setPreferredSize(d);
});
JSpinner spinner3 = new JSpinner(model) {
@Override public void setLayout(LayoutManager mgr) {
super.setLayout(new SpinnerLayout());
}
};
Box box = Box.createVerticalBox();
box.add(spinner0);
box.add(Box.createVerticalStrut(10));
box.add(spinner1);
box.add(Box.createVerticalStrut(10));
box.add(spinner2);
box.add(Box.createVerticalStrut(10));
box.add(spinner3);
JPanel p = new JPanel(new BorderLayout());
p.add(box, BorderLayout.NORTH);
p.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
return p;
}
private static Stream<Component> stream(Container parent) {
return Stream.of(parent.getComponents())
.filter(Container.class::isInstance).map(c -> stream(Container.class.cast(c)))
.reduce(Stream.of(parent), Stream::concat);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
UIManager.put("Spinner.arrowButtonSize", new Dimension(130, 0));
// UIManager.put("Spinner.arrowButtonInsets", new Insets(1, 1, 1, 1));
UIManager.put("Spinner.disableOnBoundaryValues", Boolean.TRUE);
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new SpinnerArrowButtonSizeTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
// @see javax/swing/plaf/basic/BasicSpinnerUI.java
class SpinnerLayout implements LayoutManager {
private Component nextButton = null;
private Component previousButton = null;
private Component editor = null;
@Override public void addLayoutComponent(String name, Component c) {
if ("Next".equals(name)) {
nextButton = c;
} else if ("Previous".equals(name)) {
previousButton = c;
} else if ("Editor".equals(name)) {
editor = c;
}
}
@Override public void removeLayoutComponent(Component c) {
if (c == nextButton) {
nextButton = null;
} else if (c == previousButton) {
previousButton = null;
} else if (c == editor) {
editor = null;
}
}
private Dimension preferredSize(Component c) {
return (c == null) ? new Dimension() : c.getPreferredSize();
}
@Override public Dimension preferredLayoutSize(Container parent) {
Dimension nextD = preferredSize(nextButton);
Dimension previousD = preferredSize(previousButton);
Dimension editorD = preferredSize(editor);
/* Force the editors height to be a multiple of 2
*/
editorD.height = ((editorD.height + 1) / 2) * 2;
Dimension size = new Dimension(editorD.width, editorD.height);
size.width += Math.max(nextD.width, previousD.width);
Insets insets = parent.getInsets();
size.width += insets.left + insets.right;
size.height += insets.top + insets.bottom;
return size;
}
@Override public Dimension minimumLayoutSize(Container parent) {
return preferredLayoutSize(parent);
}
private void setBounds(Component c, int x, int y, int width, int height) {
if (c != null) {
c.setBounds(x, y, width, height);
}
}
@Override public void layoutContainer(Container parent) {
int width = parent.getWidth();
int height = parent.getHeight();
Insets insets = parent.getInsets();
if (nextButton == null && previousButton == null) {
setBounds(editor, insets.left, insets.top, width - insets.left - insets.right,
height - insets.top - insets.bottom);
return;
}
Dimension nextD = preferredSize(nextButton);
Dimension previousD = preferredSize(previousButton);
int buttonsWidth = 100; // Math.max(nextD.width, previousD.width);
int editorHeight = height - (insets.top + insets.bottom);
// The arrowButtonInsets value is used instead of the JSpinner's
// insets if not null. Defining this to be (0, 0, 0, 0) causes the
// buttons to be aligned with the outer edge of the spinner's
// border, and leaving it as "null" places the buttons completely
// inside the spinner's border.
Insets buttonInsets = UIManager.getInsets("Spinner.arrowButtonInsets");
if (buttonInsets == null) {
buttonInsets = insets;
}
/* Deal with the spinner's componentOrientation property.
*/
int editorX, editorWidth, buttonsX;
if (parent.getComponentOrientation().isLeftToRight()) {
editorX = insets.left;
editorWidth = width - insets.left - buttonsWidth - buttonInsets.right;
buttonsX = width - buttonsWidth - buttonInsets.right;
} else {
buttonsX = buttonInsets.left;
editorX = buttonsX + buttonsWidth;
editorWidth = width - buttonInsets.left - buttonsWidth - insets.right;
}
int nextY = buttonInsets.top;
int nextHeight = (height / 2) + (height % 2) - nextY;
int previousY = buttonInsets.top + nextHeight;
int previousHeight = height - previousY - buttonInsets.bottom;
setBounds(editor, editorX, insets.top, editorWidth, editorHeight);
setBounds(nextButton, buttonsX, nextY, buttonsWidth, nextHeight);
setBounds(previousButton, buttonsX, previousY, buttonsWidth, previousHeight);
}
}