我试图做一个aplication但我不知道,如果是更多钞票,以做这样的事情,例如:点击它可以展开JTextArea或JTextPane?
所以矩形是JTextArea中(或JTextPane的),它有一个固定的姿色,这就是为什么有悬挂点,但是当我clikc上这样说:
我们得到的JTextArea(或JTextPane的)的扩展,但是当焦点losted它会回来的开始:
的文本可以是任何东西,所以当文本太长,自动添加“......”末
我试图做一个aplication但我不知道,如果是更多钞票,以做这样的事情,例如:点击它可以展开JTextArea或JTextPane?
所以矩形是JTextArea中(或JTextPane的),它有一个固定的姿色,这就是为什么有悬挂点,但是当我clikc上这样说:
我们得到的JTextArea(或JTextPane的)的扩展,但是当焦点losted它会回来的开始:
的文本可以是任何东西,所以当文本太长,自动添加“......”末
使用一个JPanel添加和删除JTextField和JTextArea与FocusListener
对于一个线路输入使用的JTextField,并添加“...”你需要实现KeyListener接口。
使用一个全局标志,该标志将在您对JTextField的GainFocus进行设置时设置。一个线程将检查这个标志,如果它被设置,那么它将从JPanel中删除JTextField对象,并添加带有保存字符串的JTextArea。
OR
您可以实现FocusListner到JPanel的,而不是更复杂的线程的事情。
另一种选择是使用CardLayout
和可聚焦JLabel
而不是JTextField
以使用默认JLabel
截断功能:
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);
});
}
}
一切都取决于你所使用的布局。我会做的是使用布局尊重其组件的preferredSize。然后,我会改变JTextPane的preferredSize给定一个布尔参数(展开或不展开)。
当我说更改preferredSize,我的意思是重写getPreferredSize方法。
是的,有可能,也许看看'JTextArea'的'setRows'方法。 'JTextPane'会更棘手 – MadProgrammer