我試圖做一個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