自定义对话框JDialog的使用实例

转载

 http://blog.sina.com.cn/s/blog_4a45ab3b0100orv4.html

转载

标签: 

jdialog

 

it

分类: Java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class exp extends JFrame implements ActionListener

{

    int row = 6, col = 30;

    JPanel p1 = new JPanel(),p2 = new JPanel();

    JTextArea ta = new JTextArea("文本区行数:" + row + " 列数:" + col, row, col);

    JButton exit = new JButton("退出"),dialog = new JButton("对话框");

   

    exp()

    {

       setTitle("对话框父窗口");

       Container c = getContentPane();

       setSize(350,200);

       c.add("Center",p1);

       c.add("South",p2);

       p1.add(ta);

       p2.add(exit); p2.add(dialog);

       exit.addActionListener(this);

       dialog.addActionListener(this);

       setVisible(true);

    }

   

    public void actionPerformed(ActionEvent e)

    {

       if (e.getSource()==exit)

           System.exit(0);

       else

       {

           MyDialog dlg = new MyDialog(this,true);

           dlg.show();

       }

    }

   

    public static void main(String[] args)

    {

       new exp();

    }

   

    class MyDialog extends Dialog implements ActionListener

    {

       JLabel label1 = new JLabel("请输入行数");

       JLabel label2 = new JLabel("请输入列数");

       JTextField rows = new JTextField(50);

       JTextField columns = new JTextField(50);

       JButton ok = new JButton("确定");

       JButton cancel = new JButton("取消");

       MyDialog(exp parent, boolean modal)

       {

           super(parent,modal);

           setTitle("自定义对话框");

           setSize(260,140);

           setResizable(false);

           setLayout(null);

           add(label1);

           add(label2);

           label1.setBounds(50, 30, 65, 20);

           label2.setBounds(50, 60, 65, 20);

           add(rows);

           add(columns);

           rows.setText(Integer.toString(ta.getRows()));

           columns.setText(Integer.toString(ta.getColumns()));

           rows.setBounds(120, 30, 90, 20);

           columns.setBounds(120, 60, 90, 20);

           add(ok);

           add(cancel);

           ok.setBounds(60, 100, 60, 25);

           cancel.setBounds(140, 100, 60, 25);

           ok.addActionListener(this);

           cancel.addActionListener(this);

       }

      

       public void actionPerformed(ActionEvent e)

       {

           if (e.getSource()==ok)

           {

              int row = Integer.parseInt(rows.getText());

              int col = Integer.parseInt(columns.getText());

              ta.setRows(row);

              ta.setColumns(col);

              ta.setText("文本区行数:" + row + " 列数:" + col);

           }

           dispose();

       }

    }

}

 

运行结果:

自定义对话框JDialog的使用实例

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值