gpt4 book ai didi

java - 将可编辑文本框添加到 JFrame

转载 作者:行者123 更新时间:2023-12-01 23:15:10 24 4
gpt4 key购买 nike

有没有办法将文本框放入用户可以输入文本的 JFrame 中?有点像 TextEdit 或记事本应用程序。

最佳答案

与记事本最接近的等效项是JTextArea。将其包装在用于滚动条的 JScrollPane 中。可见。

Notepad

Editpad

提示:将其设为原生 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);
}
}

关于java - 将可编辑文本框添加到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21357286/

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