gpt4 book ai didi

java - 如何从不包含 JTextArea 的方法附加 JTextArea?

转载 作者:行者123 更新时间:2023-11-29 03:26:12 27 4
gpt4 key购买 nike

我正在编写一些测试代码来练习 OOP,我想将 JTextArea 从“writeToArea”附加到定义和初始化 JTextArea 的“initialize”方法。我已经尝试直接调用“输出”变量,但这会返回“无法解析输出”错误。我想要这样,每当我在主类中调用“writeToArea”方法时,我就能够在“initialize”方法中向“output”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.");
}
}

我试过做类似的事情:Updating jtextarea from another class但它没有用。如果有人有任何建议,我将不胜感激。

最佳答案

您代码中的主要错误是缺乏 OOP 设计。让所有静态都是糟糕的设计。swing 也是基于事件的,因此您应该在事件发生时将文本附加到 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.");
}
}

这里有一个教程,其中包含比这个更好的示例 How to Use Text Areas

我删除了您的 setSize 并使用了 pack()

The pack method sizes the frame so that all its contents are at or above their preferred sizes. An alternative to pack is to establish a frame size explicitly by calling setSize or setBounds (which also sets the frame location). In general, using pack is preferable to calling setSize, since pack leaves the frame layout manager in charge of the frame size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size.

关于java - 如何从不包含 JTextArea 的方法附加 JTextArea?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20861588/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com