Java在可运行Jar文件中的内容之间来回移动
我对编程很陌生,所以我不知道正确的方法,只是做了一些实验。我想创建一个runnable,在这里我可以在不同的内容之间来回移动。当从eclipse内部运行时,下面的代码是有效的,但是如果我将它导出为JAR文件,一旦我向前移动了一次又一次,向前移动将不再给我内容,而只是返回按钮。 我试过这样的方法:Java在可运行Jar文件中的内容之间来回移动,java,user-interface,jar,Java,User Interface,Jar,我对编程很陌生,所以我不知道正确的方法,只是做了一些实验。我想创建一个runnable,在这里我可以在不同的内容之间来回移动。当从eclipse内部运行时,下面的代码是有效的,但是如果我将它导出为JAR文件,一旦我向前移动了一次又一次,向前移动将不再给我内容,而只是返回按钮。 我试过这样的方法: public class TestMain extends JFrame { static PanelClass panel; static boolean inUse = false; public
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();
}
}
}
第二个内容是我想要的课程:
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);
}
}
这样做的实际工作方式是什么?如果我有很多窗口,我希望能够在它们之间来回移动,那该怎么办?我知道读代码很糟糕/可能很难,但我希望有人能帮我解决这个问题。当您从eclipse导出为jar时,是否尝试导出为可运行的jar并选择将所需库打包到生成的jar中?这是按“问题”时运行的代码: 这是按“后退”时运行的代码: 按“问题”再按“返回”再按“问题”时,调用的顺序是什么?就是这个。(revalidate()调用不会搞乱任何事情,因此我不会显示它们)
请注意,您将面板设置为内容窗格,然后在按下“后退”时从中删除所有组件。下次按“问题”时,面板上没有任何组件,因为您删除了它们。我起初选择将库提取到生成的jar中,但现在尝试将库打包一个,似乎没有任何改变。好吧,我现在明白了,只需删除那一行就解决了问题。我仍然不明白的是,为什么这个问题只有在导出并在eclipse之外运行之后才出现,但我想这并不太重要。但是使用while循环是移动的最佳方式吗?所以当大量的窗格只是大量的while循环时?这绝对不是一个好方法。我没有意识到你要求的是一个好方法——我只是解释了为什么在你前进、后退、再前进之后屏幕上什么都没有。不,你说得对,这是我问题的重要部分。我只是想知道一个更好的方法,如果可能的话,但谢谢你。
test.setContentPane(panel);
test.getContentPane().add(back);
test.getContentPane().revalidate();
test.getContentPane().removeAll();
test.setContentPane(container);
test.getContentPane().revalidate();
// Problem
test.setContentPane(panel);
test.getContentPane().add(back);
// Back
test.getContentPane().removeAll();
test.setContentPane(container);
// Problem
test.setContentPane(panel);
test.getContentPane().add(back);