java 如何隐藏任务栏图标

sparadise1003 2011-11-14 04:46:01


我用JFrame做了一个类似QQ消息提示的窗口,但是任务栏中一直有一个图标显示着,怎么能将这个图标隐藏掉只剩一个窗口呢?
...全文
1218 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sparadise1003 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 huntor 的回复:]

JFrame/JDialog/JWindow中JWindow不会出现在任务栏。不过你需要自己提供关闭的途径、也可以使用定时器关闭。

Java code
final JWindow messagebox = new JWindow();
....
final ActionListener action = new ActionListener(){
public void ac……
[/Quote]

我就想实现上图中俄窗口形式,这个是用JWindow做的吗?
huntor 2011-11-15
  • 打赏
  • 举报
回复
JFrame/JDialog/JWindow中JWindow不会出现在任务栏。不过你需要自己提供关闭的途径、也可以使用定时器关闭。

final JWindow messagebox = new JWindow();
....
final ActionListener action = new ActionListener(){
public void actionPerformed(ActionEvent e){
messagebox.dispose();
}
};
final Timer timer = new Timer(TimeUnit.MINUTES.toMillis(3),action);
timer.setRepeat(false);
messagebox.addWindowListener(new WindowAdapter(){
public void windowOpened(WindowEvent e){
timer.start();
}
});
sparadise1003 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 huntor 的回复:]

使用 JWindow
[/Quote]

是不是说,这个窗体不用JFrame,用JWindow来实现?
alansede 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 sparadise1003 的回复:]
这是我再网上找到的一段代码,可以实现我想要的功能,用的是JDialog,我感觉很不错和大家分享一下。

Java code

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayou……
[/Quote]
我想问一下那么多的import都是要一一记住的吗?还只可以查到啊?
sparadise1003 2011-11-15
  • 打赏
  • 举报
回复
这是我再网上找到的一段代码,可以实现我想要的功能,用的是JDialog,我感觉很不错和大家分享一下。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import java.awt.Insets;
import java.awt.Toolkit;
import javax.swing.JDialog;

public class VersionUtil{
private Map<String, String> feaMap = null;
private Point oldP;//上一次坐标,拖动窗口时用
private TipWindow tw = null;//提示框
private ImageIcon img = null;//图像组件
private JLabel imgLabel = null; //背景图片标签
private JPanel headPan = null;
private JPanel feaPan =null;
private JPanel btnPan = null;
private JLabel title = null;
private JLabel head = null;
private JLabel close = null;//关闭按钮
private JTextArea feature = null;
private JScrollPane jfeaPan = null;
private JLabel releaseLabel = null;
private JLabel sure = null;
private SimpleDateFormat sdf=null;

{
sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
feaMap = new HashMap<String, String>();
feaMap.put("name", "中国信息大学固定资产管理系统");
feaMap.put("release", sdf.format(new Date()));
feaMap.put("feature", "1.开发环境:windows\n2.开发语言:java\n3.开发工具:Eclipse3.2\n4.数据库类型:SQL Server2005\n5.开发人员:花新昌\n6.联系方式:15210477080");
}

public VersionUtil() {
init();
handle();
tw.setAlwaysOnTop(true);
tw.setUndecorated(true);
tw.setResizable(false);
tw.setVisible(true);
tw.run();
}
public void init(){
//新建300x220的消息提示框
tw = new TipWindow(300, 220);
img = new ImageIcon("background.gif");
imgLabel = new JLabel(img);
//设置各个面板的布局以及面板中控件的边界
headPan = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
feaPan = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
btnPan = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
title = new JLabel("欢迎使用本系统");
head = new JLabel(feaMap.get("name"));
close = new JLabel(" x");//关闭按钮
feature = new JTextArea(feaMap.get("feature"));
jfeaPan = new JScrollPane(feature);
releaseLabel = new JLabel("登录 " + feaMap.get("release"));
sure = new JLabel("确定");
sure.setHorizontalAlignment(SwingConstants.CENTER);

// 将各个面板设置为透明,否则看不到背景图片
((JPanel) tw.getContentPane()).setOpaque(false);
headPan.setOpaque(false);
feaPan.setOpaque(false);
btnPan.setOpaque(false);

//设置JDialog的整个背景图片
tw.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
headPan.setPreferredSize(new Dimension(300, 60));

//设置提示框的边框,宽度和颜色
tw.getRootPane().setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.gray));

title.setPreferredSize(new Dimension(260, 26));
title.setVerticalTextPosition(JLabel.CENTER);
title.setHorizontalTextPosition(JLabel.CENTER);
title.setFont(new Font("宋体", Font.PLAIN, 12));
title.setForeground(Color.black);


close.setFont(new Font("Arial", Font.BOLD, 15));
close.setPreferredSize(new Dimension(20, 20));
close.setVerticalTextPosition(JLabel.CENTER);
close.setHorizontalTextPosition(JLabel.CENTER);
close.setCursor(new Cursor(12));
close.setToolTipText("关闭");


head.setPreferredSize(new Dimension(250, 35));
head.setVerticalTextPosition(JLabel.CENTER);
head.setHorizontalTextPosition(JLabel.CENTER);
head.setFont(new Font("宋体", Font.PLAIN, 12));
head.setForeground(Color.blue);


feature.setEditable(false);
feature.setForeground(Color.red);
feature.setFont(new Font("宋体", Font.PLAIN, 13));
feature.setBackground(new Color(184, 230, 172));
//设置文本域自动换行
feature.setLineWrap(true);

jfeaPan.setPreferredSize(new Dimension(250, 80));
jfeaPan.setBorder(null);
jfeaPan.setBackground(Color.black);

releaseLabel.setForeground(Color.DARK_GRAY);
releaseLabel.setFont(new Font("宋体", Font.PLAIN, 12));

//为了隐藏文本域,加个空的JLabel将他挤到下面去
JLabel jsp = new JLabel();
jsp.setPreferredSize(new Dimension(300, 25));

sure.setPreferredSize(new Dimension(110, 30));
//设置标签鼠标手形
sure.setCursor(new Cursor(12));

headPan.add(title);
headPan.add(close);
headPan.add(head);

feaPan.add(jsp);
feaPan.add(jfeaPan);
feaPan.add(releaseLabel);

btnPan.add(sure);

tw.add(headPan, BorderLayout.NORTH);
tw.add(feaPan, BorderLayout.CENTER);
tw.add(btnPan, BorderLayout.SOUTH);
}

public void handle() {
//为更新按钮增加相应的事件
sure.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(tw, "谢谢,再见");
tw.close();
}
public void mouseEntered(MouseEvent e) {
sure.setBorder(BorderFactory.createLineBorder(Color.gray));
}
public void mouseExited(MouseEvent e) {
sure.setBorder(null);
}
});
//增加鼠标拖动事件
title.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
Point newP = new Point(e.getXOnScreen(), e.getYOnScreen());
int x = tw.getX() + (newP.x - oldP.x);
int y = tw.getY() + (newP.y - oldP.y);
tw.setLocation(x, y);
oldP=newP;

}
});
//鼠标按下时初始坐标,供拖动时计算用
title.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
oldP = new Point(e.getXOnScreen(), e.getYOnScreen());
}
});

//右上角关闭按钮事件
close.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
tw.close();
}

public void mouseEntered(MouseEvent e) {
close.setBorder(BorderFactory.createLineBorder(Color.gray));
}

public void mouseExited(MouseEvent e) {
close.setBorder(null);
}
});

}
public static void main(String args[]) {
new VersionUtil();
}
}
class TipWindow extends JDialog{
private static final long serialVersionUID = 8541659783234673950L;
private static Dimension dim;
private int x, y;
private int width, height;
private static Insets screenInsets;


public TipWindow(int width,int height){
this.width=width;
this.height=height;
dim = Toolkit.getDefaultToolkit().getScreenSize();
screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration());
x = (int) (dim.getWidth() - width-3);
y = (int) (dim.getHeight()-screenInsets.bottom-3);
initComponents();
}
public void run() {
for (int i = 0; i <= height; i += 10) {
try {
this.setLocation(x, y - i);
Thread.sleep(5);
} catch (InterruptedException ex) {
}
}
//此处代码用来实现让消息提示框5秒后自动消失
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
close();
}
private void initComponents() {
this.setSize(width, height);
this.setLocation(x, y);
this.setBackground(Color.black);
}
public void close(){
x=this.getX();
y=this.getY();
int ybottom=(int)dim.getHeight()-screenInsets.bottom;
for (int i = 0; i <= ybottom-y; i += 10) {
try {
setLocation(x, y+i);
Thread.sleep(5);
} catch (InterruptedException ex) {
}
}
dispose();
}
}


huntor 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 sparadise1003 的回复:]

我需要对弹出的窗口进行编辑,例如:加文字,图片,就像QQ的消息提醒窗口,用JWindow可以做吗?
[/Quote]
可以
只不过JWindow没有上面的标题栏,需要你自己提供关闭窗口的途径。
huntor 2011-11-15
  • 打赏
  • 举报
回复
JWindow 就是一没有标题栏、边框的顶层容器
lliiqiang 2011-11-15
  • 打赏
  • 举报
回复
InternalFrame是可以嵌套的内部窗体
sparadise1003 2011-11-15
  • 打赏
  • 举报
回复
我需要对弹出的窗口进行编辑,例如:加文字,图片,就像QQ的消息提醒窗口,用JWindow可以做吗?
huntor 2011-11-14
  • 打赏
  • 举报
回复
使用 JWindow

62,634

社区成员

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

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