简体   繁体   中英

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. 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)

        // 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中?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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