- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
向 gr8 StackOverflow 社区的所有人致以问候。这是我自己尝试解决后的第一个问题,因此有点长...:-)
我在使用向下箭头键获取 JSpinner
的最小指定值时遇到问题。如果在文本字段中键入,它会接受该值。其他一切正常。奇怪的是,这仅发生在 Spinner 模型的某些组合中,特别是 new SpinnerNumberModel(29.6, 29.6, 118.1, 0.1)
和 new SpinnerNumberModel(2.00, 2.00, 6.00, 0.01)
。下面是有这个问题的 2 个 JSpinners
的代码。我不确定为什么这些模型会产生问题,以及是否有更多模型存在此问题。
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
public class JSpinnerBug {
private JFrame frame;
private JSpinner spinner;
private JSpinner spinner_1;
private JButton btnChangeValue_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JSpinnerBug window = new JSpinnerBug();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public JSpinnerBug() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{119, 88, 105, 0};
gridBagLayout.rowHeights = new int[]{25, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
JButton btnChangeValue = new JButton("Change Value");
btnChangeValue.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
spinner.setValue(2.00);
}
});
spinner = new JSpinner();
spinner.setModel(new SpinnerNumberModel(2.01, 2.00, 6.00, 0.01));
GridBagConstraints gbc_spinner = new GridBagConstraints();
gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
gbc_spinner.insets = new Insets(0, 0, 5, 5);
gbc_spinner.gridx = 1;
gbc_spinner.gridy = 0;
frame.getContentPane().add(spinner, gbc_spinner);
GridBagConstraints gbc_btnChangeValue = new GridBagConstraints();
gbc_btnChangeValue.insets = new Insets(0, 0, 5, 0);
gbc_btnChangeValue.anchor = GridBagConstraints.NORTHWEST;
gbc_btnChangeValue.gridx = 2;
gbc_btnChangeValue.gridy = 0;
frame.getContentPane().add(btnChangeValue, gbc_btnChangeValue);
spinner_1 = new JSpinner();
spinner_1.setModel(new SpinnerNumberModel(29.7, 29.6, 118.1, 0.1));
GridBagConstraints gbc_spinner_1 = new GridBagConstraints();
gbc_spinner_1.fill = GridBagConstraints.HORIZONTAL;
gbc_spinner_1.insets = new Insets(0, 0, 0, 5);
gbc_spinner_1.gridx = 1;
gbc_spinner_1.gridy = 1;
frame.getContentPane().add(spinner_1, gbc_spinner_1);
btnChangeValue_1 = new JButton("Change Value");
btnChangeValue_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
spinner_1.setValue(29.6);
}
});
GridBagConstraints gbc_btnChangeValue_1 = new GridBagConstraints();
gbc_btnChangeValue_1.gridx = 2;
gbc_btnChangeValue_1.gridy = 1;
frame.getContentPane().add(btnChangeValue_1, gbc_btnChangeValue_1);
}
/**
* Custom Spinner to detect button click on
* JSpinner. This class fixes a bug for
* JSpinner like for example for minimum value = 2.00
* and step size 0.01
*
* @author dARKpRINCE
*
*/
class CustomSpinner extends JSpinner{
@Override
public Object getPreviousValue() {
// Circumvent JSpinner bug
NumberEditor spinnerEditor = (NumberEditor) this.getEditor();
SpinnerNumberModel model = spinnerEditor.getModel();
if(model.getMinimum().equals(2.00) && model.getStepSize().equals(0.01)){
if ((Double) getValue() == 2.01) {
return 2.00;
}
else {
return super.getPreviousValue();
}
}
else if(model.getMinimum().equals(29.6) && model.getStepSize().equals(0.1)){
if ((Double) getValue() == 29.7) {
return 29.6;
}
else {
return super.getPreviousValue();
}
}
else {
return super.getPreviousValue();
}
}
}
}
请随意尝试其他模型,例如 new SpinnerNumberModel(55.3, 55.3, 118.1, 0.1)
。这样就可以了。
我的解决方案是扩展 JSpinner(在代码示例末尾给出)并关注产生此问题的每个模型,这不是最优的。因此,每个 JSpinner
都被替换为 CustomSpinner
。
我还检查了 JSpinner 的源代码,但无法确定/修复问题。谁能告诉我这是否是一个错误以及如何以最佳方式修复它?为 JSpinner 使用第三方库是我最后的选择。
最佳答案
BigDecimal#compareTo(...)
Double#compareTo(...)
输出:
import java.awt.*;
import java.math.*;
import javax.swing.*;
public class JSpinnerBug2 {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JSpinnerBug2().makeUI());
frame.setSize(320, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public JComponent makeUI() {
Box box = Box.createVerticalBox();
box.add(new JSpinner(new SpinnerNumberModel(2.01, 2.00, 6.00, 0.01)));
box.add(Box.createVerticalStrut(10));
box.add(new JSpinner(new SpinnerNumberModel(29.7f, 29.6f, 111.8f, 0.1f)));
box.add(Box.createVerticalStrut(10));
box.add(new JSeparator());
//TEST:
double d = 29.7 - 29.6 - 0.1;
System.out.format("(%f-%f-%f>=0): %b%n", 29.7, 29.6, 0.1, d>=0);
System.out.format("(abs(%f-%f-%f)<1.0e-14): %b%n", 29.7, 29.6, 0.1, Math.abs(d)<1.0e-14);
System.out.format("(abs(%f-%f-%f)<1.0e-15): %b%n", 29.7, 29.6, 0.1, Math.abs(d)<1.0e-15);
box.add(new JSpinner(new SpinnerNumberModel(2.01f, 2.00f, 6.00f, 0.01f)));
box.add(Box.createVerticalStrut(10));
box.add(new JSpinner(new SpinnerNumberModel(29.7, 29.6, 111.8, 0.1) {
@Override public Object getPreviousValue() {
Number v = getNumber();
BigDecimal value = new BigDecimal(v.toString());
BigDecimal stepSize = new BigDecimal(getStepSize().toString());
BigDecimal maximum = new BigDecimal(getMaximum().toString());
BigDecimal minimum = new BigDecimal(getMinimum().toString());
BigDecimal newValue;
if (v instanceof Double) {
newValue = value.subtract(stepSize);
} else {
return super.getPreviousValue();
}
if ((maximum != null) && (maximum.compareTo(newValue) < 0)) {
return null;
}
if ((minimum != null) && (minimum.compareTo(newValue) > 0)) {
return null;
} else {
return newValue;
}
}
}));
box.add(Box.createVerticalGlue());
box.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
return box;
}
}
关于java - JSpinner 在按下向下箭头时不显示最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21158043/
我正在使用面部跟踪进行 HCI(人机交互)。我正在尝试使用面部控制 PC。 我有 3x3 二维网格按钮。1 2 34 5 67 8 9 假设,当前焦点在按钮 5 上。如果我按向上箭头,则焦点将在 2
我正在为蛇和梯子制作一 block 板,到目前为止,我已经按降序打印了板。但是,我需要以正确的方式打印电路板。 编辑“螺旋下降”意味着 100...91 81...90 80...71 ...
我有一个可以响应式调整大小的菜单,因此每次调整大小时 div (#menuWFhover) 的内容都会重新排列,因此 div 根据窗口大小具有不同的高度。 当我使用 jQuery slideDown/
我们从服务器获取数据并附加 ListView 。我们的问题是,当向上/向下滚动时,它首先显示白屏,然后显示数据。向下/向上滚动时,之前出现的白屏应删除 for (var i=0; i");
我想要一个汉堡图标动态改变颜色的代码,以适应网站的黑色部分/部分和白色。它最初是 3 段白色,js 代码几乎是好的和正确的,但是当它在白色部分时它一直闪烁,并在红色和白色之间闪烁。 js var to
我对点击事件的页脚位置有疑问。我正在使用 bootstrap css,我有一个可折叠元素(bootstrap 中的 Accordion ),当它折叠时它有一个名为 .accordion-toggle.
在此代码段中,使用关键帧和动画并显示无/ block ,div 动画以在悬停时向下滑动。 h1 { padding: 20px; } div { width: 100%; background
我需要对一个 float 进行四舍五入。例如 4.00011 。内置函数 round() 总是在数字 > .5 时向上舍入,在 = 0 val *= 10 ** precision r
我正在尝试就地缩小文件。 我正在用另一个文件的内容替换一个文件的内容,完成后我想确保源文件是否小于目标文件,目标文件是否正确收缩。 (为什么:因为dest文件是一个备份,写入media的开销很大,所以
似乎每当我用一个负整数除以一个正整数时,我都需要它向下舍入 (向 -inf),而不是向 0。但 C# 和 C++ 都向 0 舍入。 所以我想我需要一个 DivideDownward() 方法。我可以用
考虑这个简单的代码: document.addEventListener( 'keypress', function() { console.log( 'press' ); } ); document
有什么方法可以检查 Azure 资源(例如 Azure IoT 中心或事件中心)是否可用。我所说的可用是指它是否已关闭/工作/正常/启动?我是 Azure 的新手,如果有人能提供一些启发,那就太好了。
有什么方法可以检查 Azure 资源(例如 Azure IoT 中心或事件中心)是否可用。我所说的可用是指它是否已关闭/工作/正常/启动?我是 Azure 的新手,如果有人能提供一些启发,那就太好了。
我见过几个recyclerview的无限滚动示例,但它们都是向下滚动的。 我想要的是一次又一次地加载我的列表,但无论用户是从上面还是从下面滚动。 正常列表 向上滚动 向下滚动 这样可以通过无限滚动一遍
我正在使用带有 View 的 drupal 7,并且我正在使用我发现的向上/向下文本幻灯片的 jquery 脚本。它有效,但是当我尝试将它与带有 ajax 的 View 公开过滤器一起使用时,它似乎不
*场景 我希望制作一个类似于本网站上的 jquery 效果,它位于主 Flash 添加的右侧: http://www.commbank.com.au/ *问题 我已经开始了,但是用很多方法遇到了一些障
互联网。如果这与其他人没有什么关系,请原谅我,但我会将其留在这里,以防这是一个有效的问题。 我正在尝试创建一个文本区域字段,其中用户每次按下键(a-z),都会触发背景颜色更改(在数组中列出)。我一直在
我正在创建一个网络前端来控制一个小型机器人。 Ajax 调用将在 keydown 上进行,以启动机器人,并在 keyup 上进行停止。 我的问题是,当按下某个键时,keyup、keydown 和 ke
我在内容容器中有两个 div,一个向左浮动,另一个向右浮动。我正在使用屏幕的整个宽度。左 div 宽度为 1290px,右 div 宽度为 625px。有时,在加载页面时,滚动条会更改可用屏幕宽度的宽
请看这个UI sketch图片,我在某个站点的侧边栏(黑框)中有这个 div,当我向下滚动或向上滚动时,我不希望它隐藏...我希望它在我向下滚动和移动时自行向下移动当我向上滚动时向上滚动,这样它就永远
我是一名优秀的程序员,十分优秀!