有沒有什麼辦法使用JTextArea
隱藏文本時,用戶類型?在JTextArea的密碼
有點像密碼..
在JTextArea
我有
..
密碼:
在最後一行,並在該行的任何用戶類型應該是不可見。
我已經嘗試使用setForeground
方法將字體顏色設置爲textarea顏色,這使得文本不可見,但允許用戶複製和粘貼。
是否有任何解決方法,或者我該如何實現?
請幫
有沒有什麼辦法使用JTextArea
隱藏文本時,用戶類型?在JTextArea的密碼
有點像密碼..
在JTextArea
我有
..
密碼:
在最後一行,並在該行的任何用戶類型應該是不可見。
我已經嘗試使用setForeground
方法將字體顏色設置爲textarea顏色,這使得文本不可見,但允許用戶複製和粘貼。
是否有任何解決方法,或者我該如何實現?
請幫
這就是我說的是在註釋中。請注意,這只是一個簡單的例子(可能想安全地存儲密碼等),但應該讓你開始。
此代碼僅在JTextArea
上放置DocumentFilter
,並且絕不允許將密碼字符放入Document
。相反,他們被重新路由到其他地方。這與預期敏感輸入的控制檯的行爲類似。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;
public class CapturePassword extends JFrame {
private JScrollPane scroll;
private JTextArea textArea;
private JToggleButton expectPassword;
private StringBuilder password; // you would of course use something else
public CapturePassword() {
setLayout(new BorderLayout());
password = new StringBuilder();
textArea = new JTextArea();
scroll = new JScrollPane(textArea);
add(scroll);
expectPassword = new JToggleButton("Capture password");
add(expectPassword, BorderLayout.PAGE_END);
expectPassword.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (expectPassword.isSelected()) {
capture(true);
} else {
capture(false);
JOptionPane.showConfirmDialog(
CapturePassword.this,
"Captured password: " + password.toString(),
"Password!", JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE);
password.setLength(0); // reset
}
textArea.requestFocusInWindow();
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 400);
setLocationRelativeTo(null);
}
private void capture(boolean start) {
PlainDocument document = (PlainDocument)textArea.getDocument();
DocumentFilter filter = new DocumentFilter() {
private void doAppend(String text) {
if (text.endsWith("\n")) {
expectPassword.doClick();
} else {
password.append(text);
}
}
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
// you would have to handle multi-line pastes here also
doAppend(text);
}
@Override
public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException {
// cannot remove while filtering
}
@Override
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
doAppend(text);
}
};
document.setDocumentFilter(start ? filter : null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new CapturePassword().setVisible(true);
}
});
}
}
只需使用JPasswordField
的JTextField
一個子類。 它在java doc中非常清晰和簡單的討論,所以我避免重複。
是不是JPasswordField是JTextField的子類? – user3164187
是JTextComponents的子類 – mKorbel
我認爲OP正在尋找一個'JTextArea'密碼字段,而不是'JPasswordField'的'JTextField'密碼字段。 – ryvantage
使用JPasswordField類(JTextField的一個子類)爲密碼輸入提供專門的文本字段。
這裏的鏈接以供參考: http://docs.oracle.com/javase/tutorial/uiswing/components/passwordfield.html
我與setForeground方法可以嘗試設置字體顏色爲textarea的顏色,這使得文本看不見,但允許用戶複製和粘貼。
如果要針對文本的不同部分使用不同的屬性進行播放,則需要使用JTextPane。
閱讀有關Text Component Features的Swing教程的部分以獲取更多信息和工作示例。
您是否在尋找一個'JTextArea'作爲密碼而不是'JTextField'?爲什麼你要這個......? – ryvantage
@ryvantage它是一個像teraterm這樣的終端工具,其中我使用JtextArea進行所有GUI顯示。 – user3164187
如果您可以檢測到預期密碼的時間,您可以暫時將'DocumentFilter'應用於'JTextArea'的'Document'並捕獲所有輸入,而不會成爲文本區域顯示的一部分。 – predi