将 JSliders 给出的两个数字相加
adding two numbers given by JSliders
假设我们要将用户指定的两个数字 (0 <= n <= 20) 添加到两个 JSlider 中并打印出来。问题是当我将 JSliders 的值分配给 x 和 y 变量时,它们只是不保存新值并保持 0(起初它们用 0 初始化),这里是代码:
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
public class GUIJframe extends JFrame {
private JPanel jp1 = new JPanel();
private JTextField jtf = new JTextField();
private JTextField jtf2 = new JTextField();
private JSlider js1 = new JSlider(0,20);
private JSlider js2 = new JSlider(0,20);
private int x = 0;
private int y = 0;
public GUIJframe () {
setSize(400,400);
setLocation(200,200);
setLayout(new GridLayout(5,1));
add(js1);
add(jtf);
add(js2);
add(jtf2);
Handler h = new Handler();
Handler2 h2 = new Handler2();
js1.addChangeListener(h);
js2.addChangeListener(h2);
setVisible(true);
System.out.println(x + y);
}
private class Handler implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
JSlider js = (JSlider) e.getSource();
jtf.setText( "" + js.getValue() );
x = js.getValue();
}
}
private class Handler2 implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
JSlider js = (JSlider) e.getSource();
jtf2.setText( "" + js.getValue() );
y = js.getValue();
}
}
}
当两个 x,y
都为零时,您只打印一次 x+y
,甚至在您移动滑块之前。
您可以在 Handler
或 Handler1
中添加 System.out.println(x + y);
然后如果您移动滑块它将显示总和。
如果您想在移动 JSlider 时打印 x 和 y 的总和,请移动您的 System.out.println(x+y);
进入更改侦听器方法
下面是代码:-
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
public class GUIJframe extends JFrame {
private JPanel jp1 = new JPanel();
private JTextField jtf = new JTextField();
private JTextField jtf2 = new JTextField();
private JSlider js1 = new JSlider(0,20);
private JSlider js2 = new JSlider(0,20);
private int x = 0;
private int y = 0;
public GUIJframe () {
setSize(400,400);
setLocation(200,200);
setLayout(new GridLayout(5,1));
add(js1);
add(jtf);
add(js2);
add(jtf2);
Handler h = new Handler();
Handler2 h2 = new Handler2();
js1.addChangeListener(h);
js2.addChangeListener(h2);
setVisible(true);
//dont use print statement here
}
private class Handler implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
JSlider js = (JSlider) e.getSource();
jtf.setText( "" + js.getValue() );
x = js.getValue();
System.out.println(x + y);//use it here
}
}
private class Handler2 implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
JSlider js = (JSlider) e.getSource();
jtf2.setText( "" + js.getValue() );
y = js.getValue();
System.out.println(x + y);//and here also
}
}
}
假设我们要将用户指定的两个数字 (0 <= n <= 20) 添加到两个 JSlider 中并打印出来。问题是当我将 JSliders 的值分配给 x 和 y 变量时,它们只是不保存新值并保持 0(起初它们用 0 初始化),这里是代码:
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
public class GUIJframe extends JFrame {
private JPanel jp1 = new JPanel();
private JTextField jtf = new JTextField();
private JTextField jtf2 = new JTextField();
private JSlider js1 = new JSlider(0,20);
private JSlider js2 = new JSlider(0,20);
private int x = 0;
private int y = 0;
public GUIJframe () {
setSize(400,400);
setLocation(200,200);
setLayout(new GridLayout(5,1));
add(js1);
add(jtf);
add(js2);
add(jtf2);
Handler h = new Handler();
Handler2 h2 = new Handler2();
js1.addChangeListener(h);
js2.addChangeListener(h2);
setVisible(true);
System.out.println(x + y);
}
private class Handler implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
JSlider js = (JSlider) e.getSource();
jtf.setText( "" + js.getValue() );
x = js.getValue();
}
}
private class Handler2 implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
JSlider js = (JSlider) e.getSource();
jtf2.setText( "" + js.getValue() );
y = js.getValue();
}
}
}
当两个 x,y
都为零时,您只打印一次 x+y
,甚至在您移动滑块之前。
您可以在 Handler
或 Handler1
中添加 System.out.println(x + y);
然后如果您移动滑块它将显示总和。
如果您想在移动 JSlider 时打印 x 和 y 的总和,请移动您的 System.out.println(x+y);
进入更改侦听器方法
下面是代码:-
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
public class GUIJframe extends JFrame {
private JPanel jp1 = new JPanel();
private JTextField jtf = new JTextField();
private JTextField jtf2 = new JTextField();
private JSlider js1 = new JSlider(0,20);
private JSlider js2 = new JSlider(0,20);
private int x = 0;
private int y = 0;
public GUIJframe () {
setSize(400,400);
setLocation(200,200);
setLayout(new GridLayout(5,1));
add(js1);
add(jtf);
add(js2);
add(jtf2);
Handler h = new Handler();
Handler2 h2 = new Handler2();
js1.addChangeListener(h);
js2.addChangeListener(h2);
setVisible(true);
//dont use print statement here
}
private class Handler implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
JSlider js = (JSlider) e.getSource();
jtf.setText( "" + js.getValue() );
x = js.getValue();
System.out.println(x + y);//use it here
}
}
private class Handler2 implements ChangeListener {
@Override
public void stateChanged(ChangeEvent e) {
JSlider js = (JSlider) e.getSource();
jtf2.setText( "" + js.getValue() );
y = js.getValue();
System.out.println(x + y);//and here also
}
}
}