我在一个类中写了个JFrame主窗口,另一个类中写了个子窗口JDialog,主窗口中有个按钮,点按钮就弹出这个子窗口。现在问题是要怎么样让这个子窗口始终跟随父窗口一起出现,而且子窗口始终都在父窗口上面??因为我试了一下,当弹出子窗口后,此时我如果打开另外的程序,比如打开我的电脑,然后回过来在点主窗口,就只有主窗口出现,子窗口并不显示。以下是我的2个简单的窗口程序。请高手指教,谢谢!!
主窗口程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame implements ActionListener{
JPanel panel = new JPanel();
JButton bt = new JButton();
public Test(){
try{
this.setSize(400,300);
this.setLocationRelativeTo(null);
jbInit();
}catch(Exception e){e.printStackTrace();}
}
public JFrame getJFrame(){
System.out.println(this);
return this;
}
public void go(){
this.show();
}
public static void main(String[] args) {
Test test = new Test();
test.go();
}
private void jbInit() throws Exception{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setEnabled(true);
this.setResizable(false);
this.setTitle("Test");
bt.setBounds(new Rectangle(76, 17, 107, 43));
bt.setText("qq");
bt.addActionListener(this);
panel.add(bt);
this.getContentPane().add(panel);
}
public void actionPerformed(ActionEvent e) {
String c =e.getActionCommand();
if(c.equals("qq")){
TestJDialog abc = new TestJDialog();
abc.goo();
}
}
}
子窗口程序:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;class TestJDialog {
Test test = new Test();
private JDialog dlg;
JPanel jPanel1 = new JPanel();
JTextField jTextField1 = new JTextField();
public TestJDialog() {
dlg = new JDialog(test.getJFrame(), "ww", true);
dlg.setSize(200,300);
dlg.setLocationRelativeTo(null);
dlg.getContentPane().setBackground(SystemColor.inactiveCaptionText);
dlg.setResizable(true);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
} public void goo(){
dlg.show();
} public void jbInit() throws Exception {
dlg.getContentPane().setBackground(SystemColor.control);
jPanel1.setBackground(SystemColor.inactiveCaptionText);
jPanel1.setPreferredSize(new Dimension(200, 300));
jPanel1.setLayout(null);
jTextField1.setBackground(new Color(227, 220, 226));
jTextField1.setFont(new java.awt.Font("Dialog", 0, 15));
jTextField1.setBorder(BorderFactory.createEtchedBorder());
jTextField1.setEditable(false);
jTextField1.setText("No.1");
jTextField1.setBounds(new Rectangle(7, 17, 48, 20));
dlg.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jTextField1, null);
}
}
主窗口程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame implements ActionListener{
JPanel panel = new JPanel();
JButton bt = new JButton();
public Test(){
try{
this.setSize(400,300);
this.setLocationRelativeTo(null);
jbInit();
}catch(Exception e){e.printStackTrace();}
}
public JFrame getJFrame(){
System.out.println(this);
return this;
}
public void go(){
this.show();
}
public static void main(String[] args) {
Test test = new Test();
test.go();
}
private void jbInit() throws Exception{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setEnabled(true);
this.setResizable(false);
this.setTitle("Test");
bt.setBounds(new Rectangle(76, 17, 107, 43));
bt.setText("qq");
bt.addActionListener(this);
panel.add(bt);
this.getContentPane().add(panel);
}
public void actionPerformed(ActionEvent e) {
String c =e.getActionCommand();
if(c.equals("qq")){
TestJDialog abc = new TestJDialog();
abc.goo();
}
}
}
子窗口程序:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;class TestJDialog {
Test test = new Test();
private JDialog dlg;
JPanel jPanel1 = new JPanel();
JTextField jTextField1 = new JTextField();
public TestJDialog() {
dlg = new JDialog(test.getJFrame(), "ww", true);
dlg.setSize(200,300);
dlg.setLocationRelativeTo(null);
dlg.getContentPane().setBackground(SystemColor.inactiveCaptionText);
dlg.setResizable(true);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
} public void goo(){
dlg.show();
} public void jbInit() throws Exception {
dlg.getContentPane().setBackground(SystemColor.control);
jPanel1.setBackground(SystemColor.inactiveCaptionText);
jPanel1.setPreferredSize(new Dimension(200, 300));
jPanel1.setLayout(null);
jTextField1.setBackground(new Color(227, 220, 226));
jTextField1.setFont(new java.awt.Font("Dialog", 0, 15));
jTextField1.setBorder(BorderFactory.createEtchedBorder());
jTextField1.setEditable(false);
jTextField1.setText("No.1");
jTextField1.setBounds(new Rectangle(7, 17, 48, 20));
dlg.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jTextField1, null);
}
}
解决方案 »
- 用ant能实现多线程编译吗?
- 怪!编译时不检查数组下标越界?
- notify和notifyAll为什么在sychronized(this)时,效果一样???
- 转贴(春节快到了,本人因经济困难,想做兼职)
- 抽象方法一定要在抽象类中吗?
- 关于JCreator添加API的问题
- 《java核心技术 卷1:基础知识》第六版的例子源码谁有吗??(即《Core java2 Volume1 Fundamentals》)
- java里坐标映射怎么做?
- 请教ssl协议与https协议的区别与联系?
- 程序解答,望各位帮忙,急!急!急!(在线等)
- 如何把cookie中的用户名直接放到用户名的文本框中
- 菜鸟请教一则正则表达式
|
+--java.awt.Component
|
+--java.awt.Container
|
+--java.awt.Window
|
+--java.awt.Dialog
|
+--javax.swing.JDialog
public TestJDialog(JFrame frame) {
dlg = new JDialog(frame, "ww", true);
dlg.setSize(200, 300);
dlg.setLocationRelativeTo(null);
dlg.getContentPane().setBackground(SystemColor.inactiveCaptionText);
dlg.setResizable(true);
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
然后在Test的监听中改变调用的构造函数:
public void actionPerformed(ActionEvent e) {
String c = e.getActionCommand();
if (c.equals("qq")) {
TestJDialog abc = new TestJDialog(this);
abc.goo();
}
}
这样就可以了。