第一个问题:
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();
}
}
|