Home Java javaTutorial Detailed explanation of usage examples of JDialog form in Java programming

Detailed explanation of usage examples of JDialog form in Java programming

Sep 09, 2017 am 10:34 AM
java use

This article mainly introduces the usage and examples of JDialog form in Java programming, describes its characteristics, and has certain reference value. Friends who need it can learn about it.

The JDialog form is a dialog box in the Swing component. It inherits the java.awt.Dialog class in the AWT component.

The function of JDialog form is to pop up another form from one form, just like the confirmation dialog box that pops up when using IE browser. JDialog is essentially another type of form. It is similar to the JFrame form. When using it, you also need to call the getContentPane() method to convert the form into a container, and then set the properties of the form in the container.

The following is a simple example:


import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

/**
 * 1:JDialog窗体时Swing组件中的对话框,
 * JDialog的功能就是是从一个窗体中弹出另一个窗体,就像是在使用浏览器时弹出的确定对话框一样
 * 
 * 2:JDialog窗体和JFrame窗体类似,在使用时也需要调用getContentPane()方法将
 * 窗体转化为容器,然后在容器中设置窗体的特性
 * 
 * 3:JDialog有五种构造方法,可以用来指定标题,窗体,和模式的对话框
 * @author biexiansheng
 *
 */
public class JDialogTest extends JDialog{
  
  public JDialogTest(){
    //实例化一个JDialog类对象,指定对话框的父窗体,窗体标题和类型
    super();
    Container container=getContentPane();
    container.setBackground(Color.green);
    container.add(new JLabel("这是一个对话框"));
    setBounds(120,120,100,100);
  }
  
  public void MyFrame(){
    JFrame jf=new JFrame();//实例化JFrame对象
    Container container=jf.getContentPane();//将窗体转化为容器
    JButton jb=new JButton("弹出对话框");
    jb.setBounds(10, 10, 100, 20);//设置按钮的大小
    jb.addActionListener(new ActionListener() {
      //定义匿名内部类,这样才可以点击出现反应
      @Override
      public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        new JDialogTest().setVisible(true);;
      }
    });
    container.add(jb);//将按钮添加到容器中,这点非常重要,不然无法显示
    //设置容器的结构的特性
    jf.setTitle("这是窗体转化为容器");
    jf.setSize(200,200);//设置容器的大小
    jf.setVisible(true);//使窗体可见
    //设置窗体的关闭模式
    jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    JDialogTest jd=new JDialogTest();
    jd.MyFrame();
  }

}
Copy after login

Let’s look at another one:


import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

/**
 * 1:按钮JButton
 * @author biexiansheng
 *
 */
public class MyFrame extends JFrame {

  public void MyFrame(){
    JFrame jf=new JFrame();//实例化一个JFrame对象
    Container container=jf.getContentPane();//将窗体转化为容器
    //Container container=getContentPane();
    container.setLayout(null);
    
    JLabel jl=new JLabel("这是一个JFrame窗体");//在窗体中设置标签
    jl.setHorizontalAlignment(JLabel.CENTER);//将标签中的文字置于标签中间的位置
    container.add(jl);//将标签添加到容器中
    
    JButton jb=new JButton("点我");//实例化一个按钮属性
    jb.setBounds(20, 20,100, 50);
    jb.addActionListener(new ActionListener() {
      
      @Override
      public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        //使MyJDialog窗体可见
        new MyJDialog(MyFrame.this).setVisible(true);
      //上面一句话使对话框窗体可见,这样就实现了当用户单机该按钮后将弹出对话框的功能
      }
    });
    container.add(jb);//将按钮属性添加到容器中
    
    //设置容器里面的属性特点
    container.setBackground(Color.blue);
    //设置容器的框架结构特性
    jf.setTitle("这是一个容器");//设置容器的标题
    jf.setVisible(true);//设置容器可视化
    jf.setSize(450, 400);//设置容器的大小
    //设置容器的关闭方式
    jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    MyFrame fm=new MyFrame();
    fm.MyFrame();
  }

}

class MyJDialog extends JDialog{
  //本实例代码可以看到,JDialog窗体和JFrame窗体形式基本相同,甚至在设置窗体的特性
  //时调用的方法名称都基本相同,如设置窗体的大小,设置窗体的关闭状态等
  public MyJDialog(MyFrame frame){//定义一个构造方法
    //实例化一个JDialog类对象,指定对话框的父窗体,窗体标题,和类型
    super(frame,"第一个JDialog窗体",true);
    Container container=getContentPane();//创建一个容器
    container.add(new JLabel("这是一个对话框"));//在容器中添加标签
    container.setBackground(Color.green);
    setBounds(120,120,100,100);
    
  }
}
Copy after login

This example can be seen that the JDialog form and the JFrame form are basically the same. Even the method names called when setting the characteristics of the form are basically the same, such as setting the form size, window size, etc. Body closed state, etc.

Summarize

The above is the detailed content of Detailed explanation of usage examples of JDialog form in Java programming. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles