/* * Created By Jennie on 2003-6-22 * * History */ package swing;import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame;/** * @author Jennie * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class TestDialog { private class CmdAction implements ActionListener{ private JFrame f ; public CmdAction(JFrame f) { this.f = f; } public void actionPerformed(ActionEvent e) { JDialog d = new JDialog(f,true); d.setBounds(100,100,100,100); d.show(); } } public CmdAction getAction(JFrame f) { return new CmdAction(f); } public static void main(String[] args) { TestDialog td = new TestDialog(); JFrame f = new JFrame(); f.setBounds(100,100,200,100); JButton btn = new JButton("ok"); btn.addActionListener(td.getAction(f)); f.getContentPane().add(btn); f.show(true); } }
莫modal 为true 时其父窗口不能被激活,false 时可以
用色他setAlwaysOnTop(true);将其置于顶端;
new它后能不能通过set什么来实现?
还有能不能让主frame和windows窗口一样,在工具栏上点一下最小化,再点一下还原啊?java默认的frame不是的
难道只能用alt+tab切换才行?
* Created By Jennie on 2003-6-22
*
* History
*/
package swing;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;/**
* @author Jennie
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TestDialog {
private class CmdAction implements ActionListener{
private JFrame f ;
public CmdAction(JFrame f)
{
this.f = f;
}
public void actionPerformed(ActionEvent e)
{
JDialog d = new JDialog(f,true);
d.setBounds(100,100,100,100);
d.show();
}
}
public CmdAction getAction(JFrame f)
{
return new CmdAction(f);
}
public static void main(String[] args) {
TestDialog td = new TestDialog();
JFrame f = new JFrame();
f.setBounds(100,100,200,100);
JButton btn = new JButton("ok");
btn.addActionListener(td.getAction(f));
f.getContentPane().add(btn);
f.show(true);
}
}
用这个初始化public JDialog(Frame owner, boolean modal),指明是model的
可以锁住owner
当然是弹出dialog之后啦!你试试弹出对话框的情况!
如果这个dialog不是在本frame类中创建弹出的,到哪里去找那个owner呢?
难道还把frame owner传过去?那不会太多吗?
或者在mainFrame里实例化那个dialog?那程序就很乱了。
有什么好办法啊?我思路太狭隘了吧
当然,这样确实锁住了,但问题是切换后,dialog跑到主frame后了,我如何能取到它的owner呢?
假设Frame类为TestFrame,Dialog类为TestDialog。
TestDialog的构造方法可以这样写:
public TestDialog(JFrame parent){
super(parent,true);
//other codes
}
在TestFrame这样实例化:
TestDialog dialog = new TestDialog(this);
而且程序风格是TestDialog里有个static方法show(),在自己的show()方法里实例化,在TestFrame里调用TestDialog.show(),虽然可以在show()里传,但是。
再看看jdk了,不行也没法了
我现在只能让TestFrame里面new的frame变成public static的了,这样在其他Dialog的构造函数里调用super(TestFrame.frame, true)!问题解决