1

我在添加物质外观和感觉时遇到了麻烦,这是我第一次尝试,我想添加石墨玻璃,但它不起作用这是我的代码:

 package loader;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.skin.GraphiteGlassSkin;
import org.pushingpixels.substance.api.skin.GraphiteSkin;
import org.pushingpixels.substance.api.skin.SubstanceGraphiteGlassLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceGraphiteLookAndFeel;
import org.pushingpixels.substance.api.skin.SubstanceRavenLookAndFeel;
import org.pushingpixels.substance.api.skin.TwilightSkin;

public class Loader extends JWindow implements ActionListener {
    private static URLClassLoader classLoader;
    private static int downloaded; // number of bytes downloaded
    // to
    // splash
    // image
    private final static String FRAME_IMAGE = "http://i.imgur.com/UW5ZfQ5.jpg"; // icon
                                                                                // for
                                                                                // the
                                                                                // frame
    private final static String JAR_URL = "http://chefJones.com/play/client.jar"; // link
    // to
    // jar
    final static Loader loader = new Loader();
    private final static Logger logger = Logger.getLogger(Loader.class
            .getName());
    private static String MAIN_CLASS = "RunClient"; // put your jars main class
    // here
    private static JProgressBar progressBar;
    /**
     * 
     */
    private static final long serialVersionUID = -1162207878136245145L;
    private static int size; // size of download in bytes
    private static JDialog splash;
    private final static String SPLASH_IMAGE = "http://i.imgur.com/wPH1YJb.png"; // link

    private static float getProgress() {
        return ((float) downloaded / size) * 100;
    }

    private static void grabJAR() throws ClassNotFoundException,
            InstantiationException, IllegalAccessException, IOException,
            UnsupportedLookAndFeelException {
        final URL url = new URL(Loader.JAR_URL);
        final InputStream is = url.openStream();
        final byte[] b = new byte[2048];
        int length;
        final HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();

        // Specify what portion of file to download.
        connection.setRequestProperty("Range", "bytes=" + downloaded + "-");

        // Connect to server.
        connection.connect();

        // Make sure response code is in the 200 range.
        if ((connection.getResponseCode() / 100) != 2) {
            logger.info("Unable to find file");
            return;
        }

        // set content length.
        size = connection.getContentLength();
        while ((length = is.read(b)) != -1) {
            downloaded += length;
            progressBar.setValue((int) getProgress()); // set progress bar
        }
        is.close();
        classLoader = new URLClassLoader(new URL[] { (url) });
        final Applet client = (Applet) classLoader.loadClass(MAIN_CLASS)
                .newInstance();
        client.init();
        client.start();
        loader.loadClient(client);
        setFrameTheme();

    }

    protected static void hideSplashScreen() {
        splash.setVisible(false);
        splash.dispose();
    }

    public static void main(final String... args)
            throws InstantiationException, IllegalAccessException,
            ClassNotFoundException, IOException,
            UnsupportedLookAndFeelException {
         try {
                JFrame.setDefaultLookAndFeelDecorated(true);

                SubstanceLookAndFeel.setSkin(new GraphiteSkin());
                SubstanceLookAndFeel.setSkin("org.pushingpixels.substance.api.skin.GraphiteSkin");
                UIManager.setLookAndFeel(new SubstanceGraphiteLookAndFeel());
                UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceGraphiteLookAndFeel");

            } catch (Exception e) {
                e.printStackTrace();
            }

        try {
            showSplashScreen();
        } catch (final MalformedURLException e) {
            logger.severe("Encounter error: " + e.getClass());
            logger.severe("Error message: " + e.getMessage());
            logger.severe("Error cause: " + e.getCause());
        }
        final SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {

            @Override
            protected Void doInBackground() throws Exception {
                grabJAR();
                return null;
            }

            @Override
            protected void done() {

                hideSplashScreen();
            }

        };
        worker.execute();

    }

    private static void setFrameTheme() throws ClassNotFoundException,
            InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException

    {
        try {
            UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.GraphiteGlassLookAndFeel"); 
        } catch (final Exception e) {
            UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.GraphiteGlassLookAndFeel"); 

        }
    }

    protected static void showSplashScreen() throws MalformedURLException {
        splash = new JDialog((Frame) null);
        splash.setModal(false);
        splash.setAlwaysOnTop(true);
        splash.setUndecorated(true);
        final JLabel background = new JLabel(new ImageIcon(
                new URL(SPLASH_IMAGE)));
        background.setOpaque(true);
        background.setLayout(new BorderLayout());
        splash.getContentPane().add(background);
        final JLabel text = new JLabel("Loading, please wait...");
        text.setFont(new Font("Segoe UI Light", Font.BOLD | Font.PLAIN, 15));
        text.setHorizontalAlignment(SwingConstants.CENTER);
        text.setForeground(Color.WHITE);
        text.setBorder(BorderFactory.createEmptyBorder(100, 50, 100, 50));
        background.add(text);
        progressBar = new JProgressBar();
        background.add(progressBar, BorderLayout.SOUTH);
        splash.pack();
        splash.setLocationRelativeTo(null);
        splash.setVisible(true);
    }

    private JButton Button1;

    private JButton Button2;

    private JButton Button3;
    private JButton Button6;

    private JFrame clientFrame;

    private final JPanel clientPanel = new JPanel();
    private LayoutManager Layout;

    public JPanel totalPanel;

    @Override
    public void actionPerformed(final ActionEvent actionevent) {
        final String s = actionevent.getActionCommand();

        if (s.equals("Vote")) {
            LoaderUtils.openURL("http://chefJones.com/vote/");
        } else if (s.equals("Hiscores")) {
            LoaderUtils.openURL("http://chefJones.com/hiscores");
        } else if (s.equals("Forum")) {
            LoaderUtils.openURL("http://chefJones.com/forums");
        } else

        if (s.equals("Store")) {
            LoaderUtils.openURL("http://chefJones.com/store");
        } else if (s.equals("Screenshot")) {
            LoaderUtils.takeScreenShot();
        }
    }

    private void loadClient(final Applet client) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException, IOException,
            UnsupportedLookAndFeelException {
        clientFrame = new JFrame("Loader");
        clientFrame
                .setIconImage(new ImageIcon(new URL(FRAME_IMAGE)).getImage());
        clientFrame.getContentPane().setLayout(new BorderLayout());
        clientPanel.setLayout(new BorderLayout());
        clientPanel.add(client);
        clientPanel.setPreferredSize(new Dimension(765, 503));
        clientFrame.getContentPane().add(clientPanel, "Center");
        clientFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        clientFrame.pack();
        clientFrame.setVisible(true);
        setVisible(false);
        final JMenuBar jmenubar = new JMenuBar();
        clientFrame.setJMenuBar(jmenubar);
        Layout = new FlowLayout();
        JFrame.setDefaultLookAndFeelDecorated(true);
        /*JDialog.setDefaultLookAndFeelDecorated(true);
        Button1 = new JButton("Screenshot");
        Button2 = new JButton("Vote");
        Button3 = new JButton("Forum");
        Button6 = new JButton("Upgrades");
        jmenubar.setLayout(Layout);
        jmenubar.add(Button2);
        jmenubar.add(Button3);
        jmenubar.add(Button6);
        jmenubar.add(Button1);
        Button2.addActionListener(this);
        Button3.addActionListener(this);
        Button6.addActionListener(this);
        Button1.addActionListener(this);
        Button2.setBackground(Color.BLACK);
        Button2.setForeground(Color.ORANGE);
        Button2.setText("Forum");
        Button3.setBackground(Color.BLACK);
        Button3.setForeground(Color.ORANGE);
        Button3.setText("Vote");
        Button6.setBackground(Color.BLACK);
        Button6.setForeground(Color.ORANGE);
        Button6.setText("Upgrades");
        Button1.setBackground(Color.BLACK);
        Button1.setForeground(Color.ORANGE);
        Button1.setText("Screenshot");*/
        setFrameTheme();
    }

}

我得到错误:

java.lang.IllegalStateException: This method must be called on the Event Dispatch Thread
    at org.pushingpixels.substance.api.SubstanceLookAndFeel.setSkin(SubstanceLookAndFeel.java:1970)
    at org.pushingpixels.substance.api.SubstanceLookAndFeel.setSkin(SubstanceLookAndFeel.java:2086)
    at loader.Loader.main(Loader.java:126)

请告诉我要更改或添加的内容。

谢谢你。

4

2 回答 2

2

Swing GUI 应该在 Event Dispatch Thread 上构建和更改。有关更多详细信息,请参阅Swing 中的并发,尤其是初始线程

下面是在 EDT 上创建 GUI 的示例:

public static void main(String[] args) {
    Runnable r = new Runnable() {

        @Override
        public void run() {
            ImageTimeTunnel itt = new ImageTimeTunnel();

            JFrame f = new JFrame("Image Time Tunnel");
            f.add(itt.getGUI());
            // Ensures JVM closes after frame(s) closed and
            // all non-daemon threads are finished
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

            // ensures the frame is the minimum size it needs to be
            // in order display the components within it
            f.pack();

            // See http://stackoverflow.com/a/7143398/418556 for demo.
            // f.setLocationByPlatform(true);
            f.setLocationRelativeTo(null);

            // should be done last, to avoid flickering, moving,
            // resizing artifacts.
            f.setVisible(true);
        }
    };
    // Swing GUIs should be created and updated on the EDT
    // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
    SwingUtilities.invokeLater(r);
}
于 2014-02-01T10:58:35.697 回答
0

我使用 Eclipse,当我制作 JFrame 时,它​​会为事件调度线程生成一些代码。

尝试在 main 方法中使用它:

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {

                GraphiteGlassSkin darkSkin = new GraphiteGlassSkin();
                SubstanceLookAndFeel.setSkin(darkSkin);
                UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceGraphiteGlassLookAndFeel");

                Frame frame = new Frame();
                frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    });

我相信这与 Andrew 的代码相同,只是在同一行中创建和调用 runnable。

我正在使用物质 7.1.01。

于 2017-11-17T17:41:22.270 回答