gpt4 book ai didi

java - 当我的应用程序打开一个新的 java 程序时,如何防止我的应用程序使用新主题重新绘制自身?

转载 作者:行者123 更新时间:2023-12-01 11:21:23 30 4
gpt4 key购买 nike

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

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

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;
}
}

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

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;
}
}
}

最佳答案

这个基于 Swing Launcher使用ProcessBuilder在单独的 JVM 中运行程序。您可以通过使用非默认 Look & Feel 运行它来评估其对您的应用程序的适用性。 。请注意下图中左侧的 MetalLookAndFeel 和右侧的 AquaLookAndFeel

image

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

关于java - 当我的应用程序打开一个新的 java 程序时,如何防止我的应用程序使用新主题重新绘制自身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31186408/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com