java托盘开发界面记录

设置对话框为圆角:
//首先选择设置
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
//然后设置,其中login为登陆对话框
AWTUtilities.setWindowShape(login, new RoundRectangle2D.Double(0.0D,0.0D,login.getWidth(),login.getHeight(),15.0D,15.0D));

对话框居中:
//获取屏幕大小
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
//设置位置,WIDTH、HEIGHT分别为对话框的宽高
int HEIGHT=200;
int WIDTH=300;
setLocation(screenSize.width/2-WIDTH/2, screenSize.height/2-HEIGHT/2);

初始化系统托盘:
//系统托盘
 private SystemTray tray = null;
//托盘图标
 private TrayIcon trayIcon = null;
 //托盘图标图像
 private Image icon = null;
 //图标图像路径
 private final String ICON_PATH = "icons/app.png";
//系统名称
 private String SYSTEM_NAME = "提醒助手";

tray = SystemTray.getSystemTray();
ImageIcon imageIcon = new ImageIcon(this.getClass().getClassLoader().getResource(ICON_PATH));  //获得托盘显示图标
icon=imageIcon.getImage(); //获得Image对象
   
trayIcon=new TrayIcon(icon,SYSTEM_NAME,trayMenu);
trayIcon.setImageAutoSize(true); 
   
trayIcon.setToolTip(SYSTEM_NAME);

try{
    tray.add(trayIcon);
   }catch(AWTException e){
    System.out.println("托盘初始化出现故障:" + e.getMessage());
    e.printStackTrace();
   }


消息提醒框显示在右下角:
    private JFrame jf;
    //JPanel
    private JPanel jp;
    //超链接
    private LinkLabel lb; 
    //控制窗口弹出效果的参数
    private Integer screenWidth;  // 屏幕宽度
    private Integer screenHeight; // 屏幕高度
    private Integer windowWidth = 300; // 设置提示窗口宽度
    private Integer windowHeight = 150; // 设置提示窗口高度
    private Integer bottmToolKitHeight; // 底部任务栏高度,如果没有任务栏则为零
    private Integer stayTime = 5000; // 提示框停留时间
    private Integer x; // 窗口起始X坐标
    private Integer y; // 窗口起始Y坐标

  //控制窗口弹出效果
        bottmToolKitHeight = Toolkit.getDefaultToolkit().getScreenInsets(jf.getGraphicsConfiguration()).bottom;
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        screenWidth = dimension.width;
        screenHeight = dimension.height;
        x = screenWidth - windowWidth;
        y = screenHeight;
        jf.setLocation(x, y - bottmToolKitHeight - windowHeight);
        jf.setSize(windowWidth, windowHeight);
        jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

消息列表,用JTable实现:
private JTable jTable = null;
//初始化jtable的数据
  Object rowData[][] = new Object[list.size()][4];
  for(int i=0;i<list.size();i++){
   Message msg = list.get(i);
   rowData[i][0]= msg.getMsg();
   rowData[i][1]= msg.getTime();
   rowData[i][2]= msg.getMsgUrl();
   rowData[i][3]= msg.getFrom();
  }
  Object[] columnNames = new Object[]{"消息内容","接收时间","链接","发送者"};

//设置jtable不可编辑
  TableModel tableModel = new DefaultTableModel(rowData,columnNames){
   public boolean isCellEditable(int rowIndex,int columnIndex){
    return false;
   }
   
  };

//设置表格单元格当鼠标停留时悬浮显示全部内容
jTable = new JTable(tableModel){
    @Override
    public String getToolTipText(MouseEvent event) {
     int row = jTable.rowAtPoint(event.getPoint());
     int col = jTable.columnAtPoint(event.getPoint());
     String tip=null;
     if(row>-1 && col>-1){
      Object value = jTable.getValueAt(row, col);
      if(null!=value && !"".equals(value)){
       tip = value.toString();
      }
     }
     return tip;
    }
   };

  int width = 125;
  //设置列宽
  TableColumnModel tcm = jTable.getColumnModel();
  TableColumn tc1 = tcm.getColumn(1);
  tc1.setPreferredWidth(width);
  tc1.setMaxWidth(width);
  tc1.setMinWidth(width);
// tc1.setResizable(false);
 
  TableColumn tc = tcm.getColumn(2);
  tc.setMaxWidth(width);
  tc.setMinWidth(width);
  tc.setPreferredWidth(width);
// tc.setResizable(false);
 
  int width1= 100;
  TableColumn tc3 = tcm.getColumn(3);
  tc3.setMaxWidth(width1);
  tc3.setMinWidth(width1);
  tc3.setPreferredWidth(width1);
// tc3.setResizable(false);

你可能感兴趣的:(java托盘开发界面记录)