TextArea 与 JTextArea Java
TextArea vs JTextArea Java
所以我在玩文字冒险游戏的 JTextAreas,并使用了一些我在网上找到的代码片段。无论我尝试什么,其中一些片段都不起作用,然后我意识到其中一些片段来自 TextArea 而不是 JTextArea。那么两者有什么区别呢?我通过 google 搜索发现 JTextArea class 必须嵌入到 JScrollPane 中,但这是什么意思?
我正在使用的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Text extends JPanel implements ActionListener {
protected JTextField textField;
protected JTextArea textArea;
private final static String newline = "\n";
public Text() {
super(new GridBagLayout());
textField = new JTextField(75);
textField.addActionListener(this);
textArea = new JTextArea(5,75);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
//Add Components to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
}
public void actionPerformed(ActionEvent evt) {
String text = textField.getText();
textArea.append(text + newline);
textField.selectAll();
//Make sure the new text is visible, even if there
//was a selection in the text area.
textArea.setCaretPosition(textArea.getDocument().getLength());
}
/**
* 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.
JFrame frame = new JFrame("Text adventure");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add contents to the window.
frame.add(new Text());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
javax.swing.JTextArea
是旧的 Swing component, while java.awt.TextArea
is an even older AWT component (AWT stands for "Abstract Windows Toolkit" - some history here)。
当您使用 JScrollPane
或大多数 Swing 组件时,您可能会嵌入其他 Swing 组件。
TextArea 是 Java 1.0 的一部分,它链接到/使用原生 OS TextArea 组件(现代 OSes 已经内置了这样的 GUI 工具包)但就跨平台兼容性而言,与 java.awt 中的许多其他内容一样存在问题。
JTextArea 是 (javax.) Swing 的一部分。这个项目是在 TextArea 等对等组件出现重大问题后开发的,以提供 100% java GUI 解决方案,该解决方案将 100% 跨平台(它不完全是 100%,但它一样好如你所愿)
另一个区别是旧的awt 组件都是线程安全的,而swing 组件不是。 Swing 组件只能由 UI 线程修改。使用 google 了解更多信息。
JTextArea 在 JScrollPane 之外也能正常工作,但您通常希望在 JScrollPane 中使用它,以便其内容可以超出其布局范围。
如今,Swing 和 JavaFX 优于 java.awt
中包含的 GUI 组件
所以我在玩文字冒险游戏的 JTextAreas,并使用了一些我在网上找到的代码片段。无论我尝试什么,其中一些片段都不起作用,然后我意识到其中一些片段来自 TextArea 而不是 JTextArea。那么两者有什么区别呢?我通过 google 搜索发现 JTextArea class 必须嵌入到 JScrollPane 中,但这是什么意思?
我正在使用的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Text extends JPanel implements ActionListener {
protected JTextField textField;
protected JTextArea textArea;
private final static String newline = "\n";
public Text() {
super(new GridBagLayout());
textField = new JTextField(75);
textField.addActionListener(this);
textArea = new JTextArea(5,75);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
//Add Components to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
}
public void actionPerformed(ActionEvent evt) {
String text = textField.getText();
textArea.append(text + newline);
textField.selectAll();
//Make sure the new text is visible, even if there
//was a selection in the text area.
textArea.setCaretPosition(textArea.getDocument().getLength());
}
/**
* 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.
JFrame frame = new JFrame("Text adventure");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add contents to the window.
frame.add(new Text());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
javax.swing.JTextArea
是旧的 Swing component, while java.awt.TextArea
is an even older AWT component (AWT stands for "Abstract Windows Toolkit" - some history here)。
当您使用 JScrollPane
或大多数 Swing 组件时,您可能会嵌入其他 Swing 组件。
TextArea 是 Java 1.0 的一部分,它链接到/使用原生 OS TextArea 组件(现代 OSes 已经内置了这样的 GUI 工具包)但就跨平台兼容性而言,与 java.awt 中的许多其他内容一样存在问题。
JTextArea 是 (javax.) Swing 的一部分。这个项目是在 TextArea 等对等组件出现重大问题后开发的,以提供 100% java GUI 解决方案,该解决方案将 100% 跨平台(它不完全是 100%,但它一样好如你所愿)
另一个区别是旧的awt 组件都是线程安全的,而swing 组件不是。 Swing 组件只能由 UI 线程修改。使用 google 了解更多信息。
JTextArea 在 JScrollPane 之外也能正常工作,但您通常希望在 JScrollPane 中使用它,以便其内容可以超出其布局范围。
如今,Swing 和 JavaFX 优于 java.awt
中包含的 GUI 组件