2012-12-07 20 views
4

我正在嘗試使用圖像和提示創建JTextField。文本字段的功能是搜索某些書籍的搜索字段。現在,我想更進一步。我想給這個圖像一個功能。例如,如果我點擊圖片,文本框中的文本應該被清除。單擊JTextField中的圖標並清除其內容

爲了實現這個實現,我創建了一個新類並使用JTextField對其進行了擴展。

這是代碼:

public class JSearchTextField extends JTextField implements FocusListener { 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
private String textWhenNotFocused; 
private Icon icon; 
private Insets dummyInsets; 
private JTextField dummy; 

public JSearchTextField() { 
    super(); 

    Border border = UIManager.getBorder("TextField.border"); 
    dummy = new JTextField("Suchen..."); 
    this.dummyInsets = border.getBorderInsets(dummy); 

    icon = new ImageIcon(JSearchTextField.class.getResource("/images/clearsearch.png")); 
    this.addFocusListener(this); 

} 

public JSearchTextField(String textWhenNotFocused) { 
    this(); 
    this.textWhenNotFocused = textWhenNotFocused; 
} 

public void setIcon(ImageIcon newIcon){ 
    this.icon = newIcon; 
} 

public String getTextWhenNotFocused() { 
    return this.textWhenNotFocused; 
} 

public void setTextWhenNotFocused(String newText) { 
    this.textWhenNotFocused = newText; 
} 

public void paintComponent(Graphics g){ 
    super.paintComponent(g); 

    int textX = 2; 

    if(!this.hasFocus() && this.getText().equals("")) { 
     int height = this.getHeight(); 
     Font prev = this.getFont(); 
     Font italic = prev.deriveFont(Font.ITALIC); 
     Color prevColor = g.getColor(); 
     g.setFont(italic); 
     g.setColor(UIManager.getColor("textInactiveText")); 
     int h = g.getFontMetrics().getHeight(); 
     int textBottom = (height - h)/2 + h - 4; 
     int x = this.getInsets().left; 
     Graphics2D g2d = (Graphics2D) g; 
     RenderingHints hints = g2d.getRenderingHints(); 
     g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 
          RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 
     g2d.drawString(textWhenNotFocused, x, textBottom); 
     g2d.setRenderingHints(hints); 
     g.setFont(prev); 
     g.setColor(prevColor); 
    } else { 
     int iconWidth = icon.getIconWidth(); 
     int iconHeight = icon.getIconHeight(); 
     int x = dummy.getWidth() + dummyInsets.right; 
     textX = x - 420; 
     int y = (this.getHeight() - iconHeight)/2; 
     icon.paintIcon(this, g, x, y); 
    } 

    setMargin(new Insets(2, textX, 2, 2)); 

} 
@Override 
public void focusGained(FocusEvent arg0) { 
    this.repaint(); 
} 

@Override 
public void focusLost(FocusEvent arg0) { 
    this.repaint(); 
} 

}

而這正是我創作的領域;

txtSearchBooks = new JSearchTextField("Buch suchen..."); 

現在回到我的問題。你有什麼想法,我可以給圖像一個功能,文本將被自動清除?我試圖實現一個MouseListener並將「txtSearchBooks」的文本設置爲null,但它沒有奏效。

我希望我沒有走錯方向。

對不起,很長的文章,但我真的很感激得到一些建議。

回答

6

A JTextFieldJComponent,這意味着它也是其他組件的容器。您可以使用add(Component c)方法向其中添加其他組件。但A JTextField將不會顯示其添加的組件,除非您提供LayoutManager。然後它的行爲就像一個正常的JPanel

我做了一個小例子,你如何管理你所需要的。標籤顯示在右側,點擊它將清除該字段。您也可以使用按鈕,而不是標籤。

請注意,您不需要像我一樣從頭創建Image對象,您可以從文件加載它。我以這種方式創建它,以便該示例不依賴於其他文件。

public class TextFieldWithLabel { 
    public static void main(String[] args) 
    { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     final JTextField textField = new JTextField("Search..."); 
     textField.setLayout(new BorderLayout()); 

     //creating dummy image... 
     Image image = new BufferedImage(25, 25, BufferedImage.TYPE_INT_RGB); 
     Graphics graphics = image.getGraphics(); 
     graphics.setColor(Color.WHITE); 
     graphics.fillRect(0, 0, 25, 25); 
     graphics.setColor(Color.RED); 
     graphics.fillRect(2, 11, 21, 3); 
     graphics.fillRect(11, 2, 3, 21); 

     JLabel label = new JLabel(new ImageIcon(image)); 
     textField.add(label, BorderLayout.EAST); 
     label.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mouseClicked(MouseEvent e) { 
       textField.setText(""); 
      } 
     }); 
     frame.add(textField); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 
+0

+1 for oop answer – Jimmt

+0

感謝您的幫助!它已經工作了!你做了我的一天:) – user1885888

+0

@ user1885888不要忘記在允許的情況下將此答覆標記爲接受。 –