Java 为什么我用jpanel在不同的计算机上得到不同的结果?

Java 为什么我用jpanel在不同的计算机上得到不同的结果?,java,swing,jpanel,Java,Swing,Jpanel,我用java和jpanel编写了一些程序。 但是我在不同的操作系统(WindowsXP,MacOSX)中得到不同的结果(颜色,边框…)。 为什么会这样?Windows和OSx有不同的主题。因为您使用默认的LookAndFeel,所以您得到了不同的视图 尝试com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel 作为补充说明,您还可以使用根据操作系统更改样式的类 它不支持OSx,但您可以修改它 public final class Common {

我用java和jpanel编写了一些程序。 但是我在不同的操作系统(WindowsXP,MacOSX)中得到不同的结果(颜色,边框…)。
为什么会这样?

Windows和OSx有不同的主题。因为您使用默认的LookAndFeel,所以您得到了不同的视图

尝试
com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel

作为补充说明,您还可以使用根据操作系统更改样式的类

它不支持OSx,但您可以修改它

public final class Common
    {
    // ------------------------------ CONSTANTS ------------------------------

    private static final boolean DEBUGGING = false;

    // -------------------------- PUBLIC STATIC METHODS --------------------------

    /**
     * set the look and feel for a Swing App, based on OS and JDK version..
     */
    public static void setLaf(){
                                    //"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
                                    //"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
        if ( setLaf( null, 1, 6, 0, "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" ) )
            {
            // won't take for anything less than 1.6.0_10
            return;
            }

        if ( setLaf( "Windows Vista", 1, 3, 0, UIManager.getCrossPlatformLookAndFeelClassName() ) )
            {
            return;
            }
        if ( setLaf( "Windows XP", 1, 3, 0, UIManager.getCrossPlatformLookAndFeelClassName() ) )
            {
            return;
            }

        setLaf( null, 1, 3, 0, UIManager.getSystemLookAndFeelClassName() );
        // if even that failed, give up. leave original L&F in place.
        }

    /**
     * set the look and feel for a Swing App, based on OS and JDK version..
     */
    public static void setLafNim(){
                                    //"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
                                    //"com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
        if ( setLaf( null, 1, 6, 0, "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" ) )
            {
            // won't take for anything less than 1.6.0_10
            return;
            }

        if ( setLaf( "Windows Vista", 1, 3, 0, UIManager.getCrossPlatformLookAndFeelClassName() ) )
            {
            return;
            }
        if ( setLaf( "Windows XP", 1, 3, 0, UIManager.getCrossPlatformLookAndFeelClassName() ) )
            {
            return;
            }

        setLaf( null, 1, 3, 0, UIManager.getSystemLookAndFeelClassName() );
        // if even that failed, give up. leave original L&F in place.
        }

    // -------------------------- STATIC METHODS --------------------------

    /**
     * Attempt to set the LAF, depending on OS and JDK being suitable.
     *
     * @param os           name of OS, null means it does not matter.
     * @param wantedMajor  java major version e.g. 1
     * @param wantedMinor  Java minor version e.g. 6
     * @param wantedBugFix Java bugfix version e.g. 14
     * @param lafClassName class name of laf to set if os matches and JDK version is sufficiently advanced.
     *
     * @return true the setting took, false if failed.
     */
    private static boolean setLaf( String os, int wantedMajor,
                                   int wantedMinor,
                                   int wantedBugFix,
                                   String lafClassName )
        {
        if ( os != null && !System.getProperty( "os.name", "unknown" ).equals( os ) )
            {
            return false;
            }

        try
            {
            UIManager.setLookAndFeel( lafClassName );
            }
        catch ( UnsupportedLookAndFeelException e )
            {
            if ( DEBUGGING )
                {
                System.err.println( "fail: UnsupportedLookAndFeelException" + lafClassName + e.getMessage() );
                }
            return false;
            }
        catch ( IllegalAccessException e )
            {
            if ( DEBUGGING )
                {
                System.err.println( "fail: IllegalAccessException" + lafClassName + e.getMessage() );
                }
            return false;
            }
        catch ( InstantiationException e )
            {
            if ( DEBUGGING )
                {
                System.err.println( "fail: InstantiationException" + lafClassName + e.getMessage() );
                }
            return false;
            }
        catch ( ClassNotFoundException e )
            {
            if ( DEBUGGING )
                {
                System.err.println( "fail: ClassNotFoundException" + lafClassName + e.getMessage() );
                }
            return false;
            }
        catch ( Exception e )
            {
            if ( DEBUGGING )
                {
                System.err.println( "fail: Exception" + lafClassName + e.getMessage() );
                }
            return false;
            }
        if ( DEBUGGING )
            {
            System.err.println( "Choosing L&F " + lafClassName );
            }
        return true;
        }

    }

因为,jpanel正在使用OS窗口管理器布局