dialog.txt

来自「java Java书籍里面有很多初学java的资料」· 文本 代码 · 共 71 行

TXT
71
字号
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MainFrame extends JFrame{
  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"

);
    }
    catch (Exception ex) {

    }
    MainFrame frame = new MainFrame();

  }
  public void showDialog(){
   JDialog jd = new JDialog(this,"this is a dialog box");
    JPanel jp = new JPanel();
    jp.add(new JLabel("a dialog test"));
    jd.getContentPane().add(jp);
    jd.pack();
    jd.setVisible(true);

  }
  public MainFrame(){
    showDialog();//跳出来一个普通的对话框
    JOptionPane.showMessageDialog(this,new JLabel("hello"),"a messagebox",JOptionPane.QUESTION_MESSAGE);
   //confirm dialog
   JOptionPane.showConfirmDialog(this,new JButton("confirm"),"confirm",JOptionPane.YES_NO_CANCEL_OPTION);
   //选择 dialog
   String str[]={"A","B","C","D"};
   JOptionPane.showOptionDialog(this,"please select one","no title",JOptionPane.YES_NO_CANCEL_OPTION,
      JOptionPane.QUESTION_MESSAGE,new ImageIcon("about.png"),str,str[1]);

   this.setTitle("this is a test frame");
    this.setSize(300,300);
    this.setResizable(false);
    this.setVisible(true);

    this.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
            int r; //用来接收你刚才的选择,用到confirm dialog
            r = JOptionPane.showConfirmDialog(null,"do you want to quit","no title",JOptionPane.YES_NO_CANCEL_OPTION);
            if(r==JOptionPane.YES_OPTION)
              System.exit(0);


           }

           public void windowClosed(WindowEvent e) {
           }

           public void windowOpened(WindowEvent e) {
           }

           public void windowIconified(WindowEvent e) {
           }

           public void windowDeiconified(WindowEvent e) {
           }

           public void windowActivated(WindowEvent e) {
           }

           public void windowDeactivated(WindowEvent e) {
           }
         });
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?