如何让窗口总在最前面?

aurain 2004-07-10 03:05:10
1. 在XP中,当开了很多窗口,我想让JFrame总是在最前面,请问如何实现?
2. 我想做一个用鼠标抓图的程序,请问在Java中有什么相关的类呢?
...全文
496 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunheart 2005-03-18
  • 打赏
  • 举报
回复
好贴
华生豆 2004-07-10
  • 打赏
  • 举报
回复
Frame.setAlwaysOnTop(true);

这样就可以让Frame总在最前面了~~~
shuneng 2004-07-10
  • 打赏
  • 举报
回复
mark
IMarksman 2004-07-10
  • 打赏
  • 举报
回复
用jni的实现方式可参考这个帖子
http://forum.java.sun.com/thread.jsp?forum=57&thread=121761&start=0&range=15&hilite=false&q=
IMarksman 2004-07-10
  • 打赏
  • 举报
回复
两中解决方案,第一,采用jni调用windows api
第二、用纯java,但效果不太好,下面是纯java的实现的代码,可以直接编译运行
-----------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Notifier extends JFrame{
public JList list;
public DefaultListModel listModel;
public JScrollPane jsp;
public GridBagConstraints gbc;
public GridBagLayout gbl;
public Container c;
public MyWindow myWindow;
public JFrame frame;
public Notifier(){
listModel = new DefaultListModel();
list = new JList(listModel);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for (int j = 0; j < gs.length; j++) {
GraphicsDevice gd = gs[j];
if (gd.isFullScreenSupported()) {
if (gd.getType() == gd.TYPE_RASTER_SCREEN) {
frame = new JFrame();
frame.setUndecorated(true);
frame.setDefaultLookAndFeelDecorated(false);
frame.setVisible(false);
gd.setFullScreenWindow(frame);
break;
}
}
}
if(frame != null) frame.setVisible(false);
}

public void makeWindow(){
if(frame == null) return;
try{
list.removeMouseListener(myWindow);
list.removeMouseMotionListener(myWindow);
}catch(Exception e){}
myWindow = new MyWindow(frame);
frame.setVisible(false);
myWindow.setup();
}
public static void main(String[] args){
Notifier nf = new Notifier();
nf.makeWindow();

}
public class MyWindow extends JWindow implements MouseListener,
MouseMotionListener{
public JButton closeButton;
public Point offset;
public MyWindow(JFrame frame){
super(frame);
}
public void setup(){
setVisible(true);
setLocation(300,100);
setSize(300, 300);
gbc = new GridBagConstraints();
gbl = new GridBagLayout();
c = this.getContentPane();
c.setLayout(new BorderLayout());
this.setVisible(true);
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.setBorder(BorderFactory.createLineBorder(new Color(0,0,0),2));
p.addMouseListener(this);
p.addMouseMotionListener(this);
list.addMouseListener(this);
list.addMouseMotionListener(this);
jsp = new JScrollPane(list);
p.add(jsp, BorderLayout.CENTER);
c.add(p, BorderLayout.CENTER);
p.validate();
c.validate();
repaint();
}

public void mouseClicked(java.awt.event.MouseEvent mouseEvent) { }
public void mouseEntered(java.awt.event.MouseEvent mouseEvent) { }
public void mouseExited(java.awt.event.MouseEvent mouseEvent) { }
public void mousePressed(java.awt.event.MouseEvent mouseEvent) {
if(SwingUtilities.isLeftMouseButton(mouseEvent)){
int count = mouseEvent.getClickCount();
if(count == 1){
offset = new Point(mouseEvent.getX(), mouseEvent.getY());
}else if(count == 2){
int i = list.locationToIndex(new Point(mouseEvent.getX(),
mouseEvent.getY()));
if(i >= 0 && i < listModel.size()){
String s = (String)listModel.elementAt(i);
}else return;
if(c != null) ;
}
}else if(SwingUtilities.isRightMouseButton(mouseEvent)){
Vector v = new Vector();
v.add("Close");
ClickMenu menu = new ClickMenu(v, mouseEvent);
menu.show(mouseEvent.getComponent(), mouseEvent.getX(),
mouseEvent.getY());
}
}
public void mouseReleased(java.awt.event.MouseEvent mouseEvent) {
offset = null;
}
public void mouseDragged(java.awt.event.MouseEvent mouseEvent) {
if(SwingUtilities.isLeftMouseButton(mouseEvent)){
myWindow.setLocation((int)(myWindow.getX() + mouseEvent.getX() -
offset.getX()), (int)(myWindow.getY() + mouseEvent.getY() - offset.getY()));
c.repaint();
}
}
public void mouseMoved(java.awt.event.MouseEvent mouseEvent) { }
}
public void addToList(String s){
if(listModel.contains(s)) return;
listModel.add(0, s);
list.revalidate();
list.repaint();
}
public void removeFromList(String s){
listModel.removeElement(s);
list.revalidate();
list.repaint();
}
public String getFirst(){
if(listModel.size() > 0);
return (String)listModel.get(0);
}
public class ClickMenu extends JPopupMenu implements ActionListener {
MouseEvent e;
public ClickMenu(Vector menu, MouseEvent e){
this.e = e;
JMenuItem tmpItem;
for(int x=0;x<menu.size();x++){
add(tmpItem = new JMenuItem((String)menu.elementAt(x)));
tmpItem.addActionListener(this);
}
}
public void actionPerformed(java.awt.event.ActionEvent actionEvent){
String command = actionEvent.getActionCommand();
if(command.equals("Close")){
myWindow.setVisible(false);
System.exit(0);
}

}
}
}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧