2013-07-22 83 views
1

我寫了一個java程序,它有一個CardLayoutJPanel爲每張卡。搜索欄JPanel文本

JPanel包含長文本,我只需要實現一個小「搜索欄」,只是搜索JPanel查看指定的文本。

它只是要突出搜索文本裏面的JPanel

爲了使它更清晰,我正在討論Firefox的搜索欄(ctrl-f)的Chrome瀏覽器,但在我的程序中僅適用於一個JPanel

這是一張圖片。

enter image description here

這可能嗎?我怎樣才能做到這一點?

感謝

回答

1

最簡單的方法是創建一個小的搜索JPanel,其定位文本JPanel以上。兩個JPanel s將在另一個JPanel之內。我會使用JTextFieldJButton,因爲我喜歡用戶控制何時搜索。

下面是我的一個GUI搜索JPanel

Cobol Paragraph Structure

下面是創建搜索JPanel的代碼。請注意,我使用了JPanel。當您想覆蓋某個組件方法時,只能擴展Swing組件。

package gov.bop.cobol.paragraph.structure.view; 

import gov.bop.cobol.paragraph.structure.model.ParagraphStructureModel; 
import gov.bop.cobol.paragraph.structure.thread.SearchActionThread; 

import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.BorderFactory; 
import javax.swing.Box; 
import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class SearchPanel { 

    protected JPanel     panel; 

    protected JTextField    findTextField; 

    protected ParagraphStructureFrame frame; 

    protected ParagraphStructureModel model; 

    public SearchPanel(ParagraphStructureFrame frame, 
      ParagraphStructureModel model) { 
     this.frame = frame; 
     this.model = model; 
     createPartControl(); 
    } 

    protected void createPartControl() { 
     panel = new JPanel(); 
     panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); 
     panel.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); 

     JLabel findLabel = new JLabel("Search:"); 
     panel.add(findLabel); 

     panel.add(Box.createRigidArea(new Dimension(6, 0))); 

     findTextField = new JTextField(30); 
     panel.add(findTextField); 

     panel.add(Box.createRigidArea(new Dimension(6, 0))); 

     JButton findButton = new JButton("Search"); 
     findButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent event) { 
       String s = findTextField.getText().toUpperCase().trim(); 
       if (!s.equals("")) { 
        findTextField.setText(s); 
        Thread thread = new Thread(new SearchActionThread(frame, 
          model, s)); 
        thread.start(); 
       } 
      } 
     }); 
     panel.add(findButton); 
    } 

    public JPanel getPanel() { 
     return panel; 
    } 

}