扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
在Javaapplet中实现模式对话框的关键就是在创建一个对话框的时候要为该对话框指定一个正确的父窗口.因为Applet是Panel类的子类,不可以作为对话框的父窗口,所以首先要获得applet所在的窗口,作为模式对话框的父窗口.样例代码如下:
为温州等地区用户提供了全套网页设计制作服务,及温州网站建设行业解决方案。主营业务为成都做网站、网站设计、外贸营销网站建设、温州网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
.....
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实现点击下载文件的时候,弹出“另存为”对话框,选择保存位置,然后下载,代码如下:
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);
//以启动一条线程的方式来执行一个耗时的任务
final Thread targetThread = new Thread(target);
targetThread.start();
//创建进度对话框
final ProgressMonitor dialog = new ProgressMonitor(null ,
"等待任务完成" , "已完成:" , 0 , target.getAmount());
//创建一个计时器
timer = new Timer(300 , new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//以任务的当前完成量设置进度对话框的完成比例
dialog.setProgress(target.getCurrent());
//如果用户单击了进度对话框的”取消“按钮
if (dialog.isCanceled())
{
//停止计时器
timer.stop();
//中断任务的执行线程
targetThread.interrupt();
//系统退出
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);//关闭后销毁对话框
setLocationRelativeTo(null);
JLabel jl1 = new JLabel("姓名:");
jtf1 = new JTextField(8);
JLabel jl2 = new JLabel("学号:");
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()+" 学号:"+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位数以内的字符串");
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);
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流