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中 ...
随机推荐
- Robot Framework-DatabaseLibrary数据库(MySql)
Robot Framework-Mac版本安装 Robot Framework-Windows版本安装 Robot Framework-工具简介及入门使用 Robot Framework-Databa ...
- PHP程序员的技术成长规划(转)
第一阶段:基础阶段(基础PHP程序员) 重点:把LNMP搞熟练(核心是安装配置基本操作) 目标:能够完成基本的LNMP系统安装,简单配置维护:能够做基本的简单系统的PHP开发:能够在PHP中型系统中支 ...
- IHttpActionResult – new way of creating responses in ASP.NET Web API 2
先收藏这篇文章. http://www.strathweb.com/2013/06/ihttpactionresult-new-way-of-creating-responses-in-asp-net ...
- Centos6.5 下安装PostgreSQL9.4数据库
一.安装PostgreSQL源 CentOS 6.x 32bit rpm -Uvh http://yum.postgresql.org/9.4/redhat/rhel-6-i386/pgdg-cent ...
- Working With Taxonomy Field in CSOM
How to create taxonomy field with CSOM If you need to programmatic create a taxonomy field, you need ...
- Lingo 做线性规划 - Asset allocation and Portfolio models
Reference: <An Introduction to Management Science Quantitative Approaches to Decision Making, Rev ...
- 负margin在布局中的运用(*****************************************************************)
一.左右栏宽度固定,中间栏宽度自适应 <!DOCTYPE html> <html> <head lang="en"> <meta char ...
- Bootstrap3生成响应式的特价商品展示布局
在线演示 本地下载 在线前端调试地址,大家可以在线自己调试. 在线调试唯一地址:http://www.gbtags.com/gb/debug/fa35e396-0a04-4b73-9bfc-c6334 ...
- paip.windows io监控总结
paip.windows io监控总结 io的主要参数是个.disk queue length 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专 ...
- atitit.信息安全的控制总结o7
atitit.信息安全的控制总结o7 1. 信息安全覆盖很多的内容: 1 2. #内部人员导致的安全风险 1 3. #对敏感的数据进行透明的加密 2 4. #安全防护 2 5. #通过数据安全域保护关 ...