如何实现跟win7搜索窗口一样的效果? 输入框获得焦点后在输入框下方弹出一个子窗口?

穆云 发布于 2013/01/29 16:57
阅读 772
收藏 0

详细代码如下,应该怎样修改焦点事件部分的代码呢?请教大家



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.text.StyledDocument;


public class Frame1 extends JFrame {


private static final long serialVersionUID = 8550120897650701329L;
JPanel panel1 = null;
JPanel panel2 = null;
JButton btn_back = null;
JButton btn_forward = null;
JButton btn_search = null;
JTextField text_hunt = null;

Box box = null;               //放输入组件的容器
JFrame popfrm = null;

public Frame1(){

this.setTitle("搜索");
this.setSize(new Dimension(600,450));
this.setBackground(Color.WHITE);
btn_back = new JButton("后退");
btn_forward = new JButton("前进");
final String tips = "请输入搜索对象";
text_hunt = new JTextField(tips);
text_hunt.setEditable(true);
popfrm = new JFrame();
JTextField textField = new JTextField("添加搜索筛选器");
textField.setEditable(false);
JButton[] btns = new JButton[5];
btns[0] = new JButton("种类:");
btns[1] = new JButton("修改日期:");
btns[2] = new JButton("类型:");
btns[3] = new JButton("大小:");
btns[4] = new JButton("名称:");

Box box = Box.createVerticalBox();
Box box1 = Box.createHorizontalBox();
Box box2 = Box.createHorizontalBox();
box.add(box1);
box.add(box2);
box1.add(textField);
for(int i=0;i<5;i++){
box2.add(btns[i]);
}
popfrm.getContentPane().add(box);
popfrm.setUndecorated(true);//设置无标题栏
//设置选择框位于文本框下方
popfrm.setLocationRelativeTo(text_hunt);
popfrm.pack();
popfrm.setVisible(false);
text_hunt.addFocusListener(new FocusAdapter() {

@Override
public void focusLost(FocusEvent e) {//失去焦点时隐藏选择框

}

@Override
//获得焦点时,如果文本域没有搜索对象,则弹出一个选择框
public void focusGained(FocusEvent e) {
}
});
btn_search = new JButton("搜索");

box = Box.createVerticalBox();      //窗口呈竖结构
Box box_1 = Box.createHorizontalBox();//菜单栏呈横结构
Box box_2 = Box.createVerticalBox();  //显示域呈竖结构
box.add(box_1);
box.add(Box.createVerticalStrut(4));//两行的间距
box.add(box_2);

//为搜索栏添加组件
box_1.add(btn_back);
box_1.add(btn_forward);
box_1.add(text_hunt);
box_1.add(btn_search);

this.getContentPane().add(box,BorderLayout.NORTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

}


加载中
返回顶部
顶部