<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    內(nèi)蒙古java團隊

    j2se,j2ee開發(fā)組
    posts - 139, comments - 212, trackbacks - 0, articles - 65
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    java swing 組件大全(新手快進)

    Posted on 2006-10-27 20:01 帥子 閱讀(8033) 評論(1)  編輯  收藏 所屬分類: j2se技術(shù)專區(qū)
    import?javax.swing.*;

    import?java.awt.*;
    import?java.awt.event.*;
    import?javax.swing.tree.*;
    import?javax.swing.event.*;
    import?javax.swing.border.*;
    import?javax.swing.table.*;

    /**?
    ?*?Swing?組件測試程序
    ?*?測試Swing所有組件及其相應(yīng)的事件
    ?*?@author?天翼.李?2003.4.17?晚23:14
    ?*?@link?
    http://www.robochina.org
    ?*?@link?robococde@etang.com
    ?*/
    public?class?SwingTest?extends?JFrame
    {
    ????/**
    ?????*?主模塊,初始化所有子模塊,并設(shè)置主框架的相關(guān)屬性
    ?????*/
    ????public?SwingTest()
    ????{
    ????????//?初始化所有模塊
    ????????MenuTest?menuTest?=?new?MenuTest();
    ????????LeftPanel?leftPanel?=?new?LeftPanel();
    ????????RightPanel?rightPanel?=?new?RightPanel();
    ????????BottomPanel?bottomPanel?=?new?BottomPanel();
    ????????CenterPanel?centerPanel?=?new?CenterPanel();
    ????????
    ????????//?設(shè)置主框架的布局
    ????????Container?c?=?this.getContentPane();
    ????????//?c.setLayout(new?BorderLayout())
    ????????this.setJMenuBar(menuTest);
    ????????
    ????????c.add(leftPanel,BorderLayout.WEST);
    ????????c.add(rightPanel,BorderLayout.EAST);
    ????????c.add(centerPanel,BorderLayout.CENTER);
    ????????c.add(bottomPanel,BorderLayout.SOUTH);
    ????????
    ????????//?利用無名內(nèi)隱類,增加窗口事件
    ????????this.addWindowListener(new?WindowAdapter()
    ????????????{
    ????????????????public?void?WindowClosing(WindowEvent?e)
    ????????????????{???
    ????????????????????//?釋放資源,退出程序
    ????????????????????dispose();
    ????????????????????System.exit(0);
    ????????????????}
    ????????????});
    ????????????
    ????????
    ????????
    ????????setSize(700,500);
    ????????setTitle("Swing?組件大全簡體版");
    ????????//?隱藏frame的標題欄,此功暫時關(guān)閉,以方便使用window事件
    ????????//?setUndecorated(true);
    ????????setLocation(200,150);
    ????????show();????????
    ????}

    ????////////////////////////////////////////////////////////////////////////////
    ????/**
    ?????*?菜單欄處理模塊
    ?????*?JMenuBar?--+
    ?????*????????????--JMenu--+
    ?????*???????????????????????--JMenuItem??--ActionListener?
    ?????*??????????????
    ?????*/
    ????class?MenuTest?extends?JMenuBar
    ????{
    ????????private?JDialog?aboutDialog;
    ????????????
    ????????/**
    ?????????*?菜單初始化操作
    ?????????*/????
    ????????public?MenuTest()
    ????????{
    ????????????JMenu?fileMenu?=?new?JMenu("文件");
    ????????????JMenuItem?exitMenuItem?=?new?JMenuItem("退出",KeyEvent.VK_E);
    ????????????JMenuItem?aboutMenuItem?=?new?JMenuItem("關(guān)于...",KeyEvent.VK_A);????????????
    ????????????????????????????????????????????????
    ????????????fileMenu.add(exitMenuItem);
    ????????????fileMenu.add(aboutMenuItem);
    ????????????
    ????????????this.add(fileMenu);????????
    ????????????
    ????????????????????
    ????????????aboutDialog?=?new?JDialog();
    ????????????initAboutDialog();
    ????????????????????????
    ????????????//?菜單事件
    ????????????exitMenuItem.addActionListener(new?ActionListener()
    ????????????{
    ????????????????public?void?actionPerformed(ActionEvent?e)
    ????????????????{
    ????????????????????dispose();
    ????????????????????System.exit(0);
    ????????????????}
    ????????????});
    ????????????
    ????????????aboutMenuItem.addActionListener(new?ActionListener()
    ????????????{
    ????????????????public?void?actionPerformed(ActionEvent?e)
    ????????????????{
    ????????????????????//?"關(guān)于"對話框的處理
    ????????????????????aboutDialog.show();
    ????????????????}
    ????????????});????????????
    ????????????????????????
    ????????}
    ????????
    ????????/**
    ?????????*?返回關(guān)于對話框
    ?????????*/
    ????????public?JDialog?getAboutDialog()
    ????????{
    ????????????return?aboutDialog;
    ????????}
    ????????
    ????????/**
    ?????????*?設(shè)置"關(guān)于"對話框的外觀及響應(yīng)事件,操作和JFrame一樣都是在內(nèi)容
    ?????????*?框架上進行的
    ?????????*/
    ????????public?void?initAboutDialog()
    ????????{
    ????????????aboutDialog.setTitle("關(guān)于");
    ????????????
    ????????????Container?con?=aboutDialog.getContentPane();
    ?????????????
    ????????????//?Swing?中使用html語句
    ????????????Icon?icon?=?new?ImageIcon("smile.gif");
    ????????????JLabel?aboutLabel?=?new?JLabel("<html><b><font?size=5>"+
    ????????????"<center>Swing?組件大全簡體版!"+"<br>天翼.李",icon,JLabel.CENTER);
    ????????????????????????
    ????????????//JLabel?aboutLabel?=?new?JLabel("Swing?組件大全簡體版!",icon,JLabel.CENTER);
    ????????????con.add(aboutLabel,BorderLayout.CENTER);
    ????????????
    ????????????aboutDialog.setSize(450,225);
    ????????????aboutDialog.setLocation(300,300);????????????????????????
    ????????????aboutDialog.addWindowListener(new?WindowAdapter()
    ????????????{
    ????????????????public?void?WindowClosing(WindowEvent?e)
    ????????????????{
    ????????????????????dispose();
    ????????????????}????????????????????
    ????????????});????????????
    ????????}
    ????}
    ????
    ????////////////////////////////////////////////////////////////////////////////
    ????/**
    ?????*?最左邊模塊,繼承JPanel,初始化內(nèi)容為JTree
    ?????*?JPanel--+
    ?????*?????????--JTree
    ?????*/
    ????class?LeftPanel?extends?JPanel
    ????{
    ????????private?int?i?=?0;
    ????????public?LeftPanel()
    ????????{
    ????????????
    ????????????DefaultMutableTreeNode????root?=?new?DefaultMutableTreeNode("Root");
    ????????????DefaultMutableTreeNode?child?=?new?DefaultMutableTreeNode("Child");
    ????????????DefaultMutableTreeNode?select?=?new?DefaultMutableTreeNode("select");
    ????????????
    ????????????DefaultMutableTreeNode?child1?=?new?DefaultMutableTreeNode(""+i);
    ????????????
    ????????????root.add(child);????????
    ????????????root.add(select);
    ????????????child.add(child1);????
    ????????????
    ????????????JTree?tree?=?new?JTree(root);
    ????????????tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    ????????????
    ????????????//?每個節(jié)點的行高
    ????????????tree.setRowHeight(20);????????????
    ????????????tree.addTreeSelectionListener(new?TreeSelectionListener?()
    ????????????{
    ????????????????public?void?valueChanged(TreeSelectionEvent?e)
    ????????????????{
    ????????????????????//?內(nèi)隱類不能直接引用外部類tree,1.外部變量可申明為final?2.新建外部類的對象
    ????????????????????JTree?tree?=(JTree)e.getSource();
    ????????????????????DefaultMutableTreeNode?selectNode?=?(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
    ????????????????????i++;
    ????????????????????selectNode.add(new?DefaultMutableTreeNode(""+i));
    ????????????????}
    ????????????});????????????
    ????????????
    ????????????tree.setPreferredSize(new?Dimension(100,300));
    ????????//????tree.setEnabled(true);
    ????????????JScrollPane?scrollPane?=?new?JScrollPane(tree);
    ????????????//scrollPane.setSize(100,350);
    ????????????this.add(scrollPane);
    ????????}
    ????}
    ????
    ????////////////////////////////////////////////////////////////////////////////
    ????/**
    ?????*?最下面層模塊,繼承JPanel,初始化內(nèi)容為進度條,并由定時器控制
    ?????*?JPanel--+
    ?????*?????????--JProcessBar??--Timer
    ?????*/
    ????class?BottomPanel?extends?JPanel
    ????{
    ????????private?JProgressBar?pb;
    ????????////////////////////////////////////////
    ????????//public?class?
    ????????//////////////////////////////
    ????????public?BottomPanel()
    ????????{
    ????????????pb?=?new?JProgressBar();
    ????????????pb.setPreferredSize(new?Dimension(680,20));
    ????????????
    ????????????//?設(shè)置定時器,用來控制進度條的處理
    ????????????Timer?time?=?new?Timer(1,new?ActionListener()
    ????????????{?
    ????????????????int?counter?=?0;
    ????????????????public?void?actionPerformed(ActionEvent?e)
    ????????????????{
    ????????????????????counter++;
    ????????????????????pb.setValue(counter);
    ????????????????????Timer?t?=?(Timer)e.getSource();
    ????????????????????
    ????????????????????//?如果進度條達到最大值重新開發(fā)計數(shù)
    ????????????????????if?(counter?==?pb.getMaximum())
    ????????????????????{
    ????????????????????????t.stop();
    ????????????????????????counter?=0;
    ????????????????????????t.start();
    ????????????????????}????????????????????
    ????????????????}
    ????????????});
    ????????????time.start();
    ????????????
    ????????????pb.setStringPainted(true);
    ????????????pb.setMinimum(0);
    ????????????pb.setMaximum(1000);
    ????????????pb.setBackground(Color.white);
    ????????????pb.setForeground(Color.red);
    ????????????????????????
    ????????????this.add(pb);????????????????
    ????????}
    ????????
    ????????/**
    ?????????*?設(shè)置進度條的數(shù)據(jù)模型
    ?????????*/
    ????????public?void?setProcessBar(BoundedRangeModel?rangeModel)
    ????????{
    ????????????pb.setModel(rangeModel);
    ????????}
    ????}
    ????
    ????////////////////////////////////////////////////////////////////////////////
    ????/**
    ?????*?最右邊模塊,繼承JPanel,初始化各種按鈕
    ?????*?JPanel--+
    ?????*?????????--JButton??--JToggleButton?--?JList?--?JCombox?--JCheckBox?....
    ?????*/
    ????class?RightPanel?extends?JPanel
    ????{
    ????????public?RightPanel()
    ????????{
    ????????????this.setLayout(new?GridLayout(8,1));????????
    ????????????
    ????????????
    ????????????//?初始化各種按鈕
    ????????????JCheckBox?checkBox?=?new?JCheckBox("復(fù)選按鈕");????????????
    ????????????JButton?button?=?new?JButton("打開文件");
    ????????????button.addActionListener(new?ActionListener()
    ????????????{
    ????????????????public?void?actionPerformed(ActionEvent?e)
    ????????????????{
    ????????????????????JFileChooser?file?=?new?JFileChooser();
    ????????????????????int?result?=?file.showOpenDialog(new?JPanel());
    ????????????????????if?(result?==file.APPROVE_OPTION)?
    ????????????????????{
    ????????????????????????String?fileName?=?file.getSelectedFile().getName();????????????????????
    ????????????????????????String?dir?=?file.getCurrentDirectory().toString();
    ??????????????????????????JOptionPane.showConfirmDialog(null,dir+"\\"+fileName,"選擇的文件",JOptionPane.YES_OPTION);
    ?????????????????????}
    ????????????????}
    ????????????});
    ????????????////////////////////////////////////////
    ????????????//public
    ????????????//////////////////////////////////////////
    ????????????????????
    ????????????JToggleButton?toggleButton?=?new?JToggleButton("雙態(tài)按鈕");
    ????????????
    ????????????ButtonGroup????buttonGroup?=?new?ButtonGroup();
    ????????????JRadioButton?radioButton1?=?new?JRadioButton("單選按鈕1",false);
    ????????????JRadioButton?radioButton2?=?new?JRadioButton("單選按鈕2",false);
    ????????????
    ????????????//?組合框的處理
    ????????????JComboBox?comboBox?=?new?JComboBox();
    ????????????comboBox.setToolTipText("點擊下拉列表增加選項");
    ????????????comboBox.addActionListener(new?ActionListener()?
    ????????????{
    ????????????????public?void?actionPerformed(ActionEvent?e)
    ????????????????{
    ????????????????????JComboBox?comboBox?=(JComboBox)e.getSource();
    ????????????????????comboBox.addItem("程序員");
    ????????????????????comboBox.addItem("分析員");
    ????????????????}
    ????????????});
    ????????????
    ????????????//?列表框的處理
    ????????????DefaultListModel?litem?=?new?DefaultListModel();
    ????????????litem.addElement("香蕉");
    ????????????litem.addElement("水果");
    ????????????JList?list?=?new?JList(litem);
    ????????????
    ????????????
    ????????????list.addListSelectionListener(new?ListSelectionListener?()
    ????????????{
    ????????????????public?void?valueChanged(ListSelectionEvent?e)
    ????????????????{
    ????????????????????JList?l?=?(JList)e.getSource();
    ????????????????????Object?s=?l.getSelectedValue();
    ????????????????????JOptionPane.showMessageDialog(null,s,"消息框",JOptionPane.YES_OPTION);
    ????????????????}
    ????????????});
    ????????????
    ????????????//?增加按鈕組
    ????????????buttonGroup.add(radioButton1);
    ????????????buttonGroup.add(radioButton2);
    ????????????
    ????????????//?增加各種按鈕到JPanel中顯示
    ????????????add(button);
    ????????????add(toggleButton);
    ????????????add(checkBox);
    ????????????add(radioButton1);????????????
    ????????????add(radioButton2);
    ????????????add(comboBox);
    ????????????
    ????????????add(list);
    ????????????
    ????????????this.setBorder(new?EtchedBorder(EtchedBorder.LOWERED,Color.LIGHT_GRAY,Color.blue));
    ????????}????????
    ????}
    ????
    ????////////////////////////////////////////////////////////////////////////////
    ????/**
    ?????*?中間層模塊,繼承JPanel,初始化頁簽,并在頁簽中設(shè)置文本區(qū),表格,
    ?????*?文本區(qū)上下用分隔條分隔
    ?????*?JPanel--+
    ?????*?????????-JTabbedPane--+
    ?????*??????????????????????????--Draw????--JTable??-JTextAreas?-JText?--JPopupMenu
    ?????*/
    ????class?CenterPanel?extends?JPanel
    ????{
    ????????public?CenterPanel()
    ????????{
    ????????????JTabbedPane?tab?=?new?JTabbedPane(JTabbedPane.TOP);
    ????????????
    ????????????JTextField?textField?=?new?JTextField("文本域,點擊打開<文件按鈕>可選擇文件");
    ????????????textField.setActionCommand("textField");
    ????????????
    ????????????JTextPane?textPane?=?new?JTextPane();
    ????????????textPane.setCursor(new?Cursor(Cursor.TEXT_CURSOR));
    ????????????textPane.setText("編輯器,試著點擊文本區(qū),試著拉動分隔條。");
    ????????????????????????
    ????????????textPane.addMouseListener(new?MouseAdapter?()?
    ????????????{
    ????????????????public?void?mousePressed?(MouseEvent?e)
    ????????????????{
    ????????????????????JTextPane?textPane?=?(JTextPane)e.getSource();
    ????????????????????textPane.setText("編輯器點擊命令成功");
    ????????????????//????textField.setText(""+textPane.getText());
    ????????????????}
    ????????????});
    ????????????
    ????????????/*
    ????????????UpperCaseDocument?doc?=?new?Document();?
    ????????????textField.setDocumentsetDocument(doc);
    ????????????doc.addDocumentListener(new?DocumentListener()
    ????????????{
    ????????????????public?void?changedUpdate(DocumentEvent?e){}
    ????????????????public?void?removeUpdate(DocumentEvent?e){}
    ????????????????public?void?insertUpdate(DocumentEvent?e)
    ????????????????{
    ????????????????????Document?text?=?(Document)e.getDocument();
    ????????????????????text.setText("復(fù)制成功");
    ????????????????}????????????????
    ????????????});
    ????????????*/
    ????????????
    ????????????JSplitPane?splitPane?=?new?JSplitPane(JSplitPane.VERTICAL_SPLIT,textField,textPane);
    ????????????
    ????????????????
    ????????????JTable?table?=?new?JTable(10,10);
    ????????????//table.showHorizontalLines(true);
    ????????????//table.showVerticalLines(true);
    ????????????//table.gridColor(Color.blue);
    ????????????
    ????????????JPanel?pane??=?new?JPanel();
    ????????????pane.add(table.getTableHeader(),BorderLayout.NORTH);
    ????????????pane.add(table);
    ????????????????????????
    ????????????tab.addTab("文本演示",splitPane);
    ????????????//tab.addTab(table.getTableHeader());
    ????????????tab.addTab("表格演示",pane);
    ????????????tab.setPreferredSize(new?Dimension(500,600));
    ????????????this.add(tab);
    ????????????this.setEnabled(true);????????????
    ????????}
    ????}
    ????
    ????
    ????public?static?void?main(String?args[])
    ????{
    ????????//?設(shè)置主框架屬性,此處沒有使用,可打開看看效果
    ????????//try
    ????????//{
    ????????//????UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    ????????//}
    ????????//catch??(Exception?e){}
    ????????new?SwingTest();????????????????????????
    ????}
    }



    評論

    # re: java swing 組件大全(新手快進)  回復(fù)  更多評論   

    2007-01-05 16:46 by 組件大
    java swing 組件大全
    http://www.rcomponent.com
    主站蜘蛛池模板: 国产精品无码亚洲精品2021| 两个人的视频高清在线观看免费| 国产午夜亚洲精品国产| 亚洲综合无码精品一区二区三区| 成人免费午间影院在线观看| 日韩精品无码专区免费播放| 曰批免费视频播放免费| 亚洲色偷偷偷综合网| 亚洲午夜免费视频| jlzzjlzz亚洲乱熟在线播放| 国产成人无码免费视频97| 日本人的色道免费网站| 久久99免费视频| 91在线视频免费观看| 人妻仑乱A级毛片免费看| 久久久久亚洲精品无码网址色欲 | 亚洲视频在线观看视频| 久久精品国产69国产精品亚洲| 国产亚洲福利一区二区免费看| 国内免费高清在线观看| 99爱在线精品免费观看| 久久99免费视频| 最近2019中文免费字幕在线观看 | 免费观看国产小粉嫩喷水| 久久久久久国产精品免费免费| 一区二区三区在线免费看| 免费看一区二区三区四区| 国产精品一区二区三区免费| 一道本不卡免费视频| 一级做a爰全过程免费视频毛片| 麻豆va在线精品免费播放 | 国产精品国产免费无码专区不卡 | 亚洲色欲色欲www在线丝| 亚洲国产成人五月综合网| 亚洲精品第一国产综合精品99| 亚洲AV无码一区二三区 | 人人公开免费超级碰碰碰视频| 香港特级三A毛片免费观看| 免费无码国产V片在线观看| 一二三四在线观看免费中文在线观看| 男女猛烈无遮掩视频免费软件|