GUI 中的 StackOverflow class
StackOverflow in GUI class
谁能解释一下我收到错误消息的原因...
我有两个 classes,一个用于创建 GUI,第二个 class 用于处理事件。源码如下所示...
CLASS 1
import javax.swing.*;
import java.awt.*;
public class Motion extends JFrame {
MotionEvent controller = new MotionEvent();
//row 0
JPanel row0 = new JPanel();
//row 1
JPanel row1 = new JPanel();
JButton up = new JButton("Up");
//row 2
JPanel row2 = new JPanel();
JButton left = new JButton("Left");
JButton right = new JButton("Right");
JCheckBox compassFormat = new JCheckBox("compassFormat", false);
//row 3
JPanel row3 = new JPanel();
JButton down = new JButton("down");
Motion(){
super("Motion Detector");
setSize(500,325);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layoutMaster = new GridLayout(5,1,10,10);
setLayout(layoutMaster);
add(row0);
FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER);
row1.setLayout(layout1);
up.addActionListener(controller);
row1.add(up);
add(row1);
GridLayout layout2 = new GridLayout(1, 3, 10, 10);
row2.setLayout(layout2);
left.addActionListener(controller);
//compassFormat.addItemListener(controller);
right.addActionListener(controller);
row2.add(left);
row2.add(compassFormat);
row2.add(right);
add(row2);
FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER);
row3.setLayout(layout3);
row3.add(down);
add(row3);
setVisible(true);
}
public static void main(String[] args){
Motion createGui = new Motion();
}
}
CLASS 2
import java.awt.event.*;
import javax.swing.*;
public class MotionEvent implements ActionListener/*, ItemListener */{
Motion motionObj = new Motion();
public void actionPerformed(ActionEvent event){
Object objSource = event.getSource();
if(objSource == motionObj.up){
JOptionPane.showMessageDialog(null, "You have moved up");
}
else if(objSource == motionObj.down){
JOptionPane.showMessageDialog(null, "You have moved down");
}
else if(objSource == motionObj.left){
JOptionPane.showMessageDialog(null, "You have moved left");
}
else if(objSource == motionObj.right){
JOptionPane.showMessageDialog(null, "You have moved right", "Navigator", JOptionPane.INFORMATION_MESSAGE);
}
}
//public void itemStateChanged(ItemEvent event){
}//
收到的错误信息是:
Exception in thread "main" java.lang.WhosebugError
at sun.awt.Win32GraphicsConfig.getBounds(Native Method)
at sun.awt.Win32GraphicsConfig.getBounds(Unknown Source)
at java.awt.Window.init(Unknown Source)
at java.awt.Window.<init>(Unknown Source)
at java.awt.Frame.<init>(Unknown Source)
at javax.swing.JFrame.<init>(Unknown Source)
at Motion.<init>(Motion.java:26)
at MotionEvent.<init>(MotionEvent.java:5)
at Motion.<init>(Motion.java:6)
Motion.java:6 & MotionEvent.java:5 引用引用变量的实例化,他们有什么问题吗?
存在无限循环running.MeansMotion
无限创建
每当创建 Motion
的对象时,也会创建 MotionEvent
的对象
public class Motion extends JFrame {
MotionEvent controller = new MotionEvent();
每当创建 MotionEvent
的对象时,都会创建 Motion
的对象
public class MotionEvent implements ActionListener/*, ItemListener */{
Motion motionObj = new Motion();//which will initiate endless call for this.Remove this
这反过来会导致无休止地创建这两个对象,这显然会导致 Whosebug 错误。
移除
这是对 Motion
对象的递归调用,对您的 MotionEvent 使用以下内容 class
import java.awt.event.*;
import javax.swing.*;
public class MotionEvent implements ActionListener/*, ItemListener */{
public void actionPerformed(ActionEvent event){
Object objSource = event.getActionCommand();
if(objSource.equals("Up")){
JOptionPane.showMessageDialog(null, "You have moved up");
}
else if(objSource .equals("Down")){
JOptionPane.showMessageDialog(null, "You have moved down");
}
else if(objSource.equals("Left")){
JOptionPane.showMessageDialog(null, "You have moved left");
}
else if(objSource.equals("Right")){
JOptionPane.showMessageDialog(null, "You have moved right", "Navigator", JOptionPane.INFORMATION_MESSAGE);
}
}
//public void itemStateChanged(ItemEvent event){
}//
谁能解释一下我收到错误消息的原因...
我有两个 classes,一个用于创建 GUI,第二个 class 用于处理事件。源码如下所示...
CLASS 1
import javax.swing.*;
import java.awt.*;
public class Motion extends JFrame {
MotionEvent controller = new MotionEvent();
//row 0
JPanel row0 = new JPanel();
//row 1
JPanel row1 = new JPanel();
JButton up = new JButton("Up");
//row 2
JPanel row2 = new JPanel();
JButton left = new JButton("Left");
JButton right = new JButton("Right");
JCheckBox compassFormat = new JCheckBox("compassFormat", false);
//row 3
JPanel row3 = new JPanel();
JButton down = new JButton("down");
Motion(){
super("Motion Detector");
setSize(500,325);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layoutMaster = new GridLayout(5,1,10,10);
setLayout(layoutMaster);
add(row0);
FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER);
row1.setLayout(layout1);
up.addActionListener(controller);
row1.add(up);
add(row1);
GridLayout layout2 = new GridLayout(1, 3, 10, 10);
row2.setLayout(layout2);
left.addActionListener(controller);
//compassFormat.addItemListener(controller);
right.addActionListener(controller);
row2.add(left);
row2.add(compassFormat);
row2.add(right);
add(row2);
FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER);
row3.setLayout(layout3);
row3.add(down);
add(row3);
setVisible(true);
}
public static void main(String[] args){
Motion createGui = new Motion();
}
}
CLASS 2
import java.awt.event.*;
import javax.swing.*;
public class MotionEvent implements ActionListener/*, ItemListener */{
Motion motionObj = new Motion();
public void actionPerformed(ActionEvent event){
Object objSource = event.getSource();
if(objSource == motionObj.up){
JOptionPane.showMessageDialog(null, "You have moved up");
}
else if(objSource == motionObj.down){
JOptionPane.showMessageDialog(null, "You have moved down");
}
else if(objSource == motionObj.left){
JOptionPane.showMessageDialog(null, "You have moved left");
}
else if(objSource == motionObj.right){
JOptionPane.showMessageDialog(null, "You have moved right", "Navigator", JOptionPane.INFORMATION_MESSAGE);
}
}
//public void itemStateChanged(ItemEvent event){
}//
收到的错误信息是:
Exception in thread "main" java.lang.WhosebugError
at sun.awt.Win32GraphicsConfig.getBounds(Native Method)
at sun.awt.Win32GraphicsConfig.getBounds(Unknown Source)
at java.awt.Window.init(Unknown Source)
at java.awt.Window.<init>(Unknown Source)
at java.awt.Frame.<init>(Unknown Source)
at javax.swing.JFrame.<init>(Unknown Source)
at Motion.<init>(Motion.java:26)
at MotionEvent.<init>(MotionEvent.java:5)
at Motion.<init>(Motion.java:6)
Motion.java:6 & MotionEvent.java:5 引用引用变量的实例化,他们有什么问题吗?
存在无限循环running.MeansMotion
无限创建
每当创建 Motion
的对象时,也会创建 MotionEvent
的对象
public class Motion extends JFrame {
MotionEvent controller = new MotionEvent();
每当创建 MotionEvent
的对象时,都会创建 Motion
的对象
public class MotionEvent implements ActionListener/*, ItemListener */{
Motion motionObj = new Motion();//which will initiate endless call for this.Remove this
这反过来会导致无休止地创建这两个对象,这显然会导致 Whosebug 错误。 移除
这是对 Motion
对象的递归调用,对您的 MotionEvent 使用以下内容 class
import java.awt.event.*;
import javax.swing.*;
public class MotionEvent implements ActionListener/*, ItemListener */{
public void actionPerformed(ActionEvent event){
Object objSource = event.getActionCommand();
if(objSource.equals("Up")){
JOptionPane.showMessageDialog(null, "You have moved up");
}
else if(objSource .equals("Down")){
JOptionPane.showMessageDialog(null, "You have moved down");
}
else if(objSource.equals("Left")){
JOptionPane.showMessageDialog(null, "You have moved left");
}
else if(objSource.equals("Right")){
JOptionPane.showMessageDialog(null, "You have moved right", "Navigator", JOptionPane.INFORMATION_MESSAGE);
}
}
//public void itemStateChanged(ItemEvent event){
}//