Java 如何从另一个类访问和更改JTextArea
我已经用PHP和其他web编程语言编程很长时间了,但我对Java真的很陌生。由于我在用PHP编程时一直使用过程方法,所以我对OOP也很陌生。现在我将学习一个非常基本的Java教程 我有此代码用于显示到不同的“银行帐户”: 下面是“Account”类: 这真的很有效。但是现在我想把这些信息输出到JTextArea。因此,我为UseCount类编写了以下代码:Java 如何从另一个类访问和更改JTextArea,java,swing,oop,class,jtextarea,Java,Swing,Oop,Class,Jtextarea,我已经用PHP和其他web编程语言编程很长时间了,但我对Java真的很陌生。由于我在用PHP编程时一直使用过程方法,所以我对OOP也很陌生。现在我将学习一个非常基本的Java教程 我有此代码用于显示到不同的“银行帐户”: 下面是“Account”类: 这真的很有效。但是现在我想把这些信息输出到JTextArea。因此,我为UseCount类编写了以下代码: public class Account{ String name; String address; double
public class Account{
String name;
String address;
double balance;
void display() {
System.out.print(name);
System.out.print(" (");
System.out.print(address);
System.out.print(") has $");
System.out.print(balance);
}
}
import java.awt.*;
import javax.swing.*;
public class UseAccount extends JFrame {
JTextArea output = new JTextArea();
public UseAccount() {
setLayout(new BorderLayout());
add(output, BorderLayout.CENTER);
}
public static void main(String[] args) {
UseAccount frame = new UseAccount();
frame.setTitle("Account");
frame.setSize(500,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Account myAccount = new Account();
Account yourAccount = new Account();
myAccount.name = "Jimmy";
myAccount.address = "Arjeplogsvägen 1";
myAccount.balance = 1250.70;
yourAccount.name = "Greg Giraldo";
yourAccount.address = "Fishermans friend's 4";
yourAccount.balance = -5820.30;
myAccount.display();
System.out.println();
yourAccount.display();
}
}
然后我试着让“Account”类扩展“useCount”类,然后使用output.append(“the_text”)来显示文本。但这显然不起作用:
public class Account extends UseAccount{
String name;
String address;
double balance;
void display() {
output.append(name);
output.append(" (");
output.append(address);
System.out.print(") has $");
System.out.print(balance);
}
}
我没有将每个system.out.print()都更改为output.append,因为它无论如何都不起作用
我想知道如何从另一个类访问和更改我的textarea(“输出”)的文本
我希望有人能帮我解决这个小问题
我确实知道早些时候也有人问过类似的问题。我试着寻找解决这些问题的方法来解决我的问题。但其他大多数问题都太复杂了,我无法理解这是怎么回事。因此,我现在试着提出我自己的问题。继承不是所有问题的答案,这里根本不是答案。继承应该以“is-a”为模型,并且想不到它是一个
帐户auseCount
相反,只需更改display()
的签名,将JTextArea
作为参数;然后在display()。调用display()
时,将JTextArea
传入
换言之:
void display(JTextArea ta) {
ta.append(name);
...
然后
// "frame" is the UseAccount object that contains the JTextArea variable `output`
myAccount.display(frame.output);
大多数时候,正确的问题不是“X如何访问Y的一部分”,而是“Y如何访问X自身的一部分?”
最后一点注意:在命名变量方面付出的小小努力确实是值得的 继承不是一切的答案,这里根本不是答案。继承应该以“is-a”为模型,并且想不到它是一个帐户auseCount
相反,只需更改display()
的签名,将JTextArea
作为参数;然后在display()。调用display()
时,将JTextArea
传入
换言之:
void display(JTextArea ta) {
ta.append(name);
...
然后
// "frame" is the UseAccount object that contains the JTextArea variable `output`
myAccount.display(frame.output);
大多数时候,正确的问题不是“X如何访问Y的一部分”,而是“Y如何访问X自身的一部分?”
最后一点注意:在命名变量方面付出的小小努力确实是值得的 Account
类应该更符合以下原则
public class Account extends UseAccount{
String name;
String address;
double balance;
@Override
public String toString() {
return name +
" (" +
address +
") has $" +
balance;
}
}
通过重写toString()
我们可以简单地执行以下操作:
Account account = //...
System.out.println(account);
output.setText(account.toString());
Account
类可能应该更符合以下内容
public class Account extends UseAccount{
String name;
String address;
double balance;
@Override
public String toString() {
return name +
" (" +
address +
") has $" +
balance;
}
}
通过重写toString()
我们可以简单地执行以下操作:
Account account = //...
System.out.println(account);
output.setText(account.toString());
在阅读之前的答案时,我想到了另一种方法(没有太大不同):
-类帐户提供将放置在TextArea上的文本
-框架获取该文本并将其放置在TextArea中
在你需要的班级帐户中
public class Account {
String name;
String address;
double balance;
public String toString() {
return name + address + balance; // This string should follow your desired format
}
}
在画面中:
...
output.append(account.toString())
...
在阅读之前的答案时,我想到了另一种方法(没有太大不同):
-类帐户提供将放置在TextArea上的文本
-框架获取该文本并将其放置在TextArea中
在你需要的班级帐户中
public class Account {
String name;
String address;
double balance;
public String toString() {
return name + address + balance; // This string should follow your desired format
}
}
在画面中:
...
output.append(account.toString())
...
“其他大多数问题都太复杂了,我无法理解它到底是关于什么的。”那么GUI现在可能不适合你。这就是,“其他大多数问题都太复杂了,我无法理解它到底是怎么回事”,那么GUI现在可能不适合你。这是。@trashgood谢谢;由于某种原因,当我在打字的时候,我脑海中浮现出他在使用AWT,但我显然错了!如果我这样做,并将myAccount.display(输出)放在“publicstaticvoidmain”中,我会得到错误“无法从静态上下文引用非静态变量输出”。@user1308385——你是对的。由于output
是usecomport
的实例成员,因此需要指定要使用其output
成员的usecomport
对象;请看我的编辑。谢谢!现在我想我开始掌握这个物体思考的窍门了@感谢上帝;由于某种原因,当我在打字的时候,我脑海中浮现出他在使用AWT,但我显然错了!如果我这样做,并将myAccount.display(输出)放在“publicstaticvoidmain”中,我会得到错误“无法从静态上下文引用非静态变量输出”。@user1308385——你是对的。由于output
是usecomport
的实例成员,因此需要指定要使用其output
成员的usecomport
对象;请看我的编辑。谢谢!现在我想我开始掌握这个物体思考的窍门了!