New to Java and this Page. I am trying to perfom a certain action when a JButton is pressed. Following this tutorial (in German, sorry) http://www.java-tutorial.org/actionlistener.html, I used the JFrame Design feature and added content afterwards:
Java和本页新手。我试图在按下JButton时执行某个动作。按照本教程(德语,抱歉)http://www.java-tutorial.org/actionlistener.html,我使用了JFrame Design功能并在之后添加了内容:
public class JFrame extends javax.swing.JFrame implements ActionListener
{
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrame frame = new JFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JFrame()
{
this.setTitle("BMS Anpassen");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnFlush = new JButton("Flush!");
btnFlush.setBounds(170, 209, 89, 23);
contentPane.add(btnFlush);
JSpinner spinner = new JSpinner();
spinner.setToolTipText("Gib die Spannung ein!");
spinner.setModel(new SpinnerNumberModel(new Float(3000), new Float(2700), new Float(4100), new Float(100)));
spinner.setBounds(56, 49, 52, 20);
contentPane.add(spinner);
JLabel lblMv = new JLabel("mV");
lblMv.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblMv.setBounds(113, 51, 24, 14);
contentPane.add(lblMv);
JButton btnSetParameters = new JButton("Set Parameters");
btnSetParameters.setBounds(152, 175, 130, 23);
contentPane.add(btnSetParameters);
//Buttons dem Listener zuordnen
btnFlush.addActionListener(this);
btnSetParameters.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource() == this.btnSetParameters){
// Parameter in Config schreiben/Write parameters in Config
FileWriter fw = new FileWriter("ConfigBMS.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("25000");
bw.newLine();
bw.write("42000");
bw.newLine();
bw.write("27000");
bw.newLine();
bw.write("41000");
bw.close();
}
else
{
}
}
}
It now gives me the errormessage "btnSetParameters cannot be resolved or is not a field". Reading through other posts on JButtons here didn't help me or I didn't understand it. I feel like the this
in if(ae.getSource() == this.btnSetParameters){
is the problem, but I can't find a way to fix it.
它现在给我错误消息“btnSetParameters无法解析或不是字段”。阅读JButtons上的其他帖子对我没有帮助,或者我不理解它。我觉得if(ae.getSource()== this.btnSetParameters){是问题,但我无法找到修复它的方法。
Any push in the right direction is greatly appreciated and thank you in advance,
非常感谢任何正确方向的推动,并提前感谢您,
Oli
2 个解决方案
#1
2
you have an scope issue, btnSetParameters must be declared as a Frame member field
如果存在范围问题,则必须将btnSetParameters声明为Frame成员字段
public class JFrame extends javax.swing.JFrame implements ActionListener {
private JButton btnSetParameters;
and no inside the constructor, use the constructor instead to initialize the button!
并且不在构造函数内部,而是使用构造函数来初始化按钮!
public JFrame() {
....
btnSetParameters = new JButton("Set Parameters");
}
#2
0
You have to make the the JButton btnSetParameters
a field of the class - currently it's just in the context of the constructor.
你必须使JButton btnSetParameters成为类的一个字段 - 目前它只是在构造函数的上下文中。
#1
2
you have an scope issue, btnSetParameters must be declared as a Frame member field
如果存在范围问题,则必须将btnSetParameters声明为Frame成员字段
public class JFrame extends javax.swing.JFrame implements ActionListener {
private JButton btnSetParameters;
and no inside the constructor, use the constructor instead to initialize the button!
并且不在构造函数内部,而是使用构造函数来初始化按钮!
public JFrame() {
....
btnSetParameters = new JButton("Set Parameters");
}
#2
0
You have to make the the JButton btnSetParameters
a field of the class - currently it's just in the context of the constructor.
你必须使JButton btnSetParameters成为类的一个字段 - 目前它只是在构造函数的上下文中。