带有最小和最大按钮的 JSpinner

发布于 2024-11-27 09:02:55 字数 146 浏览 0 评论 0原文

我正在寻找一个类似 JSpinner 的组件,它提供内置按钮,可以立即将微调器值设置为 jspinner 模型的最小值或最大值。在我自己实现这个之前,我认为以前可能有人做过,尽管我在该主题上的谷歌搜索没有成功。

有这样的组件吗,还是我应该自己编码?感谢您的帮助。

I'm looking for a JSpinner-like component that would provide built-in buttons that would immediately set the spinner value to the minimum or the maximum of the jspinner model. Before implementing this myself, I thought that somebody might have done it before, though my google searches on the topic were not successful.

Is there such a component out there, or should I code it myself ? Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愛上了 2024-12-04 09:02:55

我想不出有任何组件可以开箱即用。但基本上,一个带有减号按钮、微调器和最大按钮的简单流程布局就可以了。这两个按钮都有 actionListeners ,它将向微调器询问最小值和最大值并设置其值。

在微调器上使用changeListener,以编程方式(使用按钮)和通过用户交互来通知值更改。

此外,如果您的值是离散的,您可以考虑使用 JCombobox,如果不是,则可以考虑使用 JSlider,因为这两个组件都可以更清楚地向用户呈现上限和下限。

问候,
史蒂芬

I can't think of any component that would do it out of the box. But basically, a simple flow layout with a minus button, a spinner and a max button would do. Both buttons would have actionListeners that will ask the spinner for min and max values and set its value.

Use a changeListener on the spinner to be notified of value changes both programatically (using your buttons) and through user interaction.

Also, you could consider using a JCombobox if your values are discrete or a JSlider if they are not, as both components present upper and lower bounds more clearly to users.

Regards,
Stéphane

岁月流歌 2024-12-04 09:02:55

我认为自己实现它会很简单

1)我将创建一个扩展 JPanel 的类。

class MyPanel extends JPanel

2) 在此类中,您必须定义滑块和按钮的放置位置以及与按钮相关的操作。

public MyPanel(){
  super();
  // set the layout
  JSlider slider = new Slider();
  this.add(slider);
  // ..
  JButton button1 = new JButton();
  //
}

3) 您可以将您已实现的类的 JPanel 实例添加到您的应用程序中。

I think it would be simple to implemt it by yourself

1) I would create a class that extend JPanel.

class MyPanel extends JPanel

2) In this class you have to define where your slider and buttons have to be placed and the actions correlated to the button.

public MyPanel(){
  super();
  // set the layout
  JSlider slider = new Slider();
  this.add(slider);
  // ..
  JButton button1 = new JButton();
  //
}

3) You can add to your application JPanel instances of the class the you have implemented.

把时间冻结 2024-12-04 09:02:55

您必须阅读 如何使用 Spinners ,然后您可以能够为 SpinnerNumberModel

JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 20, 1));

例如(作者:StanislavL)

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.text.*;

public class TestDigitsOnlySpinner {

    public static void main(String... args) {
        SwingUtilities.invokeLater((Runnable) new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("enter digit");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JSpinner jspinner = makeDigitsOnlySpinnerUsingDocumentFilter();
                frame.getContentPane().add(jspinner, BorderLayout.CENTER);
                frame.getContentPane().add(new JButton("just another widget"), BorderLayout.SOUTH);
                frame.pack();
                frame.setVisible(true);
            }

            private JSpinner makeDigitsOnlySpinner_BasicAttempt() {
                JSpinner spinner = new JSpinner(new SpinnerNumberModel());
                return spinner;
            }

            private JSpinner makeDigitsOnlySpinnerUsingDocumentFilter() {
                JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 20, 1));
                JSpinner.NumberEditor jsEditor = (JSpinner.NumberEditor) spinner.getEditor();
                final Document jsDoc = jsEditor.getTextField().getDocument();
                if (jsDoc instanceof PlainDocument) {
                    AbstractDocument doc = new PlainDocument() {

                        private static final long serialVersionUID = 1L;

                        @Override
                        public void setDocumentFilter(DocumentFilter filter) {
                            if (filter instanceof MyDocumentFilter) {
                                super.setDocumentFilter(filter);
                            }
                        }
                    };
                    doc.setDocumentFilter(new MyDocumentFilter());
                    jsEditor.getTextField().setDocument(doc);
                }
                return spinner;
            }
        });
    }

    private static class MyDocumentFilter extends DocumentFilter {

        @Override
        public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
            if (stringContainsOnlyDigits(string)) {
                super.insertString(fb, offset, string, attr);
            }
        }

        @Override
        public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
            super.remove(fb, offset, length);
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            if (stringContainsOnlyDigits(text)) {
                super.replace(fb, offset, length, text, attrs);
            }
        }

        private boolean stringContainsOnlyDigits(String text) {
            for (int i = 0; i < text.length(); i++) {
                if (!Character.isDigit(text.charAt(i))) {
                    return false;
                }
            }
            return true;
        }
    }

    private TestDigitsOnlySpinner() {
    }
}

you have to read How to Use Spinners , and then you can be able set these value exactly for SpinnerNumberModel

JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 20, 1));

for example (by StanislavL)

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.text.*;

public class TestDigitsOnlySpinner {

    public static void main(String... args) {
        SwingUtilities.invokeLater((Runnable) new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("enter digit");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JSpinner jspinner = makeDigitsOnlySpinnerUsingDocumentFilter();
                frame.getContentPane().add(jspinner, BorderLayout.CENTER);
                frame.getContentPane().add(new JButton("just another widget"), BorderLayout.SOUTH);
                frame.pack();
                frame.setVisible(true);
            }

            private JSpinner makeDigitsOnlySpinner_BasicAttempt() {
                JSpinner spinner = new JSpinner(new SpinnerNumberModel());
                return spinner;
            }

            private JSpinner makeDigitsOnlySpinnerUsingDocumentFilter() {
                JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 20, 1));
                JSpinner.NumberEditor jsEditor = (JSpinner.NumberEditor) spinner.getEditor();
                final Document jsDoc = jsEditor.getTextField().getDocument();
                if (jsDoc instanceof PlainDocument) {
                    AbstractDocument doc = new PlainDocument() {

                        private static final long serialVersionUID = 1L;

                        @Override
                        public void setDocumentFilter(DocumentFilter filter) {
                            if (filter instanceof MyDocumentFilter) {
                                super.setDocumentFilter(filter);
                            }
                        }
                    };
                    doc.setDocumentFilter(new MyDocumentFilter());
                    jsEditor.getTextField().setDocument(doc);
                }
                return spinner;
            }
        });
    }

    private static class MyDocumentFilter extends DocumentFilter {

        @Override
        public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
            if (stringContainsOnlyDigits(string)) {
                super.insertString(fb, offset, string, attr);
            }
        }

        @Override
        public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
            super.remove(fb, offset, length);
        }

        @Override
        public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            if (stringContainsOnlyDigits(text)) {
                super.replace(fb, offset, length, text, attrs);
            }
        }

        private boolean stringContainsOnlyDigits(String text) {
            for (int i = 0; i < text.length(); i++) {
                if (!Character.isDigit(text.charAt(i))) {
                    return false;
                }
            }
            return true;
        }
    }

    private TestDigitsOnlySpinner() {
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文