情况是这样的:A窗口中单击按钮时,通过类WindowB的静态方法showWindowB()显示B窗口。然后等待B窗口中的按钮单击后再输出“******”,但是while循环的存在使得B窗口不能正常显示。可是如果是在main方法中(而不像之前所述在单击按钮时调用)直接调用showWindowB()的话,B窗口又能正常显示。太奇怪了,不知道是为什么。要用什么办法才能使B窗口正常显示呢?那位大侠能帮帮我?一万个感谢!
类WindowA源码如下:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class WindowA extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
JButton jButton1 = new JButton();
public WindowA() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} public static void main(String[] args){
WindowA window = new WindowA();
} private void jbInit() throws Exception {
getContentPane().setLayout(borderLayout1);
this.setTitle("Window A");
jButton1.setText("Show window B");
jButton1.addActionListener(new WindowA_jButton1_actionAdapter(this));
this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
jPanel1.setLayout(borderLayout2);
jPanel1.add(jButton1, java.awt.BorderLayout.CENTER); this.setBounds(100,100,200,100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public void jButton1_actionPerformed(ActionEvent e) {
WindowB.showWindowB();
}
}class WindowA_jButton1_actionAdapter implements ActionListener {
private WindowA adaptee;
WindowA_jButton1_actionAdapter(WindowA adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}类WindowB源码如下:
package functionaldecomposition;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class WindowB extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
boolean isClicked = false;
JButton jButton1 = new JButton(); public WindowB() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public static void showWindowB(){
WindowB windowB = new WindowB();
while(!windowB.isClicked){
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
System.out.println("************");
} private void jbInit() throws Exception {
getContentPane().setLayout(borderLayout1);
jButton1.setText("Set Value");
jButton1.addActionListener(new WindowB_jButton1_actionAdapter(this));
this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
jPanel1.setLayout(borderLayout2);
jPanel1.add(jButton1, java.awt.BorderLayout.CENTER);
this.setTitle("Window B");
this.setBounds(200,200,200,100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public void jButton1_actionPerformed(ActionEvent e) {
isClicked = true;
}
}class WindowB_jButton1_actionAdapter implements ActionListener {
private WindowB adaptee;
WindowB_jButton1_actionAdapter(WindowB adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}注:我使用的是JBuilder 2005.
类WindowA源码如下:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;public class WindowA extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
JButton jButton1 = new JButton();
public WindowA() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} public static void main(String[] args){
WindowA window = new WindowA();
} private void jbInit() throws Exception {
getContentPane().setLayout(borderLayout1);
this.setTitle("Window A");
jButton1.setText("Show window B");
jButton1.addActionListener(new WindowA_jButton1_actionAdapter(this));
this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
jPanel1.setLayout(borderLayout2);
jPanel1.add(jButton1, java.awt.BorderLayout.CENTER); this.setBounds(100,100,200,100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public void jButton1_actionPerformed(ActionEvent e) {
WindowB.showWindowB();
}
}class WindowA_jButton1_actionAdapter implements ActionListener {
private WindowA adaptee;
WindowA_jButton1_actionAdapter(WindowA adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}类WindowB源码如下:
package functionaldecomposition;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class WindowB extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
boolean isClicked = false;
JButton jButton1 = new JButton(); public WindowB() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public static void showWindowB(){
WindowB windowB = new WindowB();
while(!windowB.isClicked){
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
System.out.println("************");
} private void jbInit() throws Exception {
getContentPane().setLayout(borderLayout1);
jButton1.setText("Set Value");
jButton1.addActionListener(new WindowB_jButton1_actionAdapter(this));
this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
jPanel1.setLayout(borderLayout2);
jPanel1.add(jButton1, java.awt.BorderLayout.CENTER);
this.setTitle("Window B");
this.setBounds(200,200,200,100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public void jButton1_actionPerformed(ActionEvent e) {
isClicked = true;
}
}class WindowB_jButton1_actionAdapter implements ActionListener {
private WindowB adaptee;
WindowB_jButton1_actionAdapter(WindowB adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}注:我使用的是JBuilder 2005.
想点WindowB窗口中的按钮出现"**********",做循环干什么?把System.out.println("******");放到按钮事件中去多好,按钮的监听做的也太费劲了(不要把主类传出去,又在外面调用主类内的方法),直接用匿名类或者主类实现接口都很方便。import javax.swing.*;
import java.awt.event.*;public class WindowA extends JFrame implements ActionListener {
private JPanel pane = null;
private JButton button = null; public WindowA() {
super("Window A");
pane = new JPanel();
button = new JButton("Show window B");
button.addActionListener(this);
this.getContentPane().add(pane);
pane.add(button);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
} public static void main(String[] args) {
new WindowA();
} public void actionPerformed(ActionEvent e) {
WindowB.showWindowB();
}
}class WindowB extends JFrame implements ActionListener {
private static WindowB windowB = null;
private JPanel pane = null;
private JButton button = null; public WindowB() {
super("Window B");
pane = new JPanel();
button = new JButton("Set Value");
button.addActionListener(this);
this.getContentPane().add(pane);
pane.add(button);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(200, 100);
this.setVisible(true);
} public static void showWindowB() {
if (windowB == null)
windowB = new WindowB();
} public void actionPerformed(ActionEvent e) {
System.out.println("************");
}
}
“你这是一个单线程的程序,没有循环完就不能正常显示窗口内的组件,你当然看不到了。”
我在main方法中使用showWindowB是没有问题的。只在通过别一个窗口的按钮显示时,只有一个主窗体,其它都没有了。而且在循环开始之前,构造器就应该建立好这个窗口B了吧。
监听这些东东及界面相关的很多,都是在JBuilder中画界面时自动生成的代码,JBuilder提供这样的方式,想必也有其原因,且不去管它是否有必要。
还有一点我忘记说明了,帖中的代码只不过是我在某个project中遇到的问题的一个抽象,希望各位热心的兄弟单纯地从技术角度来解决我所遇到的困惑,不必关注其合理性等问题。