JMenuItems隐藏在我的JApplet之后

时间:2013-01-25 05:11:19

标签: java swing components japplet jmenu

我正在为我的服务器创建一个客户端,JMenu工作正常,直到我添加了applet,现在当我点击菜单查看下拉菜单时,它没有显示。我假设它隐藏在applet后面,但我无法解决它

public class Loader extends JApplet implements Serializable, ActionListener {

private static final long serialVersionUID = 7639088664641445302L;

private static final HashMap<String, String> THEME_LIST = new HashMap<String, String>();

private final String[] THEME_NAMES = { "Magma", "Emerald", "Raven", "Black Steel", "Challenger", "Sahara", "Silver", "Blue" };

public Properties params = new Properties();
public JFrame mainframe;
public JPanel mainpanel = new JPanel();

public static boolean useIsaac = false;

private static Robot robot;

private static final String HOME = System.getProperty("user.home") + "\\";

public static String THEME = "org.jvnet.substance.skin.SubstanceBusinessLookAndFeel";
public static String IP = "77.248.12.184";
public static boolean LOBBY_ENABLED;
public static boolean DISABLE_XTEA_CRASH = true;
public static boolean DISABLE_USELESS_PACKETS = true;
public static boolean DISABLE_RSA = false;

public static final int REVISION = 718;
public static int SUB_REVISION = 3;
public static Loader instance;

private JMenuItem forums, vote, donate;
private JCheckBoxMenuItem lastChecked;
private JMenu links, options, themes;
private JCheckBox lobbyCheckBox;
private JMenuBar menuBar;

public static void main(String[] args) throws Exception {
    UIManager.setLookAndFeel(THEME);
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    JPopupMenu.setDefaultLightWeightPopupEnabled(true);
    // final GUI gui = instance = (GUI) loadSerializedFile();
    robot = new Robot();
    final Loader gui = new Loader();
    LOBBY_ENABLED = gui.lobbyCheckBox.isSelected();
    gui.mainframe.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            storeSerializableClass(gui);
        }
    });
    gui.mainframe.setVisible(true);
    gui.startClient();
}

public void startClient() {
    try {
        client clnt = new client();
        clnt.supplyApplet(this);
        clnt.init();
        clnt.start();
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}

public Loader() {
    params.put("separate_jvm", "true");
    params.put("boxbgcolor", "black");
    params.put("image", "http://www.runescape.com/img/game/splash2.gif");
    params.put("centerimage", "true");
    params.put("boxborder", "false");
    params.put("java_arguments", "-Xmx256m -Xss2m -Dsun.java2d.noddraw=true -XX:CompileThreshold=1500 -Xincgc -XX:+UseConcMarkSweepGC -XX:+UseParNewGC");
    params.put("27", "0");
    params.put("1", "0");
    params.put("16", "false");
    params.put("17", "false");
    params.put("21", "1"); // WORLD ID
    params.put("30", "false");
    params.put("20", IP);
    params.put("29", "");
    params.put("11", "true");
    params.put("25", "1378752098");
    params.put("28", "0");
    params.put("8", ".runescape.com");
    params.put("23", "false");
    params.put("32", "0");
    params.put("15", "wwGlrZHF5gKN6D3mDdihco3oPeYN2KFybL9hUUFqOvk");
    params.put("0", "IjGJjn4L3q5lRpOR9ClzZQ");
    params.put("2", "");
    params.put("4", "1"); // WORLD ID
    params.put("14", "");
    params.put("5", "8194");
    params.put("-1", "QlwePyRU5GcnAn1lr035ag");
    params.put("6", "0");
    params.put("24", "true,false,0,43,200,18,0,21,354,-15,Verdana,11,0xF4ECE9,candy_bar_middle.gif,candy_bar_back.gif,candy_bar_outline_left.gif,candy_bar_outline_right.gif,candy_bar_outline_top.gif,candy_bar_outline_bottom.gif,loadbar_body_left.gif,loadbar_body_right.gif,loadbar_body_fill.gif,6");
    params.put("3", "hAJWGrsaETglRjuwxMwnlA/d5W6EgYWx");
    params.put("12", "false");
    params.put("13", "0");
    params.put("26", "0");
    params.put("9", "77");
    params.put("22", "false");
    params.put("18", "false");
    params.put("33", "");
    params.put("haveie6", "false");

    mainframe = new JFrame("Matrix RSPS");
    mainframe.getContentPane().setLayout(new BorderLayout());
    mainpanel.setLayout(new BorderLayout());
    mainpanel.setPreferredSize(new Dimension(765, 503));
    mainframe.getContentPane().add(mainpanel, "Center");
    mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainpanel.add(this);

    menuBar = new JMenuBar();
    mainframe.setJMenuBar(menuBar);

    lobbyCheckBox = new JCheckBox("Enable Lobby");
    lobbyCheckBox.addActionListener(this);
    lobbyCheckBox.setHorizontalTextPosition(SwingConstants.LEFT);
    menuBar.add(lobbyCheckBox);

    links = new JMenu("Links");
    menuBar.add(links);

    forums = new JMenuItem("Forums");
    forums.addActionListener(this);
    links.add(forums);

    vote = new JMenuItem("Vote");
    vote.addActionListener(this);
    links.add(vote);

    donate = new JMenuItem("Donate");
    donate.addActionListener(this);
    links.add(donate);

    options = new JMenu("Options");
    menuBar.add(options);

    themes = new JMenu("Themes");
    options.add(themes);

    JCheckBoxMenuItem temp;
    for (int i = 0; i < THEME_NAMES.length; i++) {
        String theme = THEME_NAMES[i];
        temp = new JCheckBoxMenuItem(theme);
        temp.addActionListener(this);

        if (THEME.equalsIgnoreCase(THEME_LIST.get(theme))) {
            lastChecked = temp;
            temp.setSelected(true);
        }
        themes.add(temp);
    }
    mainframe.pack();
    mainframe.setVisible(true);
}

public String getParameter(String string) {
    return (String) params.get(string);
}

public URL getDocumentBase() {
    return getCodeBase();
}

public URL getCodeBase() {
    try {
        return new URL("http://" + IP);
    } catch (Exception exception) {
        exception.printStackTrace();
        return null;
    }
}

public static final Object loadSerializedFile() {
    try {
        File saveDirect = new File(HOME + "Saradomin");
        File saveFile = new File(HOME + "Saradomin\\Client Save.sv");
        if (!saveDirect.exists() || !saveFile.exists())
            return new Loader();
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(saveFile));
        Object object = in.readObject();
        in.close();
        return object;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new Loader();
}

public static final void storeSerializableClass(Serializable o) {
    try {
        File saveDirect = new File(HOME + "Saradomin");
        File saveFile = new File(HOME + "Saradomin\\Client Save.sv");
        if (!saveDirect.exists())
            saveDirect.mkdir();
        if (!saveFile.exists())
            saveFile.createNewFile();
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(saveFile));
        out.writeObject(o);
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

static {
    THEME_LIST.put("Magma", "org.jvnet.substance.skin.SubstanceMagmaLookAndFeel");
    THEME_LIST.put("Emerald", "org.jvnet.substance.skin.SubstanceEmeraldDuskLookAndFeel");
    THEME_LIST.put("Black Steel", "org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel");
    THEME_LIST.put("Raven", "org.jvnet.substance.skin.SubstanceRavenGraphiteGlassLookAndFeel");
    THEME_LIST.put("Challenger", "org.jvnet.substance.skin.SubstanceChallengerDeepLookAndFeel");
    THEME_LIST.put("Sahara", "org.jvnet.substance.skin.SubstanceSaharaLookAndFeel");
    THEME_LIST.put("Silver", "org.jvnet.substance.skin.SubstanceBusinessLookAndFeel");
    THEME_LIST.put("Blue", "org.jvnet.substance.skin.SubstanceBusinessBlueSteelLookAndFeel");
}

private void openURL(String url) throws Exception {
    Desktop.getDesktop().browse(new URI(url));
}

public String getFormattedDate() {
    DateFormat dateFormat = new SimpleDateFormat("hh.mm.ss a");
    Date date = new Date();
    String formattedDate = dateFormat.format(date);
    return formattedDate;
}

private Rectangle getGameScreen() {
    Rectangle rect = this.getBounds();
    rect.setLocation(this.getLocationOnScreen());
    return rect;
}

private void confirmImage(BufferedImage createScreenCapture) {
    new ConfirmImage(createScreenCapture);
}

@Override
public void actionPerformed(ActionEvent e) {
    try {
        String command = e.getActionCommand();
        System.out.println(command);
        String selectedTheme = THEME_LIST.get(e.getActionCommand());
        if (selectedTheme != null) {
            if (lastChecked != null)
                lastChecked.setSelected(false);
            lastChecked = (JCheckBoxMenuItem) e.getSource();
            UIManager.setLookAndFeel(selectedTheme);
        } else if (command.equalsIgnoreCase("enable lobby")) {
            confirmImage(robot.createScreenCapture(getGameScreen()));//temp until screenshot is 100%
            //LOBBY_ENABLED = lobbyCheckBox.isSelected();
        } else if (command.equalsIgnoreCase("forums")) {
            openURL("http://www.saradom.in/");
        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}

private class ConfirmImage implements ActionListener {

    private JFrame frame;
    private JButton btnSave, btnDiscard, btnUpload;
    private BufferedImage image;

    public ConfirmImage(BufferedImage image) {
        this.image = image;
        openFrame();
    }

    public void openFrame() {
        frame = new JFrame("Decide the fate of you screenshot.");
        Insets insets = frame.getInsets();
        frame.setSize(image.getWidth()+insets.left + insets.right, image.getHeight()+insets.top + insets.bottom);
        frame.setResizable(false);
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                if (JOptionPane.showConfirmDialog(frame, "Are you sure you want to discard this image(you will not be able to get it back)?", "Confirm exit.", JOptionPane.OK_OPTION, 0, new ImageIcon("")) != 0) {
                    return;
                }
                frame.dispose();
            }

        });
        frame.setDefaultCloseOperation(0);

        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        frame.setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel picLabel = new JLabel(new ImageIcon(image));
        picLabel.setBounds(0, 0, image.getWidth(), image.getHeight());
        contentPane.add(picLabel);

        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        btnSave = new JButton("Save Image");
        menuBar.add(btnSave);
        btnSave.addActionListener(this);

        btnDiscard = new JButton("Discard Image");
        menuBar.add(btnDiscard);
        btnDiscard.addActionListener(this);

        btnUpload = new JButton("Upload Image");
        menuBar.add(btnUpload);
        btnUpload.addActionListener(this);

        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBounds(0, 0, 484, 362);
        contentPane.add(panel);

        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            String action = e.getActionCommand();
            if (action.equalsIgnoreCase("save image")) {
                ImageIO.write(image, "PNG", new File(HOME + "Saradomin" + getFormattedDate() + ".png"));
                JOptionPane.showConfirmDialog(frame, "Screenshot saved to: " + HOME + "Saradomin", "Screenshot save completed.", JOptionPane.PLAIN_MESSAGE);
                frame.dispose();
            } else if (action.equalsIgnoreCase("discard image")) {
                if (JOptionPane.showConfirmDialog(frame, "Are you sure you want to discard this image(you will not be able to get it back)?", "Confirm exit.", JOptionPane.OK_OPTION, 0, new ImageIcon("")) != 0) {
                    return;
                }
                frame.dispose();
            } else if (action.equalsIgnoreCase("upload image")) {
                new ImageLink(Utils.upload(image));
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}
}

编辑:我知道为什么这会导致你们不必继续在下面发布原因,我正在寻求一种方法。

1 个答案:

答案 0 :(得分:5)

我对frame.setContentPane(contentPane)保持警惕,contentPane有效用{默认} FlowLayout取代 frame.add(contentPane)。而是根据需要使用contentPane转发“到{{1}}。”另一个名称也可能有所帮助。另请参阅Using Top-Level Containers和此answer