Java 傳統型應用程式開發學習筆記二

來源:互聯網
上載者:User

學習Java傳統型應用程式開發_主表單

一般的應用程式都是登陸表單,登陸成功後進入主介面的,先看看主介面的草圖:

可以看出,主介面大致可以分為四塊,上面是功能表列及圖片工具列,左邊是樹形導航,下邊的狀態列。

功能表列:JMenuBar(菜單條),JMenu(菜單),JMenuItem (功能表項目)

工具條:toolbar,JButton(按鈕+圖片)

樹形導航:JTree

狀態列:toolbar,JLable

 

實現介面的代碼:

功能表列:

 

代碼

 1 final JMenuBar menuBar = new JMenuBar();
2 menuBar.setName("menuBar");
3 setJMenuBar(menuBar);
4
5 final JMenu jmenuFile = new JMenu();
6 jmenuFile.setText("檔案(F)");
7 jmenuFile.setName("menuFile");
8 menuBar.add(jmenuFile);
9
10 final JMenuItem newProject = new JMenuItem();
11 newProject.setName("newProject");
12 newProject.setText("建立工程");
13 jmenuFile.add(newProject);
14
15 final JMenu jmenuTool = new JMenu();
16 jmenuTool.setText("工具(T)");
17 jmenuTool.setName("jmenuTool");
18 menuBar.add(jmenuTool);

 

 

工具列:

 

代碼

final JToolBar toolBar = new JToolBar();
toolBar.setName("toolBar");
getContentPane().add(toolBar, BorderLayout.NORTH);

final JButton jbtnNew = new JButton();
jbtnNew.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
//點擊建立按鈕觸發事件
}
});
jbtnNew.setName("jbtnNew");
jbtnNew.setIcon(SwingResourceManager.getIcon(Main.class,
"Image/FileNew.png"));
jbtnNew.setText("建立");
toolBar.add(jbtnNew);

toolBar.addSeparator();

final JButton jbtnSave = new JButton();
jbtnSave.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent arg0) {
}
});
jbtnSave.setName("jbtnSave");
jbtnSave.setIcon(SwingResourceManager.getIcon(Main.class, "Image/FileSave.png"));
jbtnSave.setText("儲存");
toolBar.add(jbtnSave);

 

 

樹形菜單:

 

//根節點
DefaultMutableTreeNode root = new DefaultMutableTreeNode("任務資訊");
//建立樹
JTree tree = new JTree(root);

 

 

狀態列:

 

代碼

 1 final JToolBar stautsBar = new JToolBar();
2 stautsBar.setName("statusBar");
3 getContentPane().add(stautsBar, BorderLayout.SOUTH);
4
5 lblWelcome = new JLabel();
6 lblWelcome.setText("歡迎");
7 stautsBar.add(lblWelcome);
8
9 stautsBar.addSeparator();
10
11 lblSupport = new JLabel();
12 lblSupport.setText("支援人員:");
13 stautsBar.add(lblSupport);

 

 

利用上述代碼 就可以搭出主介面的架構來,可能還需要朋友們自己去擴充。。

 

注意:

1、要注意介面的布局,可以添加jpanel容器參與布局。

 

2、現在看到的草圖控制項是比較少的,如果控制項多了,處理的事件多了,那麼就為了防止主介面代碼過多,就需要我們學會使用分拆分,再組合的方式。

例如:樹形控制項或處理事件的代碼 成一個類(test1)

         面板1控制項或處理事件的代碼一個類(test2)

         。。。。。。

 

         主介面中時 組合在一起 效果是一樣的 但是主介面的代碼將大大減少。。

 

3、在建立一個jframe後,一定要為介面編寫釋放資源的代碼,如果你不編寫dispose(); 運行一次 就會在進程中添加一個javaw進程(可以自己測試)

    public void windowClosing(final WindowEvent e) {
                dispose();
    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.