下面代码有什么问题么。为什么图标在托盘里显示的时候只显示图标的一小部分呢?
图标我都是用的QQ里的托盘图标来做的。图片应该没问题的public class MainJFrame extends JFrame
{
private static final long serialVersionUID = -1017125194683930243L;
private SystemTray tray = null; // 本操作系统托盘的实例
private TrayIcon trayIcon = null; // 托盘图标 public MainJFrame()
{
add(new ThePanel());
setJFrame(); // 基本设置
if (SystemTray.isSupported())
{
tray();//激活最小化
}
SocketServer socketServer = new SocketServer();
socketServer.execute();
}
// 最小化到托盘的程序
private void tray()
{
tray = SystemTray.getSystemTray(); // 获得本操作系统托盘的实例
ImageIcon icon = new ImageIcon("image/JavaCup.png"); // 将要显示到托盘中的图标
PopupMenu pop = new PopupMenu(); // 构造一个右键弹出式菜单
MenuItem show = new MenuItem("显示窗口");
MenuItem author = new MenuItem("作者信息");
MenuItem exit = new MenuItem("关闭服务");
pop.add(show);
pop.add(author);
pop.add(exit); trayIcon = new TrayIcon(icon.getImage(), "新生报到管理系统\n《服务端》程序\n双击左键显示窗口\n单击右键显示菜单\n\n作者:***", pop); addWindowListener(new WindowAdapter()
{
public void windowIconified(WindowEvent e)
{
try
{
tray.add(trayIcon); // 将托盘图标添加到系统的托盘实例中
setVisible(false); // 使窗口不可视
}
catch (AWTException ex)
{
ex.printStackTrace();
}
}
}); // 添加鼠标监听器,当鼠标在托盘图标上双击时,默认显示窗口
trayIcon.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2) // 鼠标双击
{
tray.remove(trayIcon); // 从系统的托盘实例中移除托盘图标
setVisible(true); // 显示窗口
setExtendedState(NORMAL); // 还原成原来的窗口,而不是显示在任务栏
}
}
}); show.addActionListener(new ActionListener() // 点击"显示窗口"菜单后将窗口显示出来
{
public void actionPerformed(ActionEvent e)
{
tray.remove(trayIcon); // 从系统的托盘实例中移除托盘图标
setVisible(true); // 显示窗口
setExtendedState(NORMAL);
}
}); exit.addActionListener(new ActionListener() // 点击"退出演示"菜单后退出程序
{
public void actionPerformed(ActionEvent e)
{
showConfirmMessage();
}
}); author.addActionListener(new ActionListener()//显示 关于作者
{
public void actionPerformed(ActionEvent e)
{
showMessage();
}
}); } private void showMessage()
{
JOptionPane.showMessageDialog(this, new JLabel("长春大学-计算机科学技术学院-科学与技术系-电算05404班-学号040540421"), "作者:曲金鑫", JOptionPane.INFORMATION_MESSAGE);
}
// 一些关于窗体的基本设置
private void setJFrame()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // 和系统的观感保持一致
}
catch (Exception e)
{
e.printStackTrace();
}
SwingUtilities.updateComponentTreeUI(this);
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screensize = kit.getScreenSize(); // 显示屏分辨率
int screenheight = screensize.height; // 屏幕的高
int screenwidth = screensize.width;// 屏幕的宽
setLocation((screenwidth-280) / 2, (screenheight -220)/2);// 设置显示的位置(x-距离最左的距离,y-距离最上的距离)
setSize(280, 220); // 设置窗体的宽和高
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置最大化,最小化
setResizable(false); // 设置是否允许用户改变窗体的大小
Image image = kit.getImage("image/JavaCup.png"); // 获得图像对象
setIconImage(image);
setTitle("新生报到管理软件--服务端"); // 设置标题
setVisible(true); // 设置为可显示
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
showConfirmMessage();
}
});
} private void showConfirmMessage()
{
int i = JOptionPane.showConfirmDialog(this, "你确定退出新生报到系统服务端程序吗?\n如果退出,客户端将与数据库断开连接!", "退出提示对话框", JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if (i == 0)
{
System.exit(0);
}
else
{
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); // 使关闭窗口有效,这句是很关键的(找的老子好辛苦)。
}
}
}class ThePanel extends JPanel
{
private static final long serialVersionUID = 1L; ThePanel()
{
JLabel label=new JLabel("服务端程序启动成功......");
JLabel labe2=new JLabel("请确保此程序打开");
JLabel labe3=new JLabel("以保证客户端进行正常操作...");
JLabel labe4=new JLabel("您可以将窗口最小化于托盘中");
label.setForeground(Color.red);
labe4.setForeground(Color.blue);
setLayout(null);
label.setBounds(50, 20, 200, 22);
labe2.setBounds(50, 70, 200, 22);
labe3.setBounds(50, 90, 200, 22);
labe4.setBounds(50, 130, 200, 22);
add(label);
add(labe2);
add(labe3);
add(labe4);
}
}