是否可以隐藏我的JFrame
窗口并在此类操作中从另一个类启动主方法?
这里是行动的代码:
private AbstractAction start = new AbstractAction("start") {
@Override
public void actionPerformed(ActionEvent arg0) {
}
};
答案 0 :(得分:0)
JFrame有一个setVisible方法,可以根据需要隐藏窗口。
你可以静态地调用你的另一个类中的main方法,虽然这是一种混乱的做事方式。最好让main方法和Action调用另一个执行所需任务的方法。
因为您将JFrame设置为在操作中可见而您的其他方法可能需要一些时间,您可能需要使用SwingWorker线程,否则您的界面将挂起(使用绘制框架的相同线程调用Actions)并且在action方法存在之前它不会被隐藏。见这里:http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html
答案 1 :(得分:0)
我一直在研究与你想要完成的事情几乎相似的事情。 我有一个GUI,用户输入值,然后单击开始按钮运行程序(使用这些值作为输入)。程序启动时,我可以选择:
在这两种情况下,我想在启动程序时创建一个新的java线程。
以下是对我有用的资源:
用于创建在新线程中启动程序的动作侦听器的代码:
//Handle LAUNCH action.
else if (e.getSource() == launchButton)
{
JFrame launchConfirmationFrame = new JFrame("Launch Confirmation");
launchConfirmationFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
int answer;
launchConfirmationFrame.pack();
//downloadDateframe.setVisible(true);
//if answer=0 its yes, if its =1 its no!
answer = JOptionPane.showConfirmDialog(
launchConfirmationFrame
,"^^^^ ARE YOU SURE YOU WOULD LIKE TO PROCEED? ^^^^"
,"TITLE"
,JOptionPane.YES_NO_OPTION);
if(answer==0)
{
buttonPanel.removeAll();
optionsFrame.repaint();
optionsFrame.setVisible(true); // THIS HIDES THE GUI FRAME if it is set to false.
System.out.println("Launching YOURPROGRAM...");
//THIS CREATES AND STARTS THE THREAD WITH YOUR PROGRAM TO BE LAUNCHED
//More info on threads here http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html
Thread t = new Thread(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
launchYOURPROGRAM();
}
});
t.start();
}
else if (answer==1)
{
optionsFrame.setVisible(false);
optionsFrame.setVisible(true);
System.out.println("Returning to GUI Frame...");
}
}'
请记住将你的Jframe(这里我称之为optionsFrame)设置为静态,这样当你进入YOURPROGRAM时,你可以决定是再次显示它还是隐藏它。
从您的程序中,您可以致电:
private AbstractAction start = new AbstractAction("start") {
@Override
public void actionPerformed(ActionEvent arg0)
{
optionsFrame.setVisible(false);
}
};