2

我有以下应用程序。它有效,我只是想更好地理解它。
所以从主课我打了以下电话

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 对象窗口并初始化所有变量。

这对我来说似乎不对......但是我不确定我是否遗漏了什么。

感谢您的建议/帮助亚历克西斯

4

2 回答 2

3

首先看一下初始线程

Gui上课有点尴尬……但离实用不远了……

initialize它正在创建 UI 的基础知识,它startUI正在切换到事件调度线程,创建一个新实例Gui并显示主框架......

奇怪的部分是GuistartUI. 相反,您可以执行类似...

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
}
于 2013-08-22T12:49:59.033 回答
1

更好的方法: Gui 类:

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();
}

}

这是更好的方法,因为它不会创建任何额外的对象。

于 2013-08-22T11:43:51.697 回答