下面有两个代码部分。第一个执行我想要的第二个操作。第一个代码可以编译并正常工作,而第二个则不能。我是Java新手,并且认为我缺少有关如何调用另一个对象标签或微调器的基本知识。 我想要一种在微调器状态发生变化时执行所有操作的方法。我愿意接受所有良好实践的实现。我在Eclipse中使用Window Builder。
package stackoverflowtestgui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.SpinnerNumberModel;
public class TestGUI {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestGUI window = new TestGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TestGUI() {
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);
frame.getContentPane().setLayout(null);
JLabel lblTotal = new JLabel("total: "+total);
lblTotal.setBounds(31, 38, 101, 14);
frame.getContentPane().add(lblTotal);
JSpinner spinner_2 = new JSpinner();
JSpinner spinner_1 = new JSpinner();
spinner_2.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
num1 = (double) spinner_1.getValue();
num2 = (double) spinner_2.getValue();
total = num1+num2;
lblTotal.setText("total: "+total);
}
});
spinner_2.setModel(new SpinnerNumberModel(new Double(0), null, null, new Double(1)));
spinner_2.setBounds(31, 73, 30, 20);
frame.getContentPane().add(spinner_2);
spinner_1.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
num1 = (double) spinner_1.getValue();
num2 = (double) spinner_2.getValue();
total = num1+num2;
lblTotal.setText("total: "+total);
}
});
spinner_1.setModel(new SpinnerNumberModel(new Double(0), null, null, new Double(1)));
spinner_1.setBounds(31, 104, 30, 20);
frame.getContentPane().add(spinner_1);
}
public double num1=0;
public double num2=0;
public double total=0;
}
这是我的代码如何执行我想要的事情的示例:)
package stackoverflowtestgui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.SpinnerNumberModel;
public class TestGUI {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestGUI window = new TestGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TestGUI() {
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);
frame.getContentPane().setLayout(null);
lblTotal.setBounds(31, 38, 101, 14);
frame.getContentPane().add(lblTotal);
spinner_2.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
newmethod();
}
});
spinner_2.setModel(new SpinnerNumberModel(new Double(0), null, null, new Double(1)));
spinner_2.setBounds(31, 73, 72, 20);
frame.getContentPane().add(spinner_2);
spinner_1.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
newmethod();
}
});
spinner_1.setModel(new SpinnerNumberModel(new Double(0), null, null, new Double(1)));
spinner_1.setBounds(31, 104, 72, 20);
frame.getContentPane().add(spinner_1);
}
protected void newmethod() {
// TODO Auto-generated method stub
num1 = (double) spinner_1.getValue();
num2 = (double) spinner_2.getValue();
total = num1+num2;
lblTotal.setText("total: "+total);
}
public double num1=0;
public double num2=0;
public double total=0;
public JSpinner spinner_2 = new JSpinner();
public JSpinner spinner_1 = new JSpinner();
public JLabel lblTotal = new JLabel("total: ");
}
答案 0 :(得分:0)
您已经在initialize()方法中将变量spinner_1,spinner_2和lblTotal声明为局部变量,因此在该方法之外不可见。它们应该是该类的私有实例变量。