如何使用java中的其他类公共类中定义的变量? [英] How to use variables defined in a public class in other classes in java?
问题描述
我需要制作一个获取用户输入的Java GUI,并将其存储在文本文件中。然而,这个写作必须在一个Actionlistener类中完成(例如,用户点击按钮并创建并存储文本文件)。这意味着我必须在一个类(公共类)中定义一个变量,并在另一个类中定义一个变量(定义Actionlistener的变量)。
我该怎么做?是全局变量的唯一方法吗?
在我的代码中,我首先定义textfield为JTextField,然后我希望它被读取(如文本)和存储在'text.txt')。
import javax.swing。*;
// ...
import java.io.BufferedWriter;
public class Runcommand33
{
public static void main(String [] args)
{
final JFrame frame = new JFrame(Change Backlight );
// ...
//定义框架,面板,按钮和位置
JTextField textfield = new JTextField(); textfield.setBounds(35,20,160,30);
panel.add(textfield);
frame.setVisible(true);
button.addActionListener(new ButtonHandler());
$ b class ButtonHandler实现ActionListener {
public void actionPerformed(ActionEvent event){
String text = textfield.getText();
textfield.setText();
new BufferedWriter(new FileWriter(text.txt))。write(text).newLine()。close();
//之后需要'text'来运行一个命令
code $ pre
当我编译时,我得到
Runcommand33.java:45:error:can not find symbol
String text = textfield.getText();
$
符号:变量textfield
位置:class ButtonHandler
没有行 String text = 新的BufferedWriter 代码编译。
请注意,我已经尝试了这个建议
在其他类中获取变量
和
如何在另一个类的函数中访问一个类的变量?
但是他们没有工作。
有什么建议吗?我们从设计的角度来看这个: ButtonHandler
听起来有点过于简单通用的。点击处理按钮的方式是什么?嗯,它将文本字段的内容保存到一个文件,所以它应该被称为TextFieldSaver(或最好不要跛脚)。
现在,一个TextFieldSaver需要有一个文本字段来保存,是吗?因此,添加一个成员变量来保存文本字段,并通过构造函数传递在主类中创建的文本字段:
button .addActionListener(new TextFieldSaver(textfield));
...
class TextFieldSaver实现ActionListener {
JTextField textfield;
public TextFieldSaver(JTextField toBeSaved){
textfield = toBeSaved;
public void actionPerformed(ActionEvent event){
String text = textfield.getText();
textfield.setText();
new BufferedWriter(new FileWriter(text.txt))。write(text).newLine()。close();
$ b 这不是唯一的办法,也不一定是最好的方法,但我希望它显示如何使用专有名称有时显示出路。
A layman's question on the definition and use of variables:
I need to make a Java GUI that gets user's input and stores it within a text file. However this writing has to be done inside an Actionlistener class (ie, user is clicking the button and text file is created and stored). This means that I have to define a variable in one class (public class) and use it in another (the one that defines the Actionlistener).
How can I do this? Are global variables the only way?
In my code I first define 'textfield' as JTextField and then I want it to be read (as 'text') and stored (in 'text.txt').
import javax.swing.*;
//...
import java.io.BufferedWriter;
public class Runcommand33
{
public static void main(String[] args)
{
final JFrame frame = new JFrame("Change Backlight");
// ...
// define frames, panels, buttons and positions
JTextField textfield = new JTextField();textfield.setBounds(35,20,160,30);
panel.add(textfield);
frame.setVisible(true);
button.addActionListener(new ButtonHandler());
}
}
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String text = textfield.getText();
textfield.setText("");
new BufferedWriter(new FileWriter("text.txt")).write(text).newLine().close();
// Afterwards 'text' is needed to run a command
}
}
When I compile I get
Runcommand33.java:45: error: cannot find symbol
String text = textfield.getText();
^
symbol: variable textfield
location: class ButtonHandler
Without lines String text = to new BufferedWriter the code compiles.
Note that I have tried the suggestions of this
Get variable in other classes
and this
How do I access a variable of one class in the function of another class?
but they didn't work.
Any suggestions?
解决方案 Let's look at this from a design perspective: ButtonHandler
sounds a little too generic. In what way is the button click "handled"? Ah, it saves the contents of the text field to a file, so it should be called "TextFieldSaver" (or preferably something less lame).
Now, a TextFieldSaver needs to have a text field to save, yes? So add a member variable to hold the text field, and pass the text field created in the main class through a constructor:
button.addActionListener(new TextFieldSaver(textfield));
....
class TextFieldSaver implements ActionListener {
JTextField textfield;
public TextFieldSaver(JTextField toBeSaved) {
textfield = toBeSaved;
}
public void actionPerformed(ActionEvent event) {
String text = textfield.getText();
textfield.setText("");
new BufferedWriter(new FileWriter("text.txt")).write(text).newLine().close();
}
}
This isn't the only way to do it, nor necessarily the best way, but I hope it shows how using proper names sometimes shows a way out.
这篇关于如何使用java中的其他类公共类中定义的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!