gpt4 book ai didi

java - 如何将值从 jtextfield 复制到 jspinner

转载 作者:行者123 更新时间:2023-12-01 12:58:14 27 4
gpt4 key购买 nike

当我想要将一些十六进制值插入 JtextField 并想要继续递增或递减该值时,我遇到问题抛出 JSpinner。我的问题是我的自定义 JSPinner 总是从 0 开始递增值,并且我想从精确位置递增,用户将在 JTextfield 中设置该位置

package opa.beta1;

import com.sun.xml.internal.ws.util.StringUtils;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.math.BigInteger;
import java.text.ParseException;
import javax.swing.AbstractSpinnerModel;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.DefaultFormatter;
import javax.swing.text.DefaultFormatterFactory;

public class TextFieldEvent {

JTextField arrayIVnew[] = new JTextField[8];
JPanel panel1 = new JPanel();

public void rewriteJSpinner(JSpinner jSpinner, JTextField array[], final int u) {
Long tmp;
tmp = new Long(array[u].getText());

if (0 <= tmp && tmp <= 0xff && jSpinner.getValue() != tmp) {
jSpinner.setValue(tmp);
}
}

public String textXORing(JTextField array1[], JTextField array2[], int i) {

String window1 = array1[i].getText();
String window2 = array2[i].getText();
if (window1.equals("")) {
return array2[i].getText();
}
BigInteger pom1 = new BigInteger(window1, 16);
if (window2.equals("")) {
return array1[i].getText();
}
BigInteger pom2 = new BigInteger(window2, 16);
BigInteger res = pom1.xor(pom2);
String s = res.toString(16);
return s;
}

public void createJSPinnerTextField(final JTextField array[], JPanel container, String s, int width,
boolean docListener, boolean edit) {

for (int i = 0; i < 8; i++) {
array[i] = new JTextField(width);
array[i].setText(s);
final int u = i;

final JSpinner jSpinner = new JSpinner(new TextFieldEvent.LongNumberModel(0x00L, 0x00L, 0xFFL, 1L));

//jspinnerarray[i]= jSpinner;
JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) jSpinner.getEditor();
JFormattedTextField tf = editor.getTextField();
tf.setFormatterFactory(new TextFieldEvent.MyFormatterFactory());

Dimension d = jSpinner.getPreferredSize();
d.setSize(16, 20);
jSpinner.setPreferredSize(d);

jSpinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent ce) {
int tmp = 0;
tmp = Integer.parseInt(jSpinner.getValue().toString());
array[u].setText(String.valueOf(Integer.toHexString(tmp)).toUpperCase());
tmp++;
}
});

container.add(jSpinner);

if (edit) {
array[i].setEditable(false);
}
if (docListener) {
array[i].getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent de) {

rewriteJSpinner(jSpinner, array, u);
}

@Override
public void removeUpdate(DocumentEvent de) {

rewriteJSpinner(jSpinner, array, u);
}

@Override
public void changedUpdate(DocumentEvent de) {
rewriteJSpinner(jSpinner, array, u);
}
});
}
container.add(array[i]);
}
}

private static class LongNumberModel extends AbstractSpinnerModel {

private Long value, stepSize;
private Comparable<Long> minimum, maximum;

public LongNumberModel(Long value, Long minimum, Long maximum, Long stepSize) {
this.value = value;
this.minimum = minimum;
this.maximum = maximum;
this.stepSize = stepSize;
}

@Override
public Object getValue() {
return value;
}

@Override
public void setValue(Object value) {
this.value = (Long) value;
fireStateChanged();
}

@Override
public Object getNextValue() {
long v = value.longValue() + stepSize.longValue();
return bounded(v);
}

@Override
public Object getPreviousValue() {
long v = value.longValue() - stepSize.longValue();
return bounded(v);
}

private Object bounded(long v) {
if ((maximum != null) && (maximum.compareTo(v) < 0)) {
return null;
}
if ((minimum != null) && (minimum.compareTo(v) > 0)) {
return null;
}
return Long.valueOf(v);
}
}

private static class MyFormatterFactory extends DefaultFormatterFactory {

@Override
public JFormattedTextField.AbstractFormatter getDefaultFormatter() {
return new TextFieldEvent.HexFormatter();
}
}

private static class HexFormatter extends DefaultFormatter {

@Override
public Object stringToValue(String text) throws ParseException {
try {
return Long.valueOf(text, 16);
} catch (NumberFormatException nfe) {
throw new ParseException(text, 0);
}
}

@Override
public String valueToString(Object value) throws ParseException {
return Long.toHexString(
((Long) value).intValue()).toUpperCase();
}
}

public void addComponentsToPanel(Container panel) {

panel.setLayout(null);
JLabel label1 = new JLabel("Inicialization vector:");
panel1.add(label1);
createJSPinnerTextField(arrayIVnew, panel1, "11", 2, false, false);
panel.add(panel1);
Insets insets = panel.getInsets();
Dimension size = panel1.getPreferredSize();
panel1.setBounds(10 + insets.left, 485 + insets.top, size.width, size.height);
}

TextFieldEvent() {
JFrame frame = new JFrame("Oracle Padding Attack");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPanel(frame.getContentPane());
Insets insets = frame.getInsets();
frame.setSize(800 + insets.left + insets.right, 650 + insets.top + insets.bottom);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TextFieldEvent();
}
});

}
}

最佳答案

您正在从 ChangeListener 中更改 JSpinner 的值 - 这将调用 ChangeListener,然后更改 JSpinner 的值 - 这将调用 ChangeListener,然后更改 JSpinner 的值-- 它将调用 ChangeListener,然后更改 JSpinner 的值 -- 它将调用 ChangeListener,然后更改 JSpinner 的值 -- 它将调用 ChangeListener,然后更改 JSpinner 的值JSpinner —— 它将调用 ChangeListener,然后更改 JSpinner 的值 —— 它将调用 ChangeListener,然后更改 JSpinner 的值 —— .... 无限循环。

不要这样做。

关于java - 如何将值从 jtextfield 复制到 jspinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23722708/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com