[英]How can I append a JTextArea from a method that doesn't contain the JTextArea?
我正在編寫一些測試代碼來練習OOP,並且我想將“ writeToArea”中的JTextArea附加到定義和初始化JTextArea的“ initialize”方法中。 我已經嘗試直接調用“輸出”變量,但這會返回“無法解析輸出”錯誤。 我希望這樣,每當我在主類中調用“ writeToArea”方法時,就可以在“ initialize”方法中向“輸出” JTextArea添加行。
這是主要的課程:
public class Pangea {
public static void main(String[] args) {
UI.initialize();
UI.writeToArea();
}
}
這是初始化類:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class UI {
static void initialize() {
System.out.println("Initializing GUI.");
JFrame frame = new JFrame();
Font myFont = new Font("Courier", Font.BOLD, 14);
JTextField input = new JTextField("");
JTextArea output = new JTextArea("Initiated Succesfully.");
output.setWrapStyleWord(true);
output.setLineWrap(true);
input.setFont(myFont);
output.setFont(myFont);
input.setForeground(Color.WHITE);
output.setForeground(Color.WHITE);
input.setBackground(Color.BLACK);
input.setCaretColor(Color.WHITE);
output.setBackground(Color.BLACK);
output.setEditable(false);
JScrollPane jp = new JScrollPane(output);
frame.setTitle("PANGEA RPG [0.01 ALPHA][WIP]");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(input, BorderLayout.SOUTH);
frame.add(jp, BorderLayout.CENTER);
frame.pack();
frame.setSize(800, 500);
frame.setVisible(true);
System.out.println("GUI Initialized.");
}
static void writeToArea() {
System.out.println("\"writeToArea\" running.");
output.append("Hello!");
System.out.println("\"writeToArea\" finished.");
}
}
我試圖做類似的事情: 從另一個類更新jtextarea,但是沒有用。 如果有人有任何建議,我將非常感謝。
閱讀Swing教程中有關如何使用文本區域的部分 。 它將向您展示如何更好地構造代碼,以免在所有地方都使用靜態方法和變量。
一旦有了引用文本區域的面板,就可以添加允許您更新面板上文本區域的方法。
代碼中的主要錯誤是缺少OOP設計。 使所有靜態都是糟糕的設計。 擺動也是基於事件的,因此,當事件發生時,您應該將文本追加到textArea。 請參閱我為您編寫的示例。
public class UI {
private JPanel panel;
private JTextArea output;
public UI(){
initialize();
}
private void initialize() {
panel = new JPanel();
Font myFont = new Font("Courier", Font.BOLD, 14);
final JTextField input = new JTextField(""); // must be declared final cause you use it in anonymous class, you can make it instance variable if you want to as textArea
//add an actionListener then when you press enter this will write to textArea
input.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent evt){
writeToArea(input.getText());
}
});
output = new JTextArea("Initiated Succesfully",50,100);// let the component determinate its preferred size.
output.setWrapStyleWord(true);
output.setLineWrap(true);
input.setFont(myFont);
output.setFont(myFont);
input.setForeground(Color.WHITE);
output.setForeground(Color.WHITE);
input.setBackground(Color.BLACK);
input.setCaretColor(Color.WHITE);
output.setBackground(Color.BLACK);
output.setEditable(false);
JScrollPane jp = new JScrollPane(output);
panel.setLayout(new BorderLayout());
panel.add(input, BorderLayout.SOUTH);
panel.add(jp, BorderLayout.CENTER);
}
private void writeToArea(String something) {
System.out.println("\"writeToArea\" running.");
output.append(something);
System.out.println("\"writeToArea\" finished.");
}
public JPanel getPanel(){
return panel;
}
}
並在您的客戶代碼中
public class Pangea {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
createAndShowGUI();
}
});
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
System.out.println("Initializing GUI.");
JFrame frame = new JFrame();
frame.setTitle("PANGEA RPG [0.01 ALPHA][WIP]");
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add contents to the window.
frame.add(new UI().getPanel());
frame.pack();//sizes the frame
frame.setVisible(true);
System.out.println("GUI Initialized.");
}
}
在這里,您有一個教程,其中包含比本“ 如何使用文本區域”更好的示例。
我刪除您的setSize
並使用pack()
打包方法調整框架的大小,以便其所有內容均等於或大於其首選大小。 打包的另一種方法是通過調用setSize或setBounds(也設置幀位置)顯式建立幀大小。 通常,使用pack優於調用setSize,因為pack讓框架布局管理器負責框架大小,並且布局管理器擅長適應平台依賴性和其他影響組件大小的因素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.