Swing How to make dialogues
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的更多相关文章
- 如何使用swing创建一个BeatBox
首先,我们需要回顾一些内容(2017-01-04 14:32:14): 1.Swing组件 Swing的组件(component,或者称之为元件),是较widget更为正确的术语,它们就是会放在GUI ...
- Java Swing interview
http://www.careerride.com/Swing-AWT-Interview-Questions.aspx Swing interview questions and answers ...
- Swing布局管理器介绍
创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://zhangjunhd.blog.51cto.com/113473/128174 当选 ...
- swing with transformjs
Antecedent Facebook made a HTML5 game long time ago. The opening animation is a piece of software th ...
- java swing 双人五子棋源代码
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Toolkit; impo ...
- 用swing也可以做出好看的界面
用Swing做出的例子:JavaFX做出的界面:后来又做出了自己编写的一套基于Synth的L&F,其与直接在代码中重绘某个组件不同,最大优点是具有可插拔性,即在不改变原有程序代码的情况下,用户 ...
- Java界面设计 Swing(1)
Java界面设计的用途 开发者可以通过Java SE开发丰富并且强大的具有图形界面的桌面应用程序.也可以设计一些提高效率的工具软件,帮助自己处理机械性工作. Java 的图形界面工具包,可以用于工具类 ...
- java基础 swing编程实战
1. 实现金山词霸,点击左右收缩 效果图: exmaple code : /* * 词霸 * */ package demo7; import java.awt.*; import java.awt. ...
- Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解
Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解 在swing中,基于业务的考量,会有对话框来限制用户的行为及对用户的动作进行提示. Swing中 ...
随机推荐
- 第42讲:Scala中泛型类、泛型函数、泛型在Spark中的广泛应用
今天来了解下scala的泛型 先让我们看下这段代码 class Triple[F,S,T](val first:F,val second: S,val third: T) val triple = n ...
- HTML之jQuery
一.简介 jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more(写得更少,做得更多).jQuery在2006年1月由美国人John Resig在纽约的 ...
- h5专题应该兼容那些浏览器?
本人做专题还不算很多,但是也很腻烦了.一般一个专题制作也就3天,可是调试得4/5天.除了销售客户各种无休止的改改改.还有一点很重要就是浏览器的兼容性.刚开始做专题的时候天真的以为苹果只要兼容到ipho ...
- Keil的标题“礦ision3" 的改变(转)
MDK 的标题显示成 “礦ision3",前面的这个不是u而是一个希腊字母“缪”,在中文显示中出现问题,半个汉字. 可以使用如下方法取消. 一: 光标问题 Keil uv3 中会出现光标定位 ...
- 解读SQL Server 2014可更新列存储索引——存储机制
概述 SQL Server 2014被号称是微软数据库的一个革命性版本,其性能的提升的幅度是有史以来之最. 可更新的列存储索引作为SQL Server 2014的一个关键功能之一,在提升数据库的查询性 ...
- 从数组中选出和等于固定值的n个数(JavaScript实现)
现实生活中的问题,可能会抽象为这样一种数据模型: 从一个数组中挑选出几个数,让这几个数相加的和为指定的值. 大多数读者应该有过网购的经历,网购一般会有个凑单功能,假如读者买了70元的商品,但是必须满1 ...
- Java程序员的日常——《编程思想》一切都是对象
今天终于看完了第一章,哈哈,万事开头难....刚开始被编程思想的第一章给蒙住了,讲一堆理论,没什么意思.从第二章开始,真正的开始讲解Java相关的内容,有了一定的开发经验后,再次阅读起来,感觉收获良多 ...
- Sublime Text 常用快捷键和优秀插件
SublimeText3常用快捷键和优秀插件 SublimeText是前端的一个神器,以其精简和可DIY而让广大fans疯狂.好吧不吹了直入正题 -_-!! 首先是安装,如果你有什么软件管家的话搜一下 ...
- paip.简化字-手写参考二简字..共98个
paip.简化字-手写参考二简字..共98个 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/a ...
- Liferay7 BPM门户开发之35: AssetTag的集成查询
Tag是liferay中的Asset特性,可以用来对信息进行分类,在iferay中的Asset类型为: 1. Web Content(自定义内容) 2. Documents and Media(文档库 ...