自定義一個類,繼承與JDialog:
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MyDialog extends JDialog
{
//創建兩個按鈕
JButton noButton = new JButton("不喜歡");
JButton okButton = new JButton("喜歡");
//用於判斷每點一次變換位置
boolean isEnter = false;
//對話框默認爲取消
boolean retValue = false;
//位移量
int move = 40;
public MyDialog(JFrame frame)
{
super(frame, "真心話", true);
JPanel root = new JPanel();
this.setContentPane(root);
root.setLayout(new BorderLayout());
//顯示內容
Box line1 = Box.createHorizontalBox();
line1.setBorder(BorderFactory.createEmptyBorder(20, 0, 0, 0));
line1.add(Box.createHorizontalGlue());
line1.add(new JLabel("你喜歡我嗎?"));
line1.add(Box.createHorizontalGlue());
//兩個按鈕
JPanel line2 = new JPanel();
line2.setBorder(BorderFactory.createEmptyBorder(40, 0, 0, 0));
line2.add(okButton);
line2.add(noButton);
root.add(line1, BorderLayout.PAGE_START);
root.add(line2, BorderLayout.CENTER);
//點擊“喜歡”關閉對話框
okButton.addActionListener(e->{
//點擊確定後返回true
retValue = true;
this.setVisible(false);
});
//“不喜歡”鼠標事件
noButton.addMouseListener(new MouseAdapter() {
//鼠標移入
@Override
public void mouseEntered(MouseEvent e)
{
//獲得當前按鈕的座標
int initX = noButton.getX();
int initY = noButton.getY();
//點擊一次
if(isEnter) //isEnter的作用爲每點一次切換方向(上下)
{
//Y座標+40(向下移動40px)
initY += move;
isEnter = false;
}
else
{
//Y座標-40(向上移動40px)
initY -= move;
isEnter = true;
}
//重新設置按鈕的座標(大小不變)
noButton.setBounds(initX, initY, noButton.getWidth(), noButton.getHeight());
}
});
}
//執行此方法調出對話框
public boolean exec()
{
/**相對於JFrame佈局居中*/
//獲取JFrame的矩形
Rectangle rec = this.getOwner().getBounds();
this.setSize(300, 200);
//獲得當前對話框的寬高
int width = this.getWidth();
int height = this.getHeight();
//設置座標居中 (加上JFrame的座標)
int x = rec.x + (rec.width - width) / 2;
int y = rec.y + (rec.height - height) / 2;
this.setBounds(x, y, width, height);
this.setVisible(true);
return retValue;
}
}
在MyFrame裏調用對話框:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyFrame extends JFrame
{
JButton button = new JButton("點我");
public MyFrame(String title)
{
super(title);
JPanel root = new JPanel();
this.setContentPane(root);
root.add(button);
//按鈕點擊事件
button.addActionListener(e->{
popupDialog();
});
}
public void popupDialog()
{
//創建一個MyDialog
MyDialog dialog = new MyDialog(this);
//調用exec()彈出對話框,這裏會阻塞程序知道對話框關閉才返回值
while(true)
{
if(dialog.exec())
{
JOptionPane.showMessageDialog(this, "哈哈 我就知道你喜歡我");
break;
}
else
{
JOptionPane.showMessageDialog(this, "不能逃避 必須回答");
continue;
}
}
}
}
程序入口:
import javax.swing.JFrame;
public class SwingDemo
{
private static void createGUI()
{
// JFrame指一個窗口,構造方法的參數爲窗口標題
MyFrame frame = new MyFrame("Swing Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 設置窗口的其他參數,如窗口大小
frame.setSize(400, 300);
// 顯示窗口
frame.setVisible(true);
}
public static void main(String[] args)
{
// 此段代碼間接地調用了 createGUI(),具體原理在 Swing高級篇 裏講解
// 初學者先照抄此代碼框架即可
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run()
{
createGUI();
}
});
}
}
源碼下載:點擊進入下載頁面