j2se介面布局

來源:互聯網
上載者:User

最近一段時間在看j2se介面布局和事件的控制,如介面上處理List右鍵事件

 

MouseListener  -- 滑鼠監聽

ActionListener   -- 所有監聽器的父類

ItemListener     -- 用於捕獲帶有item的組件產生的事件

 

程式碼範例:

 

import java.awt.*;<br />import java.awt.event.*;<br />import java.applet.Applet;<br />public class ListDemo extends Applet implements MouseListener, ActionListener,<br />ItemListener {<br />private TextArea output;<br />private List mainList, fileList;<br />private String newline;<br />private PopupMenu popMenu = new PopupMenu();<br />public void init() {<br />newline = System.getProperty("line.separator");<br />//<br />mainList = new List(); //<br />mainList.add("系統資訊");<br />mainList.add("交易成員");<br />mainList.add("處理失敗訊息");<br />mainList.add("下載檔案清單");<br />mainList.addActionListener(this);<br />mainList.addItemListener(this);<br />mainList.addMouseListener(this);<br />String itemText[] = { "開始", "停止" };<br />for (int i = 0; i < itemText.length; i++) {<br />MenuItem item = new MenuItem(itemText[i]);<br />item.addActionListener(this);<br />popMenu.add(item);<br />}<br />//<br />fileList = new List();<br />fileList.add("http://127.0.0.1/test/test.txt");<br />fileList.addActionListener(this);<br />fileList.addItemListener(this);<br />//<br />GridBagLayout gridBag = new GridBagLayout();<br />setLayout(gridBag);<br />//<br />GridBagConstraints lc = new GridBagConstraints();<br />lc.fill = GridBagConstraints.BOTH;<br />lc.weightx = 1.0;<br />lc.weighty = 1.0;<br />lc.gridheight = 2;<br />gridBag.setConstraints(mainList, lc);<br />add(mainList);<br />gridBag.setConstraints(fileList, lc);<br />add(fileList);<br />output = new TextArea(10, 100);<br />output.setEditable(false);<br />GridBagConstraints tc = new GridBagConstraints();<br />tc.fill = GridBagConstraints.BOTH;<br />tc.gridx = 0;<br />tc.gridy = 2;<br />tc.weightx = 1.0;<br />tc.weighty = 1.0;<br />tc.gridwidth = GridBagConstraints.REMAINDER; // end row<br />gridBag.setConstraints(output, tc);<br />add(output);<br />add(popMenu);<br />}<br />public void actionPerformed(ActionEvent e) {<br />List list = (List) (e.getSource());<br />String language = (list == mainList) ? "主菜單" : "檔案清單";<br />output.append("Action event occurred on /"" + list.getSelectedItem()<br />+ "/" in " + language + "." + newline);<br />}<br />public void itemStateChanged(ItemEvent e) {<br />List list = (List) (e.getItemSelectable());<br />String language = (list == mainList) ? "主菜單" : "檔案清單";<br />int index = ((Integer) (e.getItem())).intValue();<br />if (e.getStateChange() == ItemEvent.SELECTED) {<br />output.append("selected Index = " + index + " (/""<br />+ list.getItem(index) + "/") in " + language + "."<br />+ newline);<br />} else { // the item was deselected<br />output.append("deselected Index = #" + index + " (/""<br />+ list.getItem(index) + "/") in " + language + "."<br />+ newline);<br />}<br />}<br />public void mouseClicked(MouseEvent e) {<br />// TODO Auto-generated method stub<br />}<br />public void mouseEntered(MouseEvent e) {<br />// TODO Auto-generated method stub<br />}<br />public void mouseExited(MouseEvent e) {<br />// TODO Auto-generated method stub<br />}<br />public void mousePressed(MouseEvent e) {<br />// TODO Auto-generated method stub<br />}<br />public void mouseReleased(MouseEvent e) {<br />// TODO Auto-generated method stub<br />if (e.isPopupTrigger()) {<br />System.out.println("e.getComponent()" + this + "e.getX() "<br />+ e.getX() + "e.getY() " + e.getY());<br />popMenu.show(e.getComponent(), e.getX(), e.getY());<br />}<br />}<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.