?? dialogexample.java
字號:
//DialogExample.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class DialogExample
{
public static void main(String[] args)
{
DialogFrame frame = new DialogFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class DialogFrame extends JFrame
{
public DialogFrame()
{
setTitle("DialogExample");
setSize(WIDTH, HEIGHT);
Container contentPane = getContentPane();
//建立容器面板
JPanel buttonPanel = new JPanel();
logoutButton = new JButton("退出");
//注冊事件監聽器
logoutButton.addActionListener(new LogoutAction());
buttonPanel.add(logoutButton);
contentPane.add(buttonPanel,BorderLayout.SOUTH);
}
//實現事件監聽器
private class LogoutAction
implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//新建確認型對話框
int selection = JOptionPane.showConfirmDialog(
DialogFrame.this,
"Are you sure?", "Logout",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
//如果選擇了“確認”按鈕,則退出
if (selection == JOptionPane.OK_OPTION)
{
System.exit(0);
}
}
}
public static final int WIDTH = 200;
public static final int HEIGHT = 120;
private JButton logoutButton;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -