【问题标题】:Increase Width of JSpinner Arrow Button增加 JSpinner 箭头按钮的宽度
【发布时间】:2023-04-06 01:49:01
【问题描述】:

我想增加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 上的宽度或箭头按钮?

【问题讨论】:

  • 请提供minimal reproducible example,以便我们还可以重现您的行为并了解您的代码中的问题。只需创建一个新的小型可运行类,它会演示您的问题并将其发布在此处。
  • 根据记忆,箭头是通过外观提供的,您需要研究提供自己的图像(可能)

标签: java swing


【解决方案1】:

我对几个LookAndFeels中的每一个都尝试了以下方法。

  • spinnder0
    • UIManager.put("Spinner.arrowButtonSize", new Dimension(130, 0));
  • spinner1
    • JButton#setPreferredSize(new Dimension(40, ...))
  • spinner2
    • JButton#setPreferredSize(new Dimension(50, ...))
    • JSpinner#setFont(spinner2.getFont().deriveFont(32f))
  • spinner3
    • JSpinner#setLayout(...)
    • int buttonsWidth = 100; // Math.max(nextD.width, previousD.width);

  • MetalLookAndFeel

  • NimbusLookAndFeel

  • WindowsLookAndFeel

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);
  }
}

【讨论】:

    猜你喜欢
    • 2019-05-03
    • 2011-06-18
    • 2012-10-23
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 2015-05-21
    • 2019-01-25
    • 1970-01-01
    相关资源
    最近更新 更多