我用java Swing写了一个查找窗口,但是为什么查找的结果没有被标记
代码:publicclassLittleDialogextendsJFrame{privateJPanelpanel=null;privateJScrollPanejsp...
代码:public class LittleDialog extends JFrame {
private JPanel panel = null;
private JScrollPane jsp = null;
private JTextArea textarea = null;
private JPanel southPanel = null;
private JTextField text = null;
private JButton search = null;
private String fileName = null;
private String fileContent = null;
private UndoManager um = null;
public LittleDialog(String fileName, String fileContent) {
this.fileName = fileName;
this.fileContent = fileContent;
init();
}
private void init() {
this.setTitle(this.fileName);
// this.setModalityType(JDialog.DEFAULT_MODALITY_TYPE);
um = new UndoManager();
panel = new JPanel();
textarea = new JTextArea();
textarea.setText(fileContent);
textarea.setSelectionColor(Color.BLUE);
textarea.getDocument().addUndoableEditListener(um);
jsp = new JScrollPane(textarea) {
public void paint(Graphics g){
super.paint(g);
LittleDialog.this.repaint();
}
//不自动换行
public boolean getScrollableTracksViewportWidth(){
return false;
}
public void setSize(Dimension d){
if(d.width<getParent().getSize().width){
d.width=getParent().getSize().width;
}
d.width+=100;
super.setSize(d);
}
};
southPanel = new JPanel();
text = new JTextField(10);
search = new JButton("查找");
search.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// String word = text.getText().trim();
String word = "d";
if(!word.equals("") && word != null) {
int num = textarea.getCaretPosition();
int i = textarea.getText().indexOf(word, num);
if(i >= 0) {
textarea.setSelectionStart(i);
textarea.setSelectionEnd(i+word.length());
num = i;
} else {
num = 0;
int j = textarea.getText().indexOf(word, num);
if(j >= 0) {
textarea.setSelectionStart(j);
textarea.setSelectionEnd(j+word.length());
} else {
JOptionPane.showMessageDialog(null, "没有查找到"+word, "查找", JOptionPane.WARNING_MESSAGE);
return;
}
}
}
System.out.println(textarea.getSelectedText());
}
});
southPanel.setLayout(new FlowLayout());
southPanel.add(text);
southPanel.add(search);
panel.setLayout(new BorderLayout());
panel.add(jsp, BorderLayout.CENTER);
panel.add(southPanel, BorderLayout.SOUTH);
this.setContentPane(panel);
this.setLocationRelativeTo(null);
this.setLocation(200, 200);
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new LittleDialog("ss", "dsaoflsafdsaf");
}
} 展开
private JPanel panel = null;
private JScrollPane jsp = null;
private JTextArea textarea = null;
private JPanel southPanel = null;
private JTextField text = null;
private JButton search = null;
private String fileName = null;
private String fileContent = null;
private UndoManager um = null;
public LittleDialog(String fileName, String fileContent) {
this.fileName = fileName;
this.fileContent = fileContent;
init();
}
private void init() {
this.setTitle(this.fileName);
// this.setModalityType(JDialog.DEFAULT_MODALITY_TYPE);
um = new UndoManager();
panel = new JPanel();
textarea = new JTextArea();
textarea.setText(fileContent);
textarea.setSelectionColor(Color.BLUE);
textarea.getDocument().addUndoableEditListener(um);
jsp = new JScrollPane(textarea) {
public void paint(Graphics g){
super.paint(g);
LittleDialog.this.repaint();
}
//不自动换行
public boolean getScrollableTracksViewportWidth(){
return false;
}
public void setSize(Dimension d){
if(d.width<getParent().getSize().width){
d.width=getParent().getSize().width;
}
d.width+=100;
super.setSize(d);
}
};
southPanel = new JPanel();
text = new JTextField(10);
search = new JButton("查找");
search.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// String word = text.getText().trim();
String word = "d";
if(!word.equals("") && word != null) {
int num = textarea.getCaretPosition();
int i = textarea.getText().indexOf(word, num);
if(i >= 0) {
textarea.setSelectionStart(i);
textarea.setSelectionEnd(i+word.length());
num = i;
} else {
num = 0;
int j = textarea.getText().indexOf(word, num);
if(j >= 0) {
textarea.setSelectionStart(j);
textarea.setSelectionEnd(j+word.length());
} else {
JOptionPane.showMessageDialog(null, "没有查找到"+word, "查找", JOptionPane.WARNING_MESSAGE);
return;
}
}
}
System.out.println(textarea.getSelectedText());
}
});
southPanel.setLayout(new FlowLayout());
southPanel.add(text);
southPanel.add(search);
panel.setLayout(new BorderLayout());
panel.add(jsp, BorderLayout.CENTER);
panel.add(southPanel, BorderLayout.SOUTH);
this.setContentPane(panel);
this.setLocationRelativeTo(null);
this.setLocation(200, 200);
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new LittleDialog("ss", "dsaoflsafdsaf");
}
} 展开
1个回答
2016-09-11
展开全部
if(i >= 0) {
textarea.setSelectionStart(i);
textarea.setSelectionEnd(i+word.length());
textarea.requestFocus(); /////////////添加
num = i;
} else {
num = 0;
int j = textarea.getText().indexOf(word, num);
if(j >= 0) {
textarea.setSelectionStart(j);
textarea.setSelectionEnd(j+word.length());
textarea.requestFocus();/////////////添加
} else {
JOptionPane.showMessageDialog(null, "没有查找到"+word, "查找", JOptionPane.WARNING_MESSAGE);
return;
}
}
textarea.setSelectionStart(i);
textarea.setSelectionEnd(i+word.length());
textarea.requestFocus(); /////////////添加
num = i;
} else {
num = 0;
int j = textarea.getText().indexOf(word, num);
if(j >= 0) {
textarea.setSelectionStart(j);
textarea.setSelectionEnd(j+word.length());
textarea.requestFocus();/////////////添加
} else {
JOptionPane.showMessageDialog(null, "没有查找到"+word, "查找", JOptionPane.WARNING_MESSAGE);
return;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询