简体   繁体   English

Java在可运行的Jar文件中的内容之间来回移动

[英]Java moving back and forth between content in runnable Jar file

I'm quite new to programming so I don't know the right way to do things and have been just experimenting a bit. 我对编程很陌生,所以我不知道正确的做事方式,只是在做一些尝试。 I want to create a runnable where I can move back and forth between different content. 我想创建一个可运行的对象,以便在不同内容之间来回移动。 The following works when run from inside eclipse, but if I export it as a JAR file, once I've moved forward once and then back again, moving forward won't give me the content anymore, but just the back button. 从eclipse内部运行时,以下工作有效,但是如果我将其导出为JAR文件,则一次向前前进然后再次向后前进时,向前前进将不再为我提供内容,而只是返回按钮。 I tried something like this: 我尝试过这样的事情:

public class TestMain extends JFrame {
static PanelClass panel;
static boolean inUse = false;

public static void main(String[] args) {
    panel = new PanelClass();

    final TestMain test = new TestMain();
    final Container container = test.getContentPane();
    container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
    test.setSize(500, 500);

    final JButton back = new JButton("Back");
    back.setAlignmentX(Component.CENTER_ALIGNMENT);
    back.setMaximumSize(new Dimension(200, 80));
    back.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            test.getContentPane().removeAll();
            test.setContentPane(container);
            test.getContentPane().revalidate();

        }
    });

    final JButton exit = new JButton("Exit");
    exit.setAlignmentX(Component.CENTER_ALIGNMENT);
    exit.setMaximumSize(new Dimension(200, 80));
    exit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);

        }
    });

    JButton problem = new JButton("Problem");
    problem.setMaximumSize(new Dimension(200, 80));
    problem.setAlignmentX(Component.CENTER_ALIGNMENT);
    problem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            inUse = true;
            test.setContentPane(panel);
            test.getContentPane().add(back);
            test.getContentPane().revalidate();

        }
    });

    container.add(problem);
    container.add(exit);

    test.setVisible(true);
    test.setDefaultCloseOperation(EXIT_ON_CLOSE);
    test.setLocationRelativeTo(null);

    while (true) {
        while (!panel.stop && !inUse) {
        }
        inUse = false;
        panel = new PanelClass();
        test.setContentPane(panel);
        test.getContentPane().add(back);
        test.getContentPane().revalidate();
    }

}

}

And the class for what I want to have as the second content: 而我想拥有的课程作为第二个内容:

public class PanelClass extends JPanel {
JTextArea text = new JTextArea("Some text here!" + '\n' + '\n');
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");

boolean stop = false;

public PanelClass() {
    text.setEditable(false);
    text.setMaximumSize(new Dimension(300, 300));
    this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    Dimension d = new Dimension(200, 60);

    button1.setAlignmentX(Component.CENTER_ALIGNMENT);
    button1.setMaximumSize(d);
    button2.setAlignmentX(Component.CENTER_ALIGNMENT);
    button2.setMaximumSize(d);
    this.add(text);
    this.add(button1);
    this.add(button2);

}

} }

What is the actual working way to do this? 实际的工作方式是什么? What if I have a lot of windows I'd like to be able to move back and forth between? 如果我想在很多窗户之间来回移动怎么办? I know it's a lot of bad/possibly-hard-to-read code, but I hope someone could help me out. 我知道这是很多不好的/可能很难阅读的代码,但我希望有人能帮助我。

This is the code that runs when you press "Problem": 这是您按“问题”时运行的代码:

        test.setContentPane(panel);
        test.getContentPane().add(back);
        test.getContentPane().revalidate();

and this is the code that runs when you press "Back": 这是按下“返回”时运行的代码:

        test.getContentPane().removeAll();
        test.setContentPane(container);
        test.getContentPane().revalidate();

What is the sequence of calls when you press "Problem" then "Back" then "Problem"? 当您依次按“问题”,“上一步”,“问题”时,呼叫顺序是什么? It's this. 就是这个 (The revalidate() calls won't mess anything up, so I won't show them) (revalidate()调用不会弄乱任何内容,因此我不会显示它们)

        // Problem
        test.setContentPane(panel);
        test.getContentPane().add(back);
        // Back
        test.getContentPane().removeAll();
        test.setContentPane(container);
        // Problem
        test.setContentPane(panel);
        test.getContentPane().add(back);

Notice that you set the panel as the content pane, and then remove all the components from it when "Back" is pressed. 请注意,将面板设置为内容窗格,然后在按下“返回”时从其中删除所有组件。 The next time you press "Problem", the panel has no components on it, because you removed them. 下次按“问题”时,面板上没有组件,因为您已将其删除。

从eclipse导出为JAR时,您是否尝试导出为可运行的jar并选择将所需的程序包打包到生成的jar中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM