Java2实用教程(第二版)程序代码——第十六章 建立对话框

该博客提供了多个Java对话框编程示例,包括自定义对话框、文件对话框、警告消息对话框、确认对话框和颜色对话框等。通过这些示例展示了如何在Java中创建和使用不同类型的对话框,处理用户交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1None.gif//例子1
  2None.gifimport java.awt.event.*; import java.awt.*;
  3None.gifclass MyDialog extends Dialog implements ActionListener  //建立对话框类。
  4ExpandedBlockStart.gifContractedBlock.gifdot.gif{  static final int YES=1,NO=0;
  5InBlock.gif   int message=-1; Button yes,no;
  6InBlock.gif   MyDialog(Frame f,String s,boolean b) //构造方法。
  7ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  super(f,s,b);
  8InBlock.gif      yes=new Button("Yes"); yes.addActionListener(this);
  9InBlock.gif      no=new Button("No");   no.addActionListener(this);
 10InBlock.gif      setLayout(new FlowLayout());
 11InBlock.gif      add(yes); add(no);
 12InBlock.gif      setBounds(60,60,100,100);
 13InBlock.gif      addWindowListener(new WindowAdapter()
 14ExpandedSubBlockStart.gifContractedSubBlock.gif                      dot.gif{   public void windowClosing(WindowEvent e)
 15ExpandedSubBlockStart.gifContractedSubBlock.gif                           dot.gif{ message=-1;setVisible(false);
 16ExpandedSubBlockEnd.gif                           }

 17ExpandedSubBlockEnd.gif                      }

 18InBlock.gif                   );
 19ExpandedSubBlockEnd.gif   }

 20InBlock.gif   public void actionPerformed(ActionEvent e)
 21ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  if(e.getSource()==yes) 
 22ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{ message=YES;setVisible(false);
 23ExpandedSubBlockEnd.gif      }

 24InBlock.gif     else if(e.getSource()==no)
 25ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{ message=NO;setVisible(false);
 26ExpandedSubBlockEnd.gif      }

 27ExpandedSubBlockEnd.gif   }

 28InBlock.gif   public int getMessage()
 29ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  return message;
 30ExpandedSubBlockEnd.gif   }

 31ExpandedBlockEnd.gif}

 32None.gifclass Dwindow extends Frame implements ActionListener 
 33ExpandedBlockStart.gifContractedBlock.gifdot.gif{  TextArea text; Button button; MyDialog dialog;
 34InBlock.gif   Dwindow(String s)
 35ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  super(s);
 36InBlock.gif      text=new TextArea(5,22); button=new Button("打开对话框"); 
 37InBlock.gif      button.addActionListener(this);
 38InBlock.gif      setLayout(new FlowLayout());
 39InBlock.gif      add(button); add(text); 
 40InBlock.gif      dialog=new MyDialog(this,"我有模式",true);
 41InBlock.gif      setBounds(60,60,300,300); setVisible(true);
 42InBlock.gif      validate();
 43InBlock.gif      addWindowListener(new WindowAdapter()
 44ExpandedSubBlockStart.gifContractedSubBlock.gif                      dot.gif{   public void windowClosing(WindowEvent e)
 45ExpandedSubBlockStart.gifContractedSubBlock.gif                           dot.gif{ System.exit(0);
 46ExpandedSubBlockEnd.gif                           }

 47ExpandedSubBlockEnd.gif                      }

 48InBlock.gif                   );
 49ExpandedSubBlockEnd.gif   }

 50InBlock.gif   public void actionPerformed(ActionEvent e)
 51ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  if(e.getSource()==button)
 52ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{  dialog.setVisible(true); //对话框激活状态时,堵塞下面的语句。
 53InBlock.gif          //对话框消失后下面的语句继续执行:
 54InBlock.gif           if(dialog.getMessage()==MyDialog.YES)   //如果单击了对话框的"yes"按钮。
 55ExpandedSubBlockStart.gifContractedSubBlock.gif             dot.gif{  text.append("\n你单击了对话框的yes按钮");
 56ExpandedSubBlockEnd.gif             }

 57InBlock.gif           else if(dialog.getMessage()==MyDialog.NO)   //如果单击了对话框的"no"按钮。
 58ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{  text.append("\n你单击了对话框的No按钮");
 59ExpandedSubBlockEnd.gif            }

 60ExpandedSubBlockEnd.gif        }

 61ExpandedSubBlockEnd.gif   }

 62ExpandedBlockEnd.gif}

 63None.gifpublic class Example16_1
 64ExpandedBlockStart.gifContractedBlock.gifdot.gif{  public static void main(String args[])
 65ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  new Dwindow("带对话框的窗口");
 66ExpandedSubBlockEnd.gif   }

 67ExpandedBlockEnd.gif}

 68None.gif
 69None.gif//例子2
 70None.gifimport java.awt.*;import java.awt.event.*;
 71None.gifpublic class Example16_2
 72ExpandedBlockStart.gifContractedBlock.gifdot.gif{  public static void main(String args[])
 73ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  FWindow f=new FWindow("窗口"); 
 74ExpandedSubBlockEnd.gif   }

 75ExpandedBlockEnd.gif}

 76None.gifclass  FWindow extends Frame implements ActionListener
 77ExpandedBlockStart.gifContractedBlock.gifdot.gif{  FileDialog filedialog_save,
 78InBlock.gif              filedialog_load;//声明2个文件对话筐
 79InBlock.gif   MenuBar menubar;
 80InBlock.gif   Menu menu;
 81InBlock.gif   MenuItem itemSave,itemLoad;
 82InBlock.gif   TextArea text;
 83InBlock.gif   FWindow(String s)
 84ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  super(s); 
 85InBlock.gif      setSize(300,400);setVisible(true); 
 86InBlock.gif      text=new TextArea(10,10);
 87InBlock.gif      add(text,"Center"); validate(); 
 88InBlock.gif      menubar=new MenuBar();menu=new Menu("文件"); 
 89InBlock.gif  itemSave=new MenuItem("保存文件"); itemLoad=new MenuItem("打开文件"); 
 90InBlock.gif      itemSave.addActionListener(this); itemLoad.addActionListener(this);
 91InBlock.gif      menu.add(itemSave); menu.add(itemLoad); 
 92InBlock.gif      menubar.add(menu);
 93InBlock.gif      setMenuBar(menubar);
 94InBlock.gif      filedialog_save=new FileDialog(this,"保存文件话框",FileDialog.SAVE);
 95InBlock.gif      filedialog_save.setVisible(false);
 96InBlock.gif      filedialog_load=new FileDialog(this,"打开文件话框",FileDialog.LOAD);
 97InBlock.gif      filedialog_load.setVisible(false);
 98InBlock.gif      filedialog_save.addWindowListener(new WindowAdapter()//对话框增加适配器。
 99ExpandedSubBlockStart.gifContractedSubBlock.gif              dot.gif{  public void windowClosing(WindowEvent e)
100ExpandedSubBlockStart.gifContractedSubBlock.gif                   dot.gif{  filedialog_save.setVisible(false);
101ExpandedSubBlockEnd.gif                   }

102ExpandedSubBlockEnd.gif              }
);
103InBlock.gif      filedialog_load.addWindowListener(new WindowAdapter()//对话框增加适配器。
104ExpandedSubBlockStart.gifContractedSubBlock.gif             dot.gif{public void windowClosing(WindowEvent e)
105ExpandedSubBlockStart.gifContractedSubBlock.gif                   dot.gif{ filedialog_load.setVisible(false);
106ExpandedSubBlockEnd.gif                   }

107ExpandedSubBlockEnd.gif             }
);
108InBlock.gif      addWindowListener(new WindowAdapter()  //窗口增加适配器。
109ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{public void windowClosing(WindowEvent e)
110ExpandedSubBlockStart.gifContractedSubBlock.gif                  dot.gif{ setVisible(false);System.exit(0);
111ExpandedSubBlockEnd.gif                  }

112ExpandedSubBlockEnd.gif            }
);
113ExpandedSubBlockEnd.gif   }

114InBlock.gif   public void actionPerformed(ActionEvent e) //实现接口中的方法。
115ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  if(e.getSource()==itemSave)
116ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{  filedialog_save.setVisible(true);
117InBlock.gif            String name=filedialog_save.getFile();
118InBlock.gif            if(name!=null)
119ExpandedSubBlockStart.gifContractedSubBlock.gif              dot.gif{  text.setText("你选择了保存文件,名字是"+name);
120ExpandedSubBlockEnd.gif              }
      
121InBlock.gif            else
122ExpandedSubBlockStart.gifContractedSubBlock.gif             dot.gif{  text.setText("没有保存文件");
123ExpandedSubBlockEnd.gif             }

124ExpandedSubBlockEnd.gif         }

125InBlock.gif       else if(e.getSource()==itemLoad)
126ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{  filedialog_load.setVisible(true);
127InBlock.gif            String name=filedialog_load.getFile();
128InBlock.gif           if(name!=null)
129ExpandedSubBlockStart.gifContractedSubBlock.gif             dot.gif{  text.setText("你选择了打开文件,名字是"+name);
130ExpandedSubBlockEnd.gif             }
      
131InBlock.gif           else
132ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{ text.setText("没有打开文件");
133ExpandedSubBlockEnd.gif            }

134ExpandedSubBlockEnd.gif        }

135ExpandedSubBlockEnd.gif   }

136ExpandedBlockEnd.gif}

137None.gif
138None.gif//例子3
139None.gifimport java.awt.event.*;import java.awt.*;
140None.gifimport javax.swing.JOptionPane;
141None.gifclass Dwindow extends Frame implements ActionListener
142ExpandedBlockStart.gifContractedBlock.gifdot.gif{  TextField inputNumber;
143InBlock.gif   TextArea  show;
144InBlock.gif   Dwindow(String s)
145ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  super(s);
146InBlock.gif      inputNumber=new TextField(22); inputNumber.addActionListener(this);
147InBlock.gif      show=new TextArea(); 
148InBlock.gif      add(inputNumber,BorderLayout.NORTH); add(show,BorderLayout.CENTER); 
149InBlock.gif      setBounds(60,60,300,300); setVisible(true);
150InBlock.gif      validate();
151InBlock.gif      addWindowListener(new WindowAdapter()
152ExpandedSubBlockStart.gifContractedSubBlock.gif                      dot.gif{   public void windowClosing(WindowEvent e)
153ExpandedSubBlockStart.gifContractedSubBlock.gif                           dot.gif{ System.exit(0);
154ExpandedSubBlockEnd.gif                           }

155ExpandedSubBlockEnd.gif                      }

156InBlock.gif                   );
157ExpandedSubBlockEnd.gif   }

158InBlock.gif   public void actionPerformed(ActionEvent e) 
159ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  boolean boo=false;
160InBlock.gif      if(e.getSource()==inputNumber)
161ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{  String s=inputNumber.getText();
162InBlock.gif           char a[]=s.toCharArray();
163InBlock.gif           for(int i=0;i<a.length;i++)
164ExpandedSubBlockStart.gifContractedSubBlock.gif              dot.gif{  if(!(Character.isDigit(a[i])))
165InBlock.gif                      boo=true;         
166ExpandedSubBlockEnd.gif              }
 
167InBlock.gif           if(boo==true)   //弹出"警告"消息对话框。
168ExpandedSubBlockStart.gifContractedSubBlock.gif              dot.gif{  JOptionPane.showMessageDialog(this,"您输入了非法字符","警告对话框",
169InBlock.gif                                             JOptionPane.WARNING_MESSAGE);
170InBlock.gif                 inputNumber.setText(null); 
171ExpandedSubBlockEnd.gif              }
  
172InBlock.gif           else if(boo==false)
173ExpandedSubBlockStart.gifContractedSubBlock.gif              dot.gif{  int number=Integer.parseInt(s);
174InBlock.gif                 show.append("\n"+number+"平方:"+(number*number));
175ExpandedSubBlockEnd.gif              }

176ExpandedSubBlockEnd.gif        }

177ExpandedSubBlockEnd.gif   }

178ExpandedBlockEnd.gif}

179None.gifpublic class Example16_3
180ExpandedBlockStart.gifContractedBlock.gifdot.gif{  public static void main(String args[]) 
181ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  new Dwindow("带对话框的窗口");
182ExpandedSubBlockEnd.gif   }

183ExpandedBlockEnd.gif}

184None.gif
185None.gif//例子4
186None.gifimport java.awt.event.*;import java.awt.*;
187None.gifimport javax.swing.JOptionPane;
188None.gifclass Dwindow extends Frame implements ActionListener
189ExpandedBlockStart.gifContractedBlock.gifdot.gif{  TextField inputName;
190InBlock.gif   TextArea  save;
191InBlock.gif   Dwindow(String s)
192ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  super(s);
193InBlock.gif      inputName=new TextField(22);inputName.addActionListener(this);
194InBlock.gif      save=new TextArea();
195InBlock.gif      add(inputName,BorderLayout.NORTH); add(save,BorderLayout.CENTER); 
196InBlock.gif      setBounds(60,60,300,300); setVisible(true);
197InBlock.gif      validate();
198InBlock.gif      addWindowListener(new WindowAdapter()
199ExpandedSubBlockStart.gifContractedSubBlock.gif                      dot.gif{   public void windowClosing(WindowEvent e)
200ExpandedSubBlockStart.gifContractedSubBlock.gif                           dot.gif{ System.exit(0);
201ExpandedSubBlockEnd.gif                           }

202ExpandedSubBlockEnd.gif                      }

203InBlock.gif                   );
204ExpandedSubBlockEnd.gif   }

205InBlock.gif   public void actionPerformed(ActionEvent e) 
206ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  String s=inputName.getText();
207InBlock.gif      int n=JOptionPane.showConfirmDialog(this,"确认正确吗?","确认对话框",
208InBlock.gif                                               JOptionPane.YES_NO_OPTION );
209InBlock.gif      if(n==JOptionPane.YES_OPTION)  
210ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{  save.append("\n"+s);
211ExpandedSubBlockEnd.gif        }
  
212InBlock.gif      else if(n==JOptionPane.NO_OPTION)  
213ExpandedSubBlockStart.gifContractedSubBlock.gif       dot.gif{ inputName.setText(null);
214ExpandedSubBlockEnd.gif       }
  
215ExpandedSubBlockEnd.gif   }

216ExpandedBlockEnd.gif}

217None.gifpublic class Example16_4
218ExpandedBlockStart.gifContractedBlock.gifdot.gif{  public static void main(String args[]) 
219ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  new Dwindow("带对话框的窗口");
220ExpandedSubBlockEnd.gif   }

221ExpandedBlockEnd.gif}

222None.gif
223None.gif//例子5
224None.gifimport java.awt.event.*;
225None.gifimport java.awt.*;
226None.gifimport javax.swing.JColorChooser;
227None.gifclass Dwindow extends Frame implements ActionListener 
228ExpandedBlockStart.gifContractedBlock.gifdot.gif{  Button button;
229InBlock.gif   Dwindow(String s)
230ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  super(s);
231InBlock.gif      button=new Button("打开颜色对话框"); 
232InBlock.gif      button.addActionListener(this);
233InBlock.gif      setLayout(new FlowLayout());
234InBlock.gif      add(button);
235InBlock.gif      setBounds(60,60,300,300);
236InBlock.gif      setVisible(true);
237InBlock.gif      validate();
238InBlock.gif      addWindowListener(new WindowAdapter()
239ExpandedSubBlockStart.gifContractedSubBlock.gif                      dot.gif{   public void windowClosing(WindowEvent e)
240ExpandedSubBlockStart.gifContractedSubBlock.gif                           dot.gif{
241InBlock.gif                             System.exit(0);
242ExpandedSubBlockEnd.gif                           }

243ExpandedSubBlockEnd.gif                      }

244InBlock.gif                   );
245InBlock.gif
246ExpandedSubBlockEnd.gif   }

247InBlock.gif   public void actionPerformed(ActionEvent e)
248ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif
249InBlock.gif     Color newColor=JColorChooser.showDialog(this,"调色板",button.getBackground());
250InBlock.gif     button.setBackground(newColor);     
251ExpandedSubBlockEnd.gif   }

252ExpandedBlockEnd.gif}

253None.gifpublic class Example16_5
254ExpandedBlockStart.gifContractedBlock.gifdot.gif{  public static void main(String args[])
255ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif
256InBlock.gif      new Dwindow("带颜色对话框的窗口");
257ExpandedSubBlockEnd.gif   }

258ExpandedBlockEnd.gif}

259None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值