单击时清除JTextField内容

时间:2014-02-22 17:59:44

标签: java swing jtextfield

所以我已经构建了一个非常基本的Web浏览器 - 当用户点击它时,我正在拼命地删除地址栏的内容(JTextField),这里显示的是默认的一些文本。任何建议表示赞赏。

祝你有美好的一天!

我的代码

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Web_Browser extends JFrame {

    private final JTextField addressBar;
    private final JEditorPane display;

    // Constructor 
    public Web_Browser() {
        super("Web Browser");

        addressBar = new JTextField("Click & Type Web Address e.g. http://www.google.com");        
        addressBar.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent event) {
                           loadGo(event.getActionCommand());
                    }
                }
        );
        add(addressBar, BorderLayout.NORTH);
        display = new JEditorPane(); 
        display.setEditable(false);
        display.addHyperlinkListener(
                new HyperlinkListener(){
                    @Override
                    public void hyperlinkUpdate(HyperlinkEvent event){
                        if(event.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
                            loadGo(event.getURL().toString());
                        }
                    }
                }
        );
        add(new JScrollPane(display), BorderLayout.CENTER);
        setSize(500,300);
        setVisible(true);
    }

    // loadGo to sisplay on the screen
    private void loadGo(String userText) {
        try{
           display.setPage(userText);
           addressBar.setText(userText);
        }catch(IOException e){
            System.out.println("Invalid URL, try again");
        }
    }
}

3 个答案:

答案 0 :(得分:3)

使用FocusListener。在focusGained上,选择全部。

  addressBar.addFocusListener(new FocusAdapter() {
     @Override
     public void focusGained(FocusEvent e) {
        JTextComponent textComponent = (JTextComponent) e.getSource();
        textComponent.selectAll();
     }
  });

例如:

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.*;
import javax.swing.text.JTextComponent;

@SuppressWarnings("serial")
public class FocusExample extends JPanel {
   private static final int TF_COUNT = 5;
   private JTextField[] textFields = new JTextField[TF_COUNT];

   public FocusExample() {
      for (int i = 0; i < textFields.length; i++) {
         textFields[i] = new JTextField("Foo " + (i + 1), 10);
         textFields[i].addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
               JTextComponent textComponent = (JTextComponent) e.getSource();
               textComponent.selectAll();
            }
         });
         add(textFields[i]);
      }
   }

   private static void createAndShowGui() {
      FocusExample mainPanel = new FocusExample();

      JFrame frame = new JFrame("FocusExample");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

这使用户可以选择保留上一个文本,添加到上一个文本,或者通过键入简单地覆盖它。

答案 1 :(得分:1)

new JTextField("Click & Type Web Address e.g. http://www.google.com");  

也许你想要Text Prompt,它实际上并没有在文本字段中存储任何文本。它只是给用户提示文本字段的用途。

这是有益的,因此您不会生成DocumentEvents等,因为您实际上并未更改Document。

答案 2 :(得分:-1)

添加mouseListener而不是actionListener方法。

addressBar.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
    addressBar.setText("");
}