這是部分代碼
else if(e.getActionCommand().equals("查找")){
wb=wby.getText();
System.out.print(wb);//這裡打印的是我在文本域中輸入的文字
new Way(1);//這是一個對話框,裡面有“開始”這個按鈕
}else if(e.getActionCommand().equals("開始")||e.getActionCommand().equals("下一個")){
System.out.print(wb);//這裡的wb卻為null,報錯,出現空指針問題。把wb=wby.getText();放進來也同樣
String temp=wbk.getText();
int s=wb.indexOf(temp,start);//這行出現空指針,因為wb為null。
if(wb.indexOf(temp,start)!=-1){
wby.setSelectionStart(s);
wby.setSelectionEnd(s+temp.length());
wby.setSelectedTextColor(Color.GREEN);
start=s+1;
an10.setText("下一個");
//value=value.substring(s+temp.length());//不能截取字串
}else {
JOptionPane.showMessageDialog(this, "查找完畢!", "提示", 0);
this.dispose();
}
我想問的是,“開始”裡的wb為什麼會是null?我很不解,怎麼樣把”查找“裡的的wb放進”開始“裡呢?
我寫了一個測試的樣例,你看下。可以在文本域中寫入文本進行查找。啟動後,在輸入框中填入要查找的內容,然後點擊開始按鈕。
/**
*
*/
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JTextArea;
/**
* @author jiaoqishun 2015-6-5 下午4:25:44
*/
public class FindText extends JFrame {
private JTextField txt_Search;
public FindText() {
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[] { 0, 0, 0, 0 };
gbl_panel.rowHeights = new int[] { 0, 0, 0 };
gbl_panel.columnWeights = new double[] { 1.0, 1.0, 0.0,
Double.MIN_VALUE };
gbl_panel.rowWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
panel.setLayout(gbl_panel);
JLabel lbl_Search = new JLabel(
"\u8F93\u5165\u8981\u67E5\u627E\u7684\u5185\u5BB9\uFF1A");
GridBagConstraints gbc_lbl_Search = new GridBagConstraints();
gbc_lbl_Search.insets = new Insets(0, 0, 5, 5);
gbc_lbl_Search.anchor = GridBagConstraints.EAST;
gbc_lbl_Search.gridx = 0;
gbc_lbl_Search.gridy = 0;
panel.add(lbl_Search, gbc_lbl_Search);
txt_Search = new JTextField();
GridBagConstraints gbc_txt_Search = new GridBagConstraints();
gbc_txt_Search.insets = new Insets(0, 0, 5, 5);
gbc_txt_Search.fill = GridBagConstraints.HORIZONTAL;
gbc_txt_Search.gridx = 1;
gbc_txt_Search.gridy = 0;
panel.add(txt_Search, gbc_txt_Search);
txt_Search.setColumns(10);
JButton btn_Search = new JButton("\u5F00\u59CB");
GridBagConstraints gbc_btn_Search = new GridBagConstraints();
gbc_btn_Search.insets = new Insets(0, 0, 5, 0);
gbc_btn_Search.gridx = 2;
gbc_btn_Search.gridy = 0;
panel.add(btn_Search, gbc_btn_Search);
final JTextArea textArea = new JTextArea();
GridBagConstraints gbc_textArea = new GridBagConstraints();
gbc_textArea.gridwidth = 3;
gbc_textArea.insets = new Insets(0, 0, 0, 5);
gbc_textArea.fill = GridBagConstraints.BOTH;
gbc_textArea.gridx = 0;
gbc_textArea.gridy = 1;
panel.add(textArea, gbc_textArea);
setTextAreaContent(textArea);
btn_Search.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String findStr = txt_Search.getText();
if ("".equals(findStr)) {
JOptionPane.showMessageDialog(null, "查找文本為空,請重新輸入。");
return;
} else {
findText(findStr, textArea);
}
}
});
}
/**
* @param findStr
* @param textArea
*/
protected void findText(String findStr, JTextArea textArea) {
String textAreaText = textArea.getText();
if (textAreaText == null || "".equals(textAreaText)) {
JOptionPane.showMessageDialog(null, "文本域內容為空,請重新輸入。");
return;
} else {
int fromIndex = 0;
int count = 0;
int index = textAreaText.indexOf(findStr, fromIndex);
while(index>=0){
count++;
index = textAreaText.indexOf(findStr, index+1);
}
if (count > 0) {
JOptionPane.showMessageDialog(null, "查找成功,共找到"+count+"處。");
}else{
JOptionPane.showMessageDialog(null, "文本域中不存在該詞匯。");
}
}
}
/**
* 設置JTextArea中的內容
*/
private void setTextAreaContent(JTextArea textArea) {
textArea.setText("白日依山盡,\r\n黃河入海流;\r\n欲窮千裡目,\r\n更上一層樓");
}
public static void main(String[] args) {
FindText findText = new FindText();
findText.setTitle("查找文本樣例");
findText.setSize(300, 300);
findText.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
findText.setLocationRelativeTo(null);
findText.setVisible(true);
}
}