我正在尝试更改源的背景颜色,但是Java给了我发生内部致命错误的消息。你能告诉我我做错了什么吗?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    class ButtonPanel extends JPanel
    {
        private JButton yellowButton = new JButton("Yellow");

        public ButtonPanel()//constructor
        {

              //setLayout is a method in JPanel that is called in this constructor
              setLayout (new FlowLayout(FlowLayout.LEFT));
              add(yellowButton);
              actions actionsSpecified = new actions();

              yellowButton.addActionListener(actionsSpecified);
           }
        }

    class actions implements ActionListener
    {
        //actionPerformed is an interface that muse be overriden
        public void actionPerformed(ActionEvent evt)
        {
            // knows where the event is generated from(from a button, scrollbar etc...)
              JButton source = (JButton) evt.getSource();

              Color colorYellow = Color.yellow;
              source.setBackground(colorYellow);

           /*Color color = getContentPane().getBackground();
           if (source == yellowButton)
              color = Color.yellow;
           else if (source == blueButton)
              color = Color.blue;
           else if (source == redButton)
              color = Color.red;
           setBackground(color);
           repaint();*/
        }
    }


    class ButtonFrame extends JFrame
    {
            public ButtonFrame()
            {
                setTitle("ButtonTest");
                setSize(300, 200);
                addWindowListener(new WindowAdapter()
                 {  public void windowClosing(WindowEvent e)
                    {
                     System.exit(0);
                    }
                 } );

              Container contentPane = getContentPane();
              contentPane.add(new ButtonPanel());
          }
    }

    class ButtonTest
    {
        public static void main(String[] args)
        {
            JFrame frame = new ButtonFrame();
            frame.show();
        }
    }

错误信息 - - - - - - - - - - - - - - - - - - - - - -
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  Internal Error (classFileParser.cpp:3375), pid=1156, tid=6332
#  Error: ShouldNotReachHere()
#
# JRE version: 6.0_23-b05
# Java VM: Java HotSpot(TM) 64-Bit Server VM (19.0-b09 mixed mode windows-amd64 compressed oops)
# An error report file with more information is saved as:
# C:\Program Files (x86)\eclipse\android-sdk-windows\samples\android-8\Snake\hs_err_pid1156.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#

这是日志---------------------------------------------- -------------------
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  Internal Error (classFileParser.cpp:3375), pid=1156, tid=6332
#  Error: ShouldNotReachHere()
#
# JRE version: 6.0_23-b05
# Java VM: Java HotSpot(TM) 64-Bit Server VM (19.0-b09 mixed mode windows-amd64 compressed oops)
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#

---------------  T H R E A D  ---------------

Current thread (0x00000000005fb800):  JavaThread "Unknown thread" [_thread_in_vm, id=6332, stack(0x0000000002590000,0x0000000002690000)]

Stack: [0x0000000002590000,0x0000000002690000]
[error occurred during error reporting (printing stack bounds), id 0xc0000005]


---------------  P R O C E S S  ---------------

Java Threads: ( => current thread )

Other Threads:

=>0x00000000005fb800 (exited) JavaThread "Unknown thread" [_thread_in_vm, id=6332, stack(0x0000000002590000,0x0000000002690000)]

VM state:not at safepoint (not fully initialized)

VM Mutex/Monitor currently owned by a thread: None

Dynamic libraries:
0x0000000000400000 - 0x000000000042e000     C:\Program Files\Java\jre6\bin\javaw.exe
0x0000000077850000 - 0x00000000779f9000     C:\Windows\SYSTEM32\ntdll.dll
0x0000000077420000 - 0x000000007753f000     C:\Windows\system32\kernel32.dll
0x000007fefe010000 - 0x000007fefe07b000     C:\Windows\system32\KERNELBASE.dll
0x000007feff600000 - 0x000007feff6db000     C:\Windows\system32\ADVAPI32.dll
0x000007feffa40000 - 0x000007feffadf000     C:\Windows\system32\msvcrt.dll
0x000007feff6e0000 - 0x000007feff6ff000     C:\Windows\SYSTEM32\sechost.dll
0x000007feff2f0000 - 0x000007feff41d000     C:\Windows\system32\RPCRT4.dll
0x0000000077750000 - 0x000000007784a000     C:\Windows\system32\USER32.dll
0x000007feff590000 - 0x000007feff5f7000     C:\Windows\system32\GDI32.dll
0x000007feffa30000 - 0x000007feffa3e000     C:\Windows\system32\LPK.dll
0x000007feff780000 - 0x000007feff849000     C:\Windows\system32\USP10.dll
0x000007feff2c0000 - 0x000007feff2ee000     C:\Windows\system32\IMM32.DLL
0x000007fefe170000 - 0x000007fefe279000     C:\Windows\system32\MSCTF.dll
0x000007fefdc90000 - 0x000007fefdcaf000     C:\Windows\system32\nvinitx.dll
0x000000006d7f0000 - 0x000000006dee0000     C:\Program Files\Java\jre6\bin\server\jvm.dll
0x000007fefb930000 - 0x000007fefb96b000     C:\Windows\system32\WINMM.dll
0x000000006d760000 - 0x000000006d76e000     C:\Program Files\Java\jre6\bin\verify.dll
0x000000006d3b0000 - 0x000000006d3d7000     C:\Program Files\Java\jre6\bin\java.dll
0x000000006d310000 - 0x000000006d31a000     C:\Program Files\Java\jre6\bin\hpi.dll
0x0000000077a20000 - 0x0000000077a27000     C:\Windows\system32\PSAPI.DLL
0x000000006d7b0000 - 0x000000006d7c2000     C:\Program Files\Java\jre6\bin\zip.dll

VM Arguments:
jvm_args: -Dfile.encoding=Cp1252 -Xbootclasspath:C:\Program Files (x86)\eclipse\android-sdk-windows\platforms\android-8\android.jar
java_command: dalvik.system.TouchDex
Launcher Type: SUN_STANDARD

Environment Variables:
CLASSPATH=.;C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip
PATH=C:/Program Files/Java/jdk1.6.0_23/bin/../jre/bin/server;C:/Program Files/Java/jdk1.6.0_23/bin/../jre/bin;C:/Program Files/Java/jdk1.6.0_23/bin/../jre/lib/amd64;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files\Java\jdk1.6.0_23\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files\WIDCOMM\Bluetooth Software\;c:\Program Files\WIDCOMM\Bluetooth Software\syswow64;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\eclipse;
USERNAME=
OS=Windows_NT
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 37 Stepping 5, GenuineIntel



---------------  S Y S T E M  ---------------

OS: Windows 7 Build 7601 Service Pack 1

CPU:total 4 (2 cores per cpu, 2 threads per core) family 6 model 37 stepping 5, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, sse4.1, sse4.2, popcnt, ht

Memory: 4k page, physical 3920176k(1115300k free), swap 7838504k(4088064k free)

vm_info: Java HotSpot(TM) 64-Bit Server VM (19.0-b09) for windows-amd64 JRE (1.6.0_23-b05), built on Nov 13 2010 02:43:42 by "java_re" with MS VC++ 8.0 (VS2005)

time: Fri Jun 10 16:11:50 2011
elapsed time: 0 seconds

最佳答案

许多示例使用此模式来启动Swing应用程序。您可能想尝试一下。整个目的是在Swing线程内创建Swing组件。

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
             JFrame frame = new ButtonFrame();
             frame.show();
        }
    });
}

编辑:更仔细地浏览了问题之后。您不能在Android/Dalvik平台上运行Swing。正如其他人所说,如果在Sun JVM上运行,它可以很好地工作,您似乎正在Android模拟器下运行它。这是行不通的。

如果您要在Android上运行此代码,请更新您的代码以包括Android。

关于Java ShouldNotReachHere错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6312899/

10-14 00:04