我有以下申请。它有效,我只是想更好地理解它 所以从主要课程我做了以下调用
public static void main(String[] args)
{
Gui gui = new Gui();
gui.startGui();
}
在GUI类
中public Gui()
{
initialize();
}
private void initialize()
{
mainWinFrm = new JFrame();
mainWinFrm.setTitle("Inventory Tool");
JMenuBar menuBar = new JMenuBar();
mainWinFrm.getContentPane().add(menuBar, BorderLayout.NORTH);
.....//allot more GUI staff getting initialize
}
public void startGui()
{
try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {}
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
Gui window = new Gui();
window.mainWinFrm.setLocationRelativeTo(null);
window.mainWinFrm.setMinimumSize(new Dimension(400, 200));
window.mainWinFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.mainWinFrm.pack();
window.mainWinFrm.setVisible(true);
}
catch (Exception e)
{e.printStackTrace();}
}
});
}
上面代码的结构是由GUI构建器构建的,我想我想了解发生了什么。
我不明白的主要观点是当我们在主类中构造对象 gui 时,它初始化所有变量,然后启动线程 gui.startGui()该方法还会创建一个新的GUI对象窗口并初始化所有变量。
这对我来说似乎不对......但是我不确定我是否遗漏了某些东西。
感谢您的建议/帮助 亚历
答案 0 :(得分:3)
首先看一下Initial Threads
Gui
课程有点尴尬......但距离可行不远......
在initialize
中创建UI的基础知识,在startUI
中切换到事件调度线程,创建Gui
的新实例并显示主要内容帧...
奇怪的部分是在Gui
中创建startUI
的第二个实例。相反,你可以做一些像......
public Gui()
{
// It's arguable, but I'm paranoid, so I prefer to do anything related
// to the UI within the EDT...
}
private void initialize()
{
mainWinFrm = new JFrame();
mainWinFrm.setTitle("Inventory Tool");
JMenuBar menuBar = new JMenuBar();
mainWinFrm.getContentPane().add(menuBar, BorderLayout.NORTH);
.....//allot more GUI staff getting initialize
}
public void startGui()
{
try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {}
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
// Initialize here instead...
initialize();
window.mainWinFrm.setLocationRelativeTo(null);
window.mainWinFrm.setMinimumSize(new Dimension(400, 200));
window.mainWinFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.mainWinFrm.pack();
window.mainWinFrm.setVisible(true);
}
catch (Exception e)
{e.printStackTrace();}
}
});
}
这仍然不合适,因为现在可以在他们被初始化之前访问课程的某些部分,相反,我更愿意做更像......
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
// Initialise the system look and feel...
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex)
{
}
Gui window = new Gui();
window.mainWinFrm.setLocationRelativeTo(null);
window.mainWinFrm.setMinimumSize(new Dimension(400, 200));
window.mainWinFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.mainWinFrm.pack();
window.mainWinFrm.setVisible(true);
}
});
}
public Gui()
{
initialize();
}
private void initialize()
{
mainWinFrm = new JFrame();
mainWinFrm.setTitle("Inventory Tool");
JMenuBar menuBar = new JMenuBar();
mainWinFrm.getContentPane().add(menuBar, BorderLayout.NORTH);
.....//allot more GUI staff getting initialize
}
答案 1 :(得分:1)
更好的方法: 桂班:
public class Gui {
private JFrame mainWinFrm;
public Gui() {
initialize();
}
private void initialize() {
mainWinFrm = new JFrame();
mainWinFrm.setTitle("Inventory Tool");
JMenuBar menuBar = new JMenuBar();
mainWinFrm.getContentPane().add(menuBar, BorderLayout.NORTH);
}
public static void startGui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
try {
Gui window = new Gui();
window.mainWinFrm.setLocationRelativeTo(null);
window.mainWinFrm.setMinimumSize(new Dimension(400, 200));
window.mainWinFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.mainWinFrm.pack();
window.mainWinFrm.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
和MainClass:
public class MainClass {
public static void main(String[] args)
{
Gui.startGui();
}
}
这是更好的方式,因为它没有创建任何额外的对象。