Home > Java > javaTutorial > body text

Detailed explanation of usage examples of JDialog form in Java programming

黄舟
Release: 2017-09-09 10:34:48
Original
6205 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template