主题:请教一下我写的窗体靠边隐藏实现不了啊?
package newpackage;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.*;
import javax.swing.Timer;
public class NewJDialog1 extends javax.swing.JDialog implements ActionListener{
private static final long serialVersionUID = 1L;
private Rectangle rect;
// 窗体离屏幕左边的距离
private int left;
// 窗体离屏幕右边的距离;
private int right;
// 屏幕的宽度;
private int screenXX;
// 窗体离屏幕顶部的距离
private int top;
// 窗体的宽
private int width;
// 窗体的高
private int height;
// 鼠标在窗体的位置
private Point point;
private Timer timer = new Timer(10,this);
private int xx, yy;
private boolean isDraging = false;
public NewJDialog1(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
timer.start();
this.setLocation(-395, 100);
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setAlwaysOnTop(true);
setResizable(false);
setUndecorated(true);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 400, 300));
pack();
}
public void actionPerformed(ActionEvent e) {
left = jPanel1.getLocationOnScreen().x;
top = jPanel1.getLocationOnScreen().y;
width = jPanel1.getWidth();
height = jPanel1.getHeight();
screenXX = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
right = screenXX - left - width;
// 获取窗体的轮廓
rect = new Rectangle(0, 0, width, height);
// 获取鼠标在窗体的位置
point = jPanel1.getMousePosition();
if (left < 0 && isPtInRect(rect, point)) {
jPanel1.setLocation(0, top); // 隐藏在左边,鼠标指到后显示窗体;
} else if (left > -5 && left < 5 && !(isPtInRect(rect, point))) {
jPanel1.setLocation(left - width + 1, top); // 窗体移到左边便边缘隐藏到左边;
}
}
public boolean isPtInRect(Rectangle rect, Point point) {
if (rect != null && point != null) {
int x0 = rect.x;
int y0 = rect.y;
int x1 = rect.width;
int y1 = rect.height;
int x = point.x;
int y = point.y;
return x >= x0 && x < x1 && y >= y0 && y < y1;
}
return false;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
NewJDialog1 dialog = new NewJDialog1(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
private javax.swing.JPanel jPanel1;
}
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.*;
import javax.swing.Timer;
public class NewJDialog1 extends javax.swing.JDialog implements ActionListener{
private static final long serialVersionUID = 1L;
private Rectangle rect;
// 窗体离屏幕左边的距离
private int left;
// 窗体离屏幕右边的距离;
private int right;
// 屏幕的宽度;
private int screenXX;
// 窗体离屏幕顶部的距离
private int top;
// 窗体的宽
private int width;
// 窗体的高
private int height;
// 鼠标在窗体的位置
private Point point;
private Timer timer = new Timer(10,this);
private int xx, yy;
private boolean isDraging = false;
public NewJDialog1(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
timer.start();
this.setLocation(-395, 100);
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setAlwaysOnTop(true);
setResizable(false);
setUndecorated(true);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 400, 300));
pack();
}
public void actionPerformed(ActionEvent e) {
left = jPanel1.getLocationOnScreen().x;
top = jPanel1.getLocationOnScreen().y;
width = jPanel1.getWidth();
height = jPanel1.getHeight();
screenXX = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
right = screenXX - left - width;
// 获取窗体的轮廓
rect = new Rectangle(0, 0, width, height);
// 获取鼠标在窗体的位置
point = jPanel1.getMousePosition();
if (left < 0 && isPtInRect(rect, point)) {
jPanel1.setLocation(0, top); // 隐藏在左边,鼠标指到后显示窗体;
} else if (left > -5 && left < 5 && !(isPtInRect(rect, point))) {
jPanel1.setLocation(left - width + 1, top); // 窗体移到左边便边缘隐藏到左边;
}
}
public boolean isPtInRect(Rectangle rect, Point point) {
if (rect != null && point != null) {
int x0 = rect.x;
int y0 = rect.y;
int x1 = rect.width;
int y1 = rect.height;
int x = point.x;
int y = point.y;
return x >= x0 && x < x1 && y >= y0 && y < y1;
}
return false;
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
NewJDialog1 dialog = new NewJDialog1(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
private javax.swing.JPanel jPanel1;
}