在Javaapplet中實現(xiàn)模式對話框的關(guān)鍵就是在創(chuàng)建一個對話框的時候要為該對話框指定一個正確的父窗口.因為Applet是Panel類的子類,不可以作為對話框的父窗口,所以首先要獲得applet所在的窗口,作為模式對話框的父窗口.樣例代碼如下:
為溫州等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及溫州網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)、溫州網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
.....
Dialogd=newDialog(getParentWindow(comp),title);
//comp為applet上的任意一個組件
....
publicvoidgetParentWindow(ComponentcompOnApplet,Stringtitle){
Containerc=compOnApplet.getParent();
while(c!=null){
if(cinstanceofFrame)
return(Frame)c;
c=c.getParent();
}
returnnull;
}
可以使用JoptionPane:
有幾種提示框:
第一種:
JOptionPane.showMessageDialog(jPanel,?"提示消息",?"標題",JOptionPane.WARNING_MESSAGE);
第二種:
int?n?=?JOptionPane.showConfirmDialog(null,?"你高興嗎?",?"標題",JOptionPane.YES_NO_OPTION);//返回的是按鈕的index ?i=0或者1
第三種:
Object[]?obj2?={?"足球",?"籃球",?"乒乓球"?};
String?s?=?(String)?JOptionPane.showInputDialog(null,"請選擇你的愛好:\n",?"愛好",?JOptionPane.PLAIN_MESSAGE,?new?ImageIcon("icon.png"),?obj2,?"足球");
Java實現(xiàn)點擊下載文件的時候,彈出“另存為”對話框,選擇保存位置,然后下載,代碼如下:
public?void?downLoad(String?filePath,?HttpServletResponse?response)?
throws?Exception?{?
System.out.println("filePath"+filePath);?
File?f?=?new?File(filePath);?
if?(!f.exists())?{?
response.sendError(404,?"File?not?found!");?
return;?
}?
BufferedInputStream?br?=?new?BufferedInputStream(new?FileInputStream(f));?
byte[]?buf?=?new?byte[1024];?
int?len?=?0;?
response.reset();?
response.setContentType("application/x-msdownload");?
response.setHeader("Content-Disposition",?"attachment;?filename="?+?f.getName());?
OutputStream?out?=?response.getOutputStream();?
while?((len?=?br.read(buf))??0)?out.write(buf,?0,?len);?
br.close();?
out.close();?
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ProgressMonitor;
import javax.swing.Timer;
public class TestProgressMonitor
{
Timer timer;
public void init()
{
final SimulatedTarget target = new SimulatedTarget(1000);
//以啟動一條線程的方式來執(zhí)行一個耗時的任務(wù)
final Thread targetThread = new Thread(target);
targetThread.start();
//創(chuàng)建進度對話框
final ProgressMonitor dialog = new ProgressMonitor(null ,
"等待任務(wù)完成" , "已完成:" , 0 , target.getAmount());
//創(chuàng)建一個計時器
timer = new Timer(300 , new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//以任務(wù)的當前完成量設(shè)置進度對話框的完成比例
dialog.setProgress(target.getCurrent());
//如果用戶單擊了進度對話框的”取消“按鈕
if (dialog.isCanceled())
{
//停止計時器
timer.stop();
//中斷任務(wù)的執(zhí)行線程
targetThread.interrupt();
//系統(tǒng)退出
System.exit(0);
}
}
});
timer.start();
}
public static void main(String[] args)
{
new TestProgressMonitor().init();
}
}
效果圖
參考代碼
import?java.awt.BorderLayout;
import?java.awt.GridLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.JButton;
import?javax.swing.JDialog;
import?javax.swing.JLabel;
import?javax.swing.JPanel;
import?javax.swing.JTextField;
public?class?DHKDemo?extends?JDialog?{
final?JTextField?jtf1,?jtf2,?jtf3;//定義三個輸入框
final?JLabel?jlinfo;
public?DHKDemo()?{
setTitle("多項輸入對話框");
setModal(true);
setSize(300,?200);//對話框的大小
setDefaultCloseOperation(DISPOSE_ON_CLOSE);//關(guān)閉后銷毀對話框
setLocationRelativeTo(null);
JLabel?jl1?=?new?JLabel("姓名:");
jtf1?=?new?JTextField(8);
JLabel?jl2?=?new?JLabel("學(xué)號:");
jtf2?=?new?JTextField(8);
JLabel?jl3?=?new?JLabel("年齡:");
jtf3?=?new?JTextField(8);
JPanel?jp?=?new?JPanel(new?GridLayout(3,?2));
jp.add(jl1);
jp.add(jtf1);
jp.add(jl2);
jp.add(jtf2);
jp.add(jl3);
jp.add(jtf3);
JButton?jb?=?new?JButton("確認輸入");
jlinfo?=?new?JLabel("信息:",JLabel.CENTER);
jb.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
String?info?=?"姓名:"+jtf1.getText()+"?學(xué)號:"+jtf2.getText()+"?年齡:"+jtf3.getText();
jlinfo.setText(info);
}
});
add(jp);
add(jlinfo,BorderLayout.NORTH);
add(jb,BorderLayout.SOUTH);
}
public?static?void?main(String[]?args)?{
new?DHKDemo().setVisible(true);
}
}
package?cn.fu;
import?java.awt.BorderLayout;
import?java.awt.EventQueue;
import?javax.swing.JFrame;
import?javax.swing.JPanel;
import?javax.swing.border.EmptyBorder;
import?javax.swing.JOptionPane;
import?javax.swing.JTextField;
import?javax.swing.JLabel;
import?javax.swing.JButton;
import?java.awt.event.ActionListener;
import?java.awt.event.ActionEvent;
import?java.awt.Window.Type;
public?class?Login?extends?JFrame?{
private?JPanel?contentPane;
private?JTextField?textField;
/**
*?Launch?the?application.
*/
public?static?void?main(String[]?args)?{
EventQueue.invokeLater(new?Runnable()?{
public?void?run()?{
try?{
Login?frame?=?new?Login();
frame.setVisible(true);
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
});
}
/**
*?Create?the?frame.
*/
public?Login()?{
setTitle("工具");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,?100,?450,?300);
contentPane?=?new?JPanel();
contentPane.setToolTipText("");
contentPane.setBorder(new?EmptyBorder(5,?5,?5,?5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField?=?new?JTextField();
textField.setBounds(121,?86,?194,?21);
contentPane.add(textField);
textField.setColumns(10);
JLabel?lblNewLabel?=?new?JLabel("請輸入10位數(shù)以內(nèi)的字符串");
lblNewLabel.setBounds(145,?59,?194,?15);
contentPane.add(lblNewLabel);
JButton?btnNewButton?=?new?JButton("確定");
btnNewButton.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
String?ca?=?textField.getText();
int?n?=?ca.length();
if?(n??10)?{
JOptionPane.showMessageDialog(null,?"對不起,您輸入的字符串長度超過10",
"錯誤提示",?JOptionPane.ERROR_MESSAGE);
}?else?if?(n?=?0?||?n?=?10)?{
JOptionPane.showMessageDialog(null,?"字符串長度為"?+?n,?"提示",
JOptionPane.PLAIN_MESSAGE);
}
}
});
btnNewButton.setBounds(172,?130,?93,?23);
contentPane.add(btnNewButton);
}
}