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. iOS 汉字转拼音

    - (NSString *)getFirstString:(ICCustom *)custom {     NSMutableString *source = [custom.merchantAbbr ...

  2. iOS 设置tableview头部不可以向下拉动..

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGPoint point = scrollView.contentOffset; i ...

  3. 2014年IT界最具“钱”景的10类岗位

    来自人力资源公司罗致恒富(Robert Half)的一组2014年薪资指导显示,在工程.软件开发和程序领域有一技之长的职工的薪资可能出现何种程度的增长.罗致恒富预计2014年薪资将平均上调3.7%,而 ...

  4. 在cocos2d里面如何使用Texture Packer和像素格式来优化spritesheet

    免责申明(必读!):本博客提供的所有教程的翻译原稿均来自于互联网,仅供学习交流之用,切勿进行商业传播.同时,转载时不要移除本申明.如产生任何纠纷,均与本博客所有人.发表该翻译稿之人无任何关系.谢谢合作 ...

  5. CSS的四种引入方式

    1.使用link标签引入css文件: <head> <link rel="stylesheet" type="text/css" href=& ...

  6. Libgls 1.0.1 发布,OpenGL 立体渲染

    Libgls 1.0.1 只是修复了一些小问题,更新了 FindGLS.cmake 脚本. Libgls 允许 OpenGL 立体渲染,不需要硬件支持四缓冲立体.支持许多立体显示模式,从立体眼镜的3D ...

  7. C#设计模式——抽象工厂

    一.引言 我相信看到这段文字的人,都具备了良好的技术功底.但是对于自己编写的代码总是充满抱怨,希望能够将自己编写的代码如同房子一般先进行有效 的设计,然后在进行建设.那么这篇文章能够给你一些思路,这里 ...

  8. 鸟瞰Nodejs

    一,基础. 1,Node的包管理器:npm; 安装node环境时会自动安装. 本地模式获取一个包:npm install [package_name] 此时包被安装到当前木的node_modules子 ...

  9. 深入理解java虚拟机【Java虚拟机类生命周期】

    C/C++等纯编译语言从源码到最终执行一般要经历:编译.连接和运行三个阶段,连接是在编译期间完成,而java在编译期间仅仅是将源码编译为Java虚拟机可以识别的字节码Class类文件,Java虚拟机对 ...

  10. 自己动手写UI库——引入ExtJs(布局)

    第一: 来看一下最终的效果 第二: 来看一下使用方法: 第三: Component类代码如下所示: public class Component     {                   pub ...