Java 将可编辑文本框添加到 JFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21357286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a Editable Text Box to a JFrame
提问by alexsmbaratti
Is there any way to put a text box into a JFrame that the user can input text into? Kind of like the TextEdit or Notepad app.
有没有办法将文本框放入用户可以输入文本的 JFrame 中?有点像 TextEdit 或记事本应用程序。
采纳答案by Andrew Thompson
The closest equivalent to Notepad is a JTextArea
. Wrap it in a JScrollPane
for scroll-bars. Vis.
与记事本最接近的等价物是JTextArea
. 将其包裹在JScrollPane
滚动条中。可见。
Tip: Make it the native PLAF for an even closer lookalike.
提示:将其设为原生 PLAF 以获得更接近的外观。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class Editpad {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new BorderLayout());
gui.setBorder(new EmptyBorder(2,3,2,3));
// adjust numbers for a bigger default area
JTextArea editArea = new JTextArea(5,40);
// adjust the font to a monospaced font.
Font font = new Font(
Font.MONOSPACED,
Font.PLAIN,
editArea.getFont().getSize());
editArea.setFont(font);
gui.add(new JScrollPane(editArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
JFrame f = new JFrame("Editpad");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
回答by Martin Dinov
Use a JTextField
, which is a single-line "text area", or a JTextArea
, which can handle multiple lines. Both work similarly. Here's a simple example with JTextField
:
使用 a JTextField
,它是一个单行“文本区域”,或者 a JTextArea
,它可以处理多行。两者的工作方式相似。这是一个简单的例子JTextField
:
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextField;
public class Foo extends JFrame {
public Foo() {
setLayout(new FlowLayout());
JTextField field = new JTextField(20);
add(field);
setSize(500, 500);
setVisible(true);
}
public static void main(String[] args) {
Foo foo = new Foo();
}
}
To do anything useful with such a component, you need to also add an event/action listener to a component (say a JButton
) to do something with the text in it (which you can get by calling getText()
on the JTextField
or JTextArea
)
做任何有用的事情有这样的组件,你也需要一个事件/动作监听器添加到组件(说JButton
)做一些与它的文本(你可以通过调用得到getText()
的JTextField
或JTextArea
)