我使用重写的toString方法设置一个简单的随机生成的字符串,以显示从文本文件中检索的一些项目。我可以让它在控制台中显示得很好,但我还在学习GUI,目前正在使用内置GUI工具的NetBeans。我还有主要的类Automobile.java和一个单独的GUI.java类。我在GUI中有一个jTextArea,当我点击一个按钮时,我想要填充我打印到控制台的内容,但是我在开始使用它时遇到了麻烦。任何帮助,将不胜感激!谢谢!
注意:我已经在这里看了7或8个关于类似问题的其他讨论,但我仍然无法弄清楚。
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
class Automobile implements Comparable<Automobile> {
static java.util.Random randomGen = new java.util.Random();
static ArrayList<String> carMakes = new ArrayList<>();
static ArrayList<String> carModels = new ArrayList<>();
static SORTBY sortBy = SORTBY.MAKE;
static int nextUID = 1;
String make, model;
double engineSize = 0;
int doors = 0;
int invID = 0;
static {
try {
java.util.Scanner scFirst = new java.util.Scanner
(new java.io.File("carMakes.txt"));
java.util.Scanner scLast = new java.util.Scanner
(new java.io.File("carModels.txt"));
while (scFirst.hasNext()) {
carMakes.add(scFirst.next());
}
while (scLast.hasNext()) {
carModels.add(scLast.next());
}
} catch (java.io.FileNotFoundException e) {
System.out.println(e);
} // end try catch
} // end static intializer
enum SORTBY {
MAKE, MODEL, DOORS, ENGINESIZE
}
public Automobile(String st) {
this(new Scanner(st));
}
public Automobile(Scanner sc) {
invID = nextUID++;
make = sc.next();
model = sc.next();
doors = sc.nextInt();
engineSize = sc.nextDouble();
} // end Scanner constructor
public Automobile() {
invID = nextUID++;
} // no parameter constructor
public int compareTo(Automobile x) {
switch (sortBy) {
case MAKE:
return make.compareTo(x.make);
case MODEL:
return model.compareTo(x.model);
case DOORS:
return doors - x.doors;
case ENGINESIZE:
return (engineSize > x.engineSize) ? 1 : -1;
} // end switch
return 0;
} // end compareTo for Comparable interface
public String toString() {
return String.format
("%5d %15s %15s %5d %10.1fL", invID, make, model, doors, engineSize);
}// end method toString
public static Automobile[] makeRandom(int m) {
Automobile[] auto = new Automobile[m];
for (int i = 0; i < auto.length; i++) {
auto[i] = new Automobile();
auto[i].make = carMakes.get(randomGen.nextInt(carMakes.size()));
auto[i].model = carModels.get(randomGen.nextInt(carModels.size()));
auto[i].doors = randomGen.nextInt(6);
if(auto[i].doors == 0 || auto[i].doors == 1)
auto[i].doors = 2;
auto[i].engineSize = randomGen.nextDouble() * 6.0;
if(auto[i].engineSize <= 1.49)
auto[i].engineSize = 1.5;
} // end for each student to instantiate
return auto;
} // end method makeRandom
public static void main(String args[]) {
System.out.println(new Automobile("X X 0 0.0"));
Automobile[] x = makeRandom(5);
for (Automobile m : x) {
System.out.println(m);
}
Arrays.sort(x);
System.out.println("---- Sorted By Make -----");
for (Automobile m : x) {
System.out.println(m);
}
System.out.println("---- Sorted By Model -----");
Automobile.sortBy = SORTBY.MODEL;
Arrays.sort(x);
for (Automobile m : x) {
System.out.println(m);
}
System.out.println("---- Sorted By Doors -----");
Automobile.sortBy = SORTBY.DOORS;
Arrays.sort(x);
for (Automobile m : x) {
System.out.println(m);
}
System.out.println("---- Sorted By Engine Size -----");
Automobile.sortBy = SORTBY.ENGINESIZE;
Arrays.sort(x);
for (Automobile m : x) {
System.out.println(m);
}
} // end main
}
GUI类
答案 0 :(得分:0)
如果我理解正确,您可以在GUI类中创建对JTextArea的静态引用。在创建一个按钮之后,你应该为它设置动作监听器并覆盖这样的actionPerformed方法(取消匿名内部类):
JButton button = new JButton("Button");
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
//adds text to the end of the document of the textArea
MyClass.myTextArea.append(automobile.toString());
//or
//overrides the document of the textArea with your new string
MyClass.myTextArea.setText(automobile.toString());
}
});