主题:求教这个怎么改?
用的是Eclipse Java
Console:
java.lang.Error: Do not use Menu.add() use Menu.getContentPane().add() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
at javax.swing.JFrame.addImpl(JFrame.java:491)
at java.awt.Container.add(Container.java:518)
at Menu.<init>(Menu.java:92)
at Menu.main(Menu.java:164)
Exception in thread "main"
前两个是JFrame的问题。求教!要改的两行已标为红色,谢谢!
Ps:字数限制,删掉了部分注释
JFrame的代码:
package javax.swing;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeListener;
import java.util.Locale;
import java.util.Vector;
import java.io.Serializable;
import javax.accessibility.*;
public JFrame(String title) throws HeadlessException {
super(title);
frameInit();
}
public JFrame(String title, GraphicsConfiguration gc) {
super(title, gc);
frameInit();
}
/** Called by the constructors to init the <code>JFrame</code> properly. */
protected void frameInit() {
enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK);
setLocale( JComponent.getDefaultLocale() );
setRootPane(createRootPane());
setBackground(UIManager.getColor("control"));
setRootPaneCheckingEnabled(true);
if (JFrame.isDefaultLookAndFeelDecorated()) {
boolean supportsWindowDecorations =
UIManager.getLookAndFeel().getSupportsWindowDecorations();
if (supportsWindowDecorations) {
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
}
}
setFocusTraversalPolicy(KeyboardFocusManager.
getCurrentKeyboardFocusManager().
getDefaultFocusTraversalPolicy());
}
protected JRootPane createRootPane() {
return new JRootPane();
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
switch(defaultCloseOperation) {
case HIDE_ON_CLOSE:
setVisible(false);
break;
case DISPOSE_ON_CLOSE:
setVisible(false);
dispose();
break;
case DO_NOTHING_ON_CLOSE:
default:
break;
case EXIT_ON_CLOSE:
// This needs to match the checkExit call in
// setDefaultCloseOperation
System.exit(0);
break;
}
}
}
// public void setMenuBar(MenuBar menu) {
// throw new IllegalComponentStateException("Please use setJMenuBar() with JFrame.");
// }
public void setDefaultCloseOperation(int operation) {
if (operation != DO_NOTHING_ON_CLOSE &&
operation != HIDE_ON_CLOSE &&
operation != DISPOSE_ON_CLOSE &&
operation != EXIT_ON_CLOSE) {
throw new IllegalArgumentException("defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE");
}
if (this.defaultCloseOperation != operation) {
if (operation == EXIT_ON_CLOSE) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkExit(0);
}
}
int oldValue = this.defaultCloseOperation;
this.defaultCloseOperation = operation;
firePropertyChange("defaultCloseOperation", oldValue, operation);
}
}
public int getDefaultCloseOperation() {
return defaultCloseOperation;
}
public void update(Graphics g) {
paint(g);
}
public void setJMenuBar(JMenuBar menubar) {
getRootPane().setMenuBar(menubar);
}
public JMenuBar getJMenuBar() {
return getRootPane().getMenuBar();
}
protected boolean isRootPaneCheckingEnabled() {
return rootPaneCheckingEnabled;
}
protected void setRootPaneCheckingEnabled(boolean enabled) {
rootPaneCheckingEnabled = enabled;
}
private Error createRootPaneException(String op) {
String type = getClass().getName();
[color=FF0000]return new Error([/color]
"Do not use " + type + "." + op + "() use "
+ type + ".getContentPane()." + op + "() instead");
}
protected void addImpl(Component comp, Object constraints, int index)
{
if(isRootPaneCheckingEnabled()) {
[color=FF0000]throw createRootPaneException("add");[/color]
}
else {
super.addImpl(comp, constraints, index);
}
}
public void remove(Component comp) {
if (comp == rootPane) {
super.remove(comp);
} else {
// Client mistake, but we need to handle it to avoid a
// common object leak in client applications.
getContentPane().remove(comp);
}
}
public void setLayout(LayoutManager manager) {
if(isRootPaneCheckingEnabled()) {
throw createRootPaneException("setLayout");
}
else {
super.setLayout(manager);
}
}
public JRootPane getRootPane() {
return rootPane;
}
protected void setRootPane(JRootPane root)
{
if(rootPane != null) {
remove(rootPane);
}
rootPane = root;
if(rootPane != null) {
boolean checkingEnabled = isRootPaneCheckingEnabled();
try {
setRootPaneCheckingEnabled(false);
add(rootPane, BorderLayout.CENTER);
}
finally {
setRootPaneCheckingEnabled(checkingEnabled);
}
}
}
public Container getContentPane() {
return getRootPane().getContentPane();
}
public void setContentPane(Container contentPane) {
getRootPane().setContentPane(contentPane);
}
public JLayeredPane getLayeredPane() {
return getRootPane().getLayeredPane();
}
public void setLayeredPane(JLayeredPane layeredPane) {
getRootPane().setLayeredPane(layeredPane);
}
public Component getGlassPane() {
return getRootPane().getGlassPane();
}
public void setGlassPane(Component glassPane) {
getRootPane().setGlassPane(glassPane);
}
public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) {
if (defaultLookAndFeelDecorated) {
SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE);
} else {
SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE);
}
}
public static boolean isDefaultLookAndFeelDecorated() {
Boolean defaultLookAndFeelDecorated =
(Boolean) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey);
if (defaultLookAndFeelDecorated == null) {
defaultLookAndFeelDecorated = Boolean.FALSE;
}
return defaultLookAndFeelDecorated.booleanValue();
}
protected String paramString() {
String defaultCloseOperationString;
if (defaultCloseOperation == HIDE_ON_CLOSE) {
defaultCloseOperationString = "HIDE_ON_CLOSE";
} else if (defaultCloseOperation == DISPOSE_ON_CLOSE) {
defaultCloseOperationString = "DISPOSE_ON_CLOSE";
} else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) {
defaultCloseOperationString = "DO_NOTHING_ON_CLOSE";
} else if (defaultCloseOperation == 3) {
defaultCloseOperationString = "EXIT_ON_CLOSE";
} else defaultCloseOperationString = "";
String rootPaneString = (rootPane != null ?
rootPane.toString() : "");
String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ?
"true" : "false");
return super.paramString() +
",defaultCloseOperation=" + defaultCloseOperationString +
",rootPane=" + rootPaneString +
",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString;
}
/** The accessible context property. */
protected AccessibleContext accessibleContext = null;
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleJFrame();
}
return accessibleContext;
}
protected class AccessibleJFrame extends AccessibleAWTFrame {
// AccessibleContext methods
public String getAccessibleName() {
if (accessibleName != null) {
return accessibleName;
} else {
if (getTitle() == null) {
return super.getAccessibleName();
} else {
return getTitle();
}
}
}
public AccessibleStateSet getAccessibleStateSet() {
AccessibleStateSet states = super.getAccessibleStateSet();
if (isResizable()) {
states.add(AccessibleState.RESIZABLE);
}
if (getFocusOwner() != null) {
states.add(AccessibleState.ACTIVE);
}
// FIXME: [[[WDW - should also return ICONIFIED and ICONIFIABLE
// if we can ever figure these out]]]
return states;
}
} // inner class AccessibleJFrame
}
Console:
java.lang.Error: Do not use Menu.add() use Menu.getContentPane().add() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
at javax.swing.JFrame.addImpl(JFrame.java:491)
at java.awt.Container.add(Container.java:518)
at Menu.<init>(Menu.java:92)
at Menu.main(Menu.java:164)
Exception in thread "main"
前两个是JFrame的问题。求教!要改的两行已标为红色,谢谢!
Ps:字数限制,删掉了部分注释
JFrame的代码:
package javax.swing;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeListener;
import java.util.Locale;
import java.util.Vector;
import java.io.Serializable;
import javax.accessibility.*;
public JFrame(String title) throws HeadlessException {
super(title);
frameInit();
}
public JFrame(String title, GraphicsConfiguration gc) {
super(title, gc);
frameInit();
}
/** Called by the constructors to init the <code>JFrame</code> properly. */
protected void frameInit() {
enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK);
setLocale( JComponent.getDefaultLocale() );
setRootPane(createRootPane());
setBackground(UIManager.getColor("control"));
setRootPaneCheckingEnabled(true);
if (JFrame.isDefaultLookAndFeelDecorated()) {
boolean supportsWindowDecorations =
UIManager.getLookAndFeel().getSupportsWindowDecorations();
if (supportsWindowDecorations) {
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
}
}
setFocusTraversalPolicy(KeyboardFocusManager.
getCurrentKeyboardFocusManager().
getDefaultFocusTraversalPolicy());
}
protected JRootPane createRootPane() {
return new JRootPane();
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
switch(defaultCloseOperation) {
case HIDE_ON_CLOSE:
setVisible(false);
break;
case DISPOSE_ON_CLOSE:
setVisible(false);
dispose();
break;
case DO_NOTHING_ON_CLOSE:
default:
break;
case EXIT_ON_CLOSE:
// This needs to match the checkExit call in
// setDefaultCloseOperation
System.exit(0);
break;
}
}
}
// public void setMenuBar(MenuBar menu) {
// throw new IllegalComponentStateException("Please use setJMenuBar() with JFrame.");
// }
public void setDefaultCloseOperation(int operation) {
if (operation != DO_NOTHING_ON_CLOSE &&
operation != HIDE_ON_CLOSE &&
operation != DISPOSE_ON_CLOSE &&
operation != EXIT_ON_CLOSE) {
throw new IllegalArgumentException("defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE");
}
if (this.defaultCloseOperation != operation) {
if (operation == EXIT_ON_CLOSE) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkExit(0);
}
}
int oldValue = this.defaultCloseOperation;
this.defaultCloseOperation = operation;
firePropertyChange("defaultCloseOperation", oldValue, operation);
}
}
public int getDefaultCloseOperation() {
return defaultCloseOperation;
}
public void update(Graphics g) {
paint(g);
}
public void setJMenuBar(JMenuBar menubar) {
getRootPane().setMenuBar(menubar);
}
public JMenuBar getJMenuBar() {
return getRootPane().getMenuBar();
}
protected boolean isRootPaneCheckingEnabled() {
return rootPaneCheckingEnabled;
}
protected void setRootPaneCheckingEnabled(boolean enabled) {
rootPaneCheckingEnabled = enabled;
}
private Error createRootPaneException(String op) {
String type = getClass().getName();
[color=FF0000]return new Error([/color]
"Do not use " + type + "." + op + "() use "
+ type + ".getContentPane()." + op + "() instead");
}
protected void addImpl(Component comp, Object constraints, int index)
{
if(isRootPaneCheckingEnabled()) {
[color=FF0000]throw createRootPaneException("add");[/color]
}
else {
super.addImpl(comp, constraints, index);
}
}
public void remove(Component comp) {
if (comp == rootPane) {
super.remove(comp);
} else {
// Client mistake, but we need to handle it to avoid a
// common object leak in client applications.
getContentPane().remove(comp);
}
}
public void setLayout(LayoutManager manager) {
if(isRootPaneCheckingEnabled()) {
throw createRootPaneException("setLayout");
}
else {
super.setLayout(manager);
}
}
public JRootPane getRootPane() {
return rootPane;
}
protected void setRootPane(JRootPane root)
{
if(rootPane != null) {
remove(rootPane);
}
rootPane = root;
if(rootPane != null) {
boolean checkingEnabled = isRootPaneCheckingEnabled();
try {
setRootPaneCheckingEnabled(false);
add(rootPane, BorderLayout.CENTER);
}
finally {
setRootPaneCheckingEnabled(checkingEnabled);
}
}
}
public Container getContentPane() {
return getRootPane().getContentPane();
}
public void setContentPane(Container contentPane) {
getRootPane().setContentPane(contentPane);
}
public JLayeredPane getLayeredPane() {
return getRootPane().getLayeredPane();
}
public void setLayeredPane(JLayeredPane layeredPane) {
getRootPane().setLayeredPane(layeredPane);
}
public Component getGlassPane() {
return getRootPane().getGlassPane();
}
public void setGlassPane(Component glassPane) {
getRootPane().setGlassPane(glassPane);
}
public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) {
if (defaultLookAndFeelDecorated) {
SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE);
} else {
SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE);
}
}
public static boolean isDefaultLookAndFeelDecorated() {
Boolean defaultLookAndFeelDecorated =
(Boolean) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey);
if (defaultLookAndFeelDecorated == null) {
defaultLookAndFeelDecorated = Boolean.FALSE;
}
return defaultLookAndFeelDecorated.booleanValue();
}
protected String paramString() {
String defaultCloseOperationString;
if (defaultCloseOperation == HIDE_ON_CLOSE) {
defaultCloseOperationString = "HIDE_ON_CLOSE";
} else if (defaultCloseOperation == DISPOSE_ON_CLOSE) {
defaultCloseOperationString = "DISPOSE_ON_CLOSE";
} else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) {
defaultCloseOperationString = "DO_NOTHING_ON_CLOSE";
} else if (defaultCloseOperation == 3) {
defaultCloseOperationString = "EXIT_ON_CLOSE";
} else defaultCloseOperationString = "";
String rootPaneString = (rootPane != null ?
rootPane.toString() : "");
String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ?
"true" : "false");
return super.paramString() +
",defaultCloseOperation=" + defaultCloseOperationString +
",rootPane=" + rootPaneString +
",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString;
}
/** The accessible context property. */
protected AccessibleContext accessibleContext = null;
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleJFrame();
}
return accessibleContext;
}
protected class AccessibleJFrame extends AccessibleAWTFrame {
// AccessibleContext methods
public String getAccessibleName() {
if (accessibleName != null) {
return accessibleName;
} else {
if (getTitle() == null) {
return super.getAccessibleName();
} else {
return getTitle();
}
}
}
public AccessibleStateSet getAccessibleStateSet() {
AccessibleStateSet states = super.getAccessibleStateSet();
if (isResizable()) {
states.add(AccessibleState.RESIZABLE);
}
if (getFocusOwner() != null) {
states.add(AccessibleState.ACTIVE);
}
// FIXME: [[[WDW - should also return ICONIFIED and ICONIFIABLE
// if we can ever figure these out]]]
return states;
}
} // inner class AccessibleJFrame
}