gpt4 book ai didi

java - 是否可以通过单击来展开 JTextArea 或 JTextPane?

转载 作者:行者123 更新时间:2023-11-30 06:50:43 25 4
gpt4 key购买 nike

我正在尝试做一个应用程序,但我不知道是否可以做这样的事情,例如:enter image description here

所以矩形是JTextArea(或JTextPane),它有一个固定的zise,这就是悬挂点的原因,但是当我像这样点击它时:

enter image description here

我们得到了JTextArea(或JTextPane)的扩展,但是当焦点丢失时它会回到开头:

enter image description here

文本可以是任何内容,因此当文本太长时,自动在末尾添加“...”

最佳答案

另一种选择是使用 CardLayout 和可聚焦的 JLabel 而不是 JTextField,以便使用默认的 JLabel > 截断功能:

screenshot

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TextAreaExpandTest {
private static final String TEXT =
"The text can be anything, so when the text is too long," +
" automatically add '...' at the end.";
public JComponent makeUI() {
CardLayout cardLayout = new CardLayout();
JPanel cp = new JPanel(cardLayout);

JTextArea textArea = new JTextArea(TEXT, 5, 10) {
@Override public void updateUI() {
super.updateUI();
setLineWrap(true);
setWrapStyleWord(true);
setMargin(new Insets(1, 1, 1, 1));
}
};

JLabel textField = new JLabel(" ") {
@Override public void updateUI() {
super.updateUI();
setOpaque(true);
setFocusable(true);
setBackground(UIManager.getColor("TextField.background"));
setForeground(UIManager.getColor("TextField.foreground"));
setBorder(UIManager.getBorder("TextField.border"));
}
};

textArea.addFocusListener(new FocusAdapter() {
@Override public void focusLost(FocusEvent e) {
String text = textArea.getText();
textField.setText(text.isEmpty() ? " " : text);
cardLayout.show(cp, "TextField");
}
});
textField.addFocusListener(new FocusAdapter() {
@Override public void focusGained(FocusEvent e) {
cardLayout.show(cp, "TextArea");
textArea.requestFocusInWindow();
}
});
textField.addMouseListener(new MouseAdapter() {
@Override public void mousePressed(MouseEvent e) {
cardLayout.show(cp, "TextArea");
textArea.requestFocusInWindow();
}
});
JPanel panel = new JPanel(new BorderLayout());
panel.add(textField, BorderLayout.NORTH);
JScrollPane scroll = new JScrollPane(
textArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
cp.add(panel, "TextField");
cp.add(scroll, "TextArea");

JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createEmptyBorder(32, 32, 32, 32));
p.add(cp, BorderLayout.NORTH);
p.add(new JButton("focus dummy"), BorderLayout.SOUTH);
return p;
}
// //TEST: JTextArea"setRows(...)
// public JComponent makeUI2() {
// JPanel p = new JPanel(new BorderLayout());
// JTextArea textArea = new JTextArea("", 1, 10);
// textArea.setLineWrap(true);
// textArea.addFocusListener(new FocusListener() {
// @Override public void focusGained(FocusEvent e) {
// JTextArea ta = (JTextArea) e.getComponent();
// ta.setRows(8);
// p.revalidate();
// }
// @Override public void focusLost(FocusEvent e) {
// JTextArea ta = (JTextArea) e.getComponent();
// ta.setRows(1);
// p.revalidate();
// }
// });
// JScrollPane scroll = new JScrollPane(
// textArea,
// ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
// ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
// p.add(scroll, BorderLayout.NORTH);
// p.add(new JButton("focus dummy"), BorderLayout.SOUTH);
// p.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// return p;
// }
public static void main(String... args) {
EventQueue.invokeLater(() -> {
UIManager.put("swing.boldMetal", Boolean.FALSE);
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new TextAreaExpandTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}

关于java - 是否可以通过单击来展开 JTextArea 或 JTextPane?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42847168/

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