There are two types of dialog:

  • modal
  • non-modal: must use JDialog directly

Taken JFileChooser as Example:

1. Open a JFileChooser:

JFileChooserfileChooser = new JFileChooser();

2. Get value from it:

File file = fileChooser.getSelectedFile();

Code:



 import javax.swing.*;

 import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File; public class nameDlg extends JFrame { public nameDlg() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("GridBagLayout"); this.setSize(600, 200); Container pane = this.getContentPane(); GridBagLayout layout = new GridBagLayout(); pane.setLayout(layout); GridBagConstraints c = new GridBagConstraints(); // lable
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(3, 3, 3, 3);
c.weightx = 0;
c.weighty = 0.25; pane.add(new JLabel("Please enter the prefix:"), c); // textfield
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 3;
c.weightx = 0; pane.add(new JTextField(), c); // lable
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1; pane.add(new JLabel("Please enter the starting number:"), c); // textfield
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 3; pane.add(new JTextField(), c); // lable
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1; pane.add(new JLabel("Please select a folder:"), c); // textfield shows file path
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
c.weightx = 0.75; final JTextField pathText = new JTextField();
pane.add(pathText, c); // button
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 2;
c.weightx = 0.25; JButton folderBtn = new JButton("Folder"); folderBtn.addActionListener(new ActionListener() {
JFileChooser fileChooser; @Override
public void actionPerformed(ActionEvent event) { fileChooser = new JFileChooser();
int returnVal = fileChooser.showOpenDialog(nameDlg.this); if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile(); pathText.setText(file.getPath());
} }
}); pane.add(folderBtn, c); // button enter
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 5;
c.insets.top = 10;
c.weightx = 0; pane.add(new JButton("Enter"), c); // button quit
c.fill = GridBagConstraints.NONE;
c.gridx = 1;
c.gridy = 5; pane.add(new JButton("Quit"), c); this.setVisible(true); }
}

Swing How to make dialogues的更多相关文章

  1. 如何使用swing创建一个BeatBox

    首先,我们需要回顾一些内容(2017-01-04 14:32:14): 1.Swing组件 Swing的组件(component,或者称之为元件),是较widget更为正确的术语,它们就是会放在GUI ...

  2. Java Swing interview

    http://www.careerride.com/Swing-AWT-Interview-Questions.aspx   Swing interview questions and answers ...

  3. Swing布局管理器介绍

    创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://zhangjunhd.blog.51cto.com/113473/128174 当选 ...

  4. swing with transformjs

    Antecedent Facebook made a HTML5 game long time ago. The opening animation is a piece of software th ...

  5. java swing 双人五子棋源代码

    import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Toolkit; impo ...

  6. 用swing也可以做出好看的界面

    用Swing做出的例子:JavaFX做出的界面:后来又做出了自己编写的一套基于Synth的L&F,其与直接在代码中重绘某个组件不同,最大优点是具有可插拔性,即在不改变原有程序代码的情况下,用户 ...

  7. Java界面设计 Swing(1)

    Java界面设计的用途 开发者可以通过Java SE开发丰富并且强大的具有图形界面的桌面应用程序.也可以设计一些提高效率的工具软件,帮助自己处理机械性工作. Java 的图形界面工具包,可以用于工具类 ...

  8. java基础 swing编程实战

    1. 实现金山词霸,点击左右收缩 效果图: exmaple code : /* * 词霸 * */ package demo7; import java.awt.*; import java.awt. ...

  9. Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解

    Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解   在swing中,基于业务的考量,会有对话框来限制用户的行为及对用户的动作进行提示. Swing中 ...

随机推荐

  1. day5----正则

    %a    本地(locale)简化星期名称     %A    本地完整星期名称     %b    本地简化月份名称     %B    本地完整月份名称     %c    本地相应的日期和时间 ...

  2. iOS ViewController生命周期

    ViewController是view的controller,viewController的职责主要包括管理内部各个view的加载显示与卸载,同时负责与其他ViewController的通信和协调. ...

  3. 【原创】-- Linux 下利用dnw进行USB下载

    原帖地址: http://blog.csdn.net/jjzhoujun2010 http://blog.csdn.net/yf210yf/article/details/6700391 http:/ ...

  4. 最常用的PHP正则表达式收集整理

    最常用的PHP正则表达式收集整理 提交 我的评论 加载中 已评论 最常用的PHP正则表达式收集整理 2015-03-20 PHP100中文网 PHP100中文网 PHP100中文网 微信号 功能介绍 ...

  5. autorelease的对象何时被释放

    autorelease的对象何时被释放 参考答案: 如果了解一点点Run Loop的知道,应该了解到:Run Loop在每个事件循环结束后会去自动释放池将所有自动释放对象的引用计数减一,若引用计数变成 ...

  6. 使用lock和condition实现的阻塞队列-字符串

    在jdk 的API中提供了一个字符串的阻塞队列 : class BoundedBuffer { final Lock lock = new ReentrantLock(); final Conditi ...

  7. SQLSERVER监控复制并使用数据库邮件功能发告警邮件

    SQLSERVER监控复制并使用数据库邮件功能发告警邮件 最近熬出病来了,都说IT行业伤不起,不说了,说回今天的正题 正题 上个月月底的时候因为要搬迁机房,需要将一个数据信息数据库先搬到我们的机房,然 ...

  8. RaisingStudio.SessionFactory 发布 0.1版

    功能描述: 1. 支持Orchard中方便使用自定义数据库连接. 2. 连接信息可配置. 用法: 1. 构造函数中添加IRepositoryFactory引用 private readonly IRe ...

  9. Connect教程系列--响应式布局(一)

    微软终于跨平台了,最近在第九频道(Event-Visual Studio)下面出来一系列关于vs2015的视频,介绍vs2015以及.NET5.0的新特性等,现针对其讲解的.NET和ASP.NET,进 ...

  10. CodeIgniter nginx 404

    默认情况下CI 不支持路由模式需要在server里面配置,配置成如下即可: server { listen 80 ; server_name wechat.XX.com.cn; root XX; in ...