当我的应用程序打开一个新的 Java 程序时,如何防止我的应用程序使用新主题重新绘制自己? [英] When my app opens a new java program, how do I prevent my app from repainting itself with a new theme?

查看:24
本文介绍了当我的应用程序打开一个新的 Java 程序时,如何防止我的应用程序使用新主题重新绘制自己?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在做的是创建一个用作启动器的 Swing 应用程序.它所做的只是为用户提供 3 个选项,他们可以从中选择打开一个新的 Java 应用程序.3 个不同的java 应用程序都有不同的主题,一个根本没有主题.我正在尝试获取它,因此当我选择一个选项时,我的启动器应用程序不会将其重新绘制为新程序的内容.我希望启动器保持其主题.

Basically what I'm doing is creating a swing application that acts as a launcher. All it does is gives the user 3 options they can choose from to open a new java application. The 3 different java applications all have different themes, and one doesn't have a theme at all. I'm trying to get it so when I select an option, my launcher app doesn't repaint it self to what the new program is. I want the launcher to maintain its theme.

我可能没有正确使用 EventQueue,但我不确定该使用哪一个.

I'm probably not using EventQueue right but I'm not sure which one to use.

package wind;

import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.text.ParseException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import de.javasoft.plaf.synthetica.SyntheticaAluOxideLookAndFeel;

public class Launcher {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private JButton btn1, btn2, btn3;
private GridBagConstraints gbc = new GridBagConstraints();
private JMenuBar menuBar;
private JMenu menu, menu2, menu3, menu4;
private JMenuItem menuItem, menuItem2, menuItem3, menuItem4,
menuItem5, menuItem6, menuItem7, menuItem8, menuItem9, menuItem10,
menuItem11, menuItem12, menuItem13, submenu1, submenu2, submenu3, submenu4,         submenu5,
submenu6, submenu7, submenu8, submenu9, submenu10;

JFrame frame;

public Launcher() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);
    try {
        UIManager.setLookAndFeel(new SyntheticaAluOxideLookAndFeel());
    } catch (UnsupportedLookAndFeelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    EventQueue.invokeLater(new Runnable() {

        public void run() {
            frame = new JFrame();
            frame.setTitle("Project Wind Client Launcher"); 
            Image icon = getImage("windicon.png");
            if (icon != null)
                frame.setIconImage(icon);
            components();
            frame.add(mainPanel());
            frame.setJMenuBar(menuBar);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(500, 300);
            frame.setResizable(false);
            frame.setVisible(true);
        }           
    });
}

private JPanel mainPanel() {
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new GridBagLayout());

    btn1 = new JButton();
      try {
            Image img = ImageIO.read(getClass().getResource("/resources/gui.png"));
            btn1.setIcon(new ImageIcon(img));
        } catch (IOException ex) {
      }
    btn1.setToolTipText("Click here to launch the client with a graphic user interface.");
    btn1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            runGUIMode(e);
        }           
    });
    //btn1.setBorder(null);
    btn1.setOpaque(true);
    btn1.setContentAreaFilled(false);
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.weightx = 0.25;
    mainPanel.add(btn1, gbc);

    btn2 = new JButton();
      try {
            Image img2 = ImageIO.read(getClass().getResource("/resources/nogui.png"));
            btn2.setIcon(new ImageIcon(img2));
        } catch (IOException ex) {
      }
    btn2.setToolTipText("This will launch the client without a graphic user interface.");
    btn2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            runNoGUI(e);
        }           
    });
    btn2.setContentAreaFilled(false);
    gbc.gridx = 2;
    gbc.gridy = 0;
    gbc.weightx = 0.25;
    mainPanel.add(btn2, gbc);

    btn3 = new JButton();
      try {
            Image img2 = ImageIO.read(getClass().getResource("/resources/app.png"));
            btn3.setIcon(new ImageIcon(img2));
        } catch (IOException ex) {
      }
    btn3.setToolTipText("This will launch the client in application mode.");
    btn3.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            runApplicationMode(e);
        }           
    });
    btn3.setContentAreaFilled(false);
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.weightx = 0.25;
    mainPanel.add(btn3, gbc);       

    return mainPanel;
}

private void runApplicationMode(ActionEvent e) {    
    new FrameListener(FrameListener.LaunchMode.APPLICATION);
}

private void runNoGUI(ActionEvent e) {
    new FrameListener(FrameListener.LaunchMode.NOGUI);
}

private void runGUIMode(ActionEvent e) {
    new FrameListener(FrameListener.LaunchMode.GUI);
}

public void components() {
    menuBar = new JMenuBar();

    menu = new JMenu("File");
    menu2 = new JMenu("Links");
    menu3 = new JMenu("Guides");
    menu4 = new JMenu("Help");

    menuBar.add(menu);
    menuBar.add(menu2);
    menuBar.add(menu3);
    menuBar.add(menu4);

    menuItem = new JMenuItem("Exit");
    menuItem.setToolTipText("Click here to exit the client launcher.");
    menuItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            exitClient(e);
        }           
    });

    menuItem2 = new JMenuItem("Update");
    menuItem2.setToolTipText("Click here to update your client.");
    menuItem2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            updateClient(e);                
        }           
    });

    menuItem3 = new JMenuItem("Check Version");
    menuItem3.setToolTipText("Click here to check the version of your client.");
    menuItem3.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            checkVersion(e);
        }           
    });

    menuItem13 = new JMenuItem("Hide Launcher");
    menuItem13.setToolTipText("Click here to hide the client launcher.");
    menuItem13.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            hideLauncher(e);
        }       
    });
    menu.add(menuItem3);
    menu.add(menuItem);
    menu.add(menuItem13);
    menu.add(menuItem2);


    menuItem4 = new JMenuItem("Home");
    menuItem4.setToolTipText("Click here to open up your homepage.");
    menuItem4.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            home(e);
        }       
    });
    menuItem5 = new JMenuItem("YouTube");
    menuItem5.setToolTipText("Click here to open up YouTube.");
    menuItem5.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == menuItem5)
                openURL("http://youtube.com");  
        }           
    });
    menuItem6 = new JMenuItem("Twitter");
    menuItem6.setToolTipText("Click here to open up Twitter.");
    menuItem6.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == menuItem6)
                openURL("http://twitter.com");
        }           
    });

    menuItem10 = new JMenuItem("Twitch");
    menuItem10.setToolTipText("Click here to open up Twitch.");
    menuItem10.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == menuItem10)
                openURL("http://twitch.tv");
        }           
    });
    menu2.add(menuItem4);       
    menu2.add(menuItem10);
    menu2.add(menuItem6);
    menu2.add(menuItem5);


    menuItem7 = new JMenu("Combat");
    menuItem7.setToolTipText("Click here to view more options related to combat.");

    submenu1 = new JMenu("Attack");
    submenu2 = new JMenu("Strength");
    submenu3 = new JMenu("Defence");
    submenu4 = new JMenu("Hitpoints");
    submenu5 = new JMenu("Prayer");
    submenu6 = new JMenu("Ranged");
    submenu7 = new JMenu("Magic");
    menuItem7.add(submenu1);
    menuItem7.add(submenu3);
    menuItem7.add(submenu4);
    menuItem7.add(submenu5);
    menuItem7.add(submenu7);
    menuItem7.add(submenu6);
    menuItem7.add(submenu2);

    menuItem8 = new JMenu("Skilling");
    menuItem8.setToolTipText("Click here to view more options about skilling.");

    submenu8 = new JMenu("Cooking");
    submenu9 = new JMenu("Fishing");
    submenu10 = new JMenu("Fletching");

    menuItem8.add(submenu8);
    menuItem8.add(submenu9);
    menuItem8.add(submenu10);

    menuItem9 = new JMenu("Money Making");
    menuItem9.setToolTipText("Click here to view more options related to money making.");

    menu3.add(menuItem7);
    menu3.add(menuItem8);
    menu3.add(menuItem9);

    menuItem11 = new JMenu("Report a Bug");
    menuItem11.setToolTipText("See any bugs? Click here and report them.");

    menuItem12 = new JMenu("Commands");
    menuItem12.setToolTipText("Click here to see which commands are available.");

    menu4.add(menuItem11);
    menu4.add(menuItem12);
}

private void exitClient(ActionEvent e) {
    System.exit(1);             
}

private void updateClient(ActionEvent e) {
    JOptionPane.showMessageDialog(null,
            "Client will now update.");     
}

private void checkVersion(ActionEvent e) {
    JOptionPane.showMessageDialog(null,
            "Your files are fully updated.");       
}

private void hideLauncher(ActionEvent e) {
    frame.setState(Frame.ICONIFIED);        
}

private void home(ActionEvent e) {
    openURL("http://google.com");       
}   

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new Launcher();             
        }           
    });
}


public void openURL(String url) {
    try {
          Desktop desktop = java.awt.Desktop.getDesktop();
          URI oURL = new URI(url);
          desktop.browse(oURL);
        } catch (Exception e) {
          e.printStackTrace();
    }
}   

private Image getImage(String name) {
    String url = "https://dl.dropboxusercontent.com/u/5173165/icons/" + name;
    try {
        File f = new File(name);
        if (f.exists())
            return ImageIO.read(f.toURI().toURL());
        Image img = ImageIO.read(new URL(url));
        if (img != null) {
            ImageIO.write((RenderedImage) img, "PNG", f);
            return img;
        }
    } catch (MalformedURLException e) {
        System.out.println("Error connecting to image URL: " + url);
    } catch (IOException e) {
        System.out.println("Error reading file: " + name);
    }
    return null;
}
}

这是一个创建用户选择的新应用程序的类

This is a class that creates the new application the user selects

package wind;

import java.awt.EventQueue;

import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import wind.gui.Application;
import wind.gui.Gui;
import wind.web.WebClient;

public class FrameListener {

static JLabel image;

LaunchMode mode;

public FrameListener(LaunchMode mode) {
    this.mode = mode;   
    initClient();
}


public enum LaunchMode {
    APPLICATION,
    GUI,
    NOGUI;
}

public void initClient() {  
    switch(mode) {      
    case APPLICATION:
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Application();                  
            }               
        });
        break;

    case GUI:
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Gui();                  
            }               
        });
        break;

    case NOGUI:
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new WebClient(null);                    
            }               
        });
        break;

    default:
        mode = LaunchMode.GUI;
        break;      
    }
}   
}

推荐答案

This Swing based Launcher 使用 ProcessBuilder 在单独的 JVM 中运行程序.您可以通过使用非默认的 看&感觉.注意下图中左侧的 MetalLookAndFeel 和右侧的 AquaLookAndFeel.

This Swing based Launcher uses ProcessBuilder to run programs in a separate JVM. You can assess its suitability for your application by running it with a non-default Look & Feel. Note MetalLookAndFeel on the left and AquaLookAndFeel on the right in the illustration below.

$ java -Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel \
  -cp build/classes gui.Launcher

这篇关于当我的应用程序打开一个新的 Java 程序时,如何防止我的应用程序使用新主题重新绘制自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆