Khi sử dụng JTextFields tôi muốn đặt văn bản mặc định.Bỏ chọn lựa chọn mặc định trên JTextfield
Sau đó, tôi chạy chương trình và văn bản mặc định này sẽ tự động được chọn (ít nhất là khi bạn chỉ có một trường). Nói cách khác, nếu tôi nhập một chữ cái ngay lập tức, văn bản mặc định sẽ bị xóa và thay thế bằng văn bản mới.
Câu hỏi của tôi là cách tôi có thể thay đổi cài đặt mặc định theo cách cho phép tôi nhập một chữ mà không xóa văn bản mặc định? Tôi muốn chữ cái vừa được thêm vào cuối văn bản mặc định.
Dưới đây là mã của tôi:
public class ButtonsNText extends JPanel implements ActionListener, KeyListener {
private JTextField TextLine;
private JToggleButton UpperCaseButton;
private JToggleButton LowerCaseButton;
private JCheckBox ContinuousButton;
private ButtonGroup myButtonGroup;
public ButtonsNText(){
TextLine = new JTextField(10);
add(TextLine); TextLine.setName("TextLine");
TextLine.setText("default text");
TextLine.setCaretPosition(TextLine.getText().length());
TextLine.addKeyListener(this);
myButtonGroup = new ButtonGroup();
UpperCaseButton = new JToggleButton("Upper case");
add(UpperCaseButton);UpperCaseButton.setName("UpperCaseButton");
LowerCaseButton = new JToggleButton("Lower case");
add(LowerCaseButton); LowerCaseButton.setName("LowerCaseButton");
ContinuousButton = new JCheckBox("Continuous?");
add(ContinuousButton); ContinuousButton.setName("ContinuousButton");
myButtonGroup.add(UpperCaseButton); myButtonGroup.add(LowerCaseButton);
UpperCaseButton.addActionListener(this);
LowerCaseButton.addActionListener(this);
ContinuousButton.addActionListener(this);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Hello world example");
frame.add(new ButtonsNText());
frame.pack();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == UpperCaseButton){
TextLine.setText(TextLine.getText().toUpperCase());
}
else if(e.getSource() == LowerCaseButton){
TextLine.setText(TextLine.getText().toLowerCase());
}
}
@Override
public void keyReleased(KeyEvent k) {
if(ContinuousButton.isSelected()){
if(UpperCaseButton.isSelected()){
int pos = TextLine.getCaretPosition();
TextLine.setText(TextLine.getText().toUpperCase());
TextLine.setCaretPosition(pos);
}
else if(LowerCaseButton.isSelected()){
int pos = TextLine.getCaretPosition();
TextLine.setText(TextLine.getText().toLowerCase());
TextLine.setCaretPosition(pos);
}
}
int key = k.getKeyCode();
if(key == KeyEvent.VK_ENTER){
if(UpperCaseButton.isSelected()){
TextLine.setText(TextLine.getText().toUpperCase());
}
else if(LowerCaseButton.isSelected()){
TextLine.setText(TextLine.getText().toLowerCase());
}
}
}
}
Tôi đã thử những thứ như isFocusable(), setFocusable(), setCaterPosition() và phương pháp tương tự khác, nhưng ở đây tôi nghĩ mình cần một cách tiếp cận khác nhau .
Để được trợ giúp tốt hơn sớm hơn, gửi một [SSCCE] (http://sscce.org/). –
hãy tìm hiểu các quy ước đặt tên java và gắn bó với chúng – kleopatra
... và _don't_ sử dụng keylisteners – kleopatra