A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 乔玉吉 中级黑马   /  2012-4-5 00:10  /  1796 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

第一个问题:
Frame = new Frame("my frame");
f.setBounds(300,100,600,400);

300是什么单位,像素还是毫米?

第二个问题:
如何设置一个对话框,让他的宽度随着标签中的提示信息的长度变化而变化,始终保持"确定"按钮在第二排(我用的是流式布局)

import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyWindowDemo
{
        private Frame f;
        private Button but;
        private TextField tf;
        private TextArea ta;
        private Dialog d;

        MyWindowDemo()
        {
                init();
        }

        public void init()
        {
                f = new Frame("my frame");
                f.setBounds(300,100,600,400);
                f.setLayout(new FlowLayout());

                but = new Button("转到");
                tf = new TextField(30);
                ta = new TextArea(10,50);
                f.add(tf);
                f.add(but);
                f.add(ta);

                myEvent();

                f.setVisible(true);
        }

        public void myEvent()
        {
               
                f.addWindowListener(new WindowAdapter()
                {
                        public void windowClosing(WindowEvent e)
                        {
                                System.exit(0);
                        }
                });

                but.addActionListener(new ActionListener()
                {
                        public void  actionPerformed(ActionEvent e)
                        {
                                showDir();
                        }
                });

                tf.addKeyListener(new KeyAdapter()
                {
                        public void keyPressed(KeyEvent e)
                        {
//                                if(e.getKeyText(e.getKeyCode()).equals("Enter"))
                                if(e.getKeyCode()==KeyEvent.VK_ENTER)
                                {
                                        showDir();
                                }
                        }
                });
        }

        private void showDir()
        {
                String dirPath = tf.getText();

                File dir = new File(dirPath);
                if(dir.exists()&&dir.isDirectory())
                {
                        ta.setText("");
                        String[] names = dir.list();
                        for(String name : names)
                        {
                                ta.append(name+"\r\n");
                        }
                }
                else
                {
                        d = new Dialog(f,"提示信息",true);
                        d.setBounds(435,200,350,100);//如何让他的宽度变成可变的,并随着info的变化而变化,始终保持okBut确定按钮在第二排显示

                        d.setResizable(false);//设置此 dialog 是否可以由用户调整大小。

                        d.setLayout(new FlowLayout());

                        String info = "找不到指定的路径   "+dirPath+"。  请确定路径地址正确.  \r\n";//因为这个dirPath的长度不是固定的
                        Label lab = new Label(info);

                        Button okBut = new Button("确定");
                       
                        d.add(lab);
                        d.add(okBut);

                        d.addWindowListener(new WindowAdapter()
                        {
                                public void windowClosing(WindowEvent e)
                                {
                                        d.setVisible(false);
                                }
                        });
                        okBut.addActionListener(new ActionListener()
                        {
                                public void actionPerformed(ActionEvent e)
                                {
                                        d.setVisible(false);
                                }
                        });

                        d.setVisible(true);
                }
        }

        public static void main(String[] args)
        {
                new MyWindowDemo();
        }
}

2 个回复

倒序浏览
第一个问题,单位是像素
第二个问题:
     不要把宽和高固定,设置为变量,动态获取
回复 使用道具 举报
格式化 发表于 2012-4-5 02:27
第一个问题,单位是像素
第二个问题:
     不要把宽和高固定,设置为变量,动态获取 ...

具体该如何实现啊?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马