java組件小計–JRadioButton單選框

來源:互聯網
上載者:User
package gui;          import java.awt.event.ActionEvent;import java.awt.event.ActionListener;          import javax.swing.BoxLayout;import javax.swing.ButtonGroup;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.JTextField;          public class CheckNet extends JFrame {/**主面板**/JPanel mainPanel;/**單選框的面板**/JPanel netPanel;/**ip及連接埠號碼的面板**/JPanel txtPanel;/**按鈕面板**/JPanel btnPanel;/**區域網路跟互連網的單選框**/JRadioButton rBtnNet1,rBtnNet2;/**用來承裝互斥組件的**/ButtonGroup buttonGroup;/**IP地址跟連接埠號碼的文字框**/JTextField ipTxt,portTxt;/**確定按鈕**/JButton confirmBtn;/**取消按鈕**/JButton cancelBtn;public CheckNet(IOTFrame iotFrame){mainPanel = new JPanel();rBtnNet1 = new JRadioButton("區域網路",true);rBtnNet2 = new JRadioButton("互連網");buttonGroup = new ButtonGroup();netPanel = new JPanel();ipTxt = new JTextField();portTxt = new JTextField();ipTxt.setText("192.168.1.110");        portTxt.setText("8000");txtPanel = new JPanel();confirmBtn = new JButton("確定");cancelBtn = new JButton("取消");btnPanel = new JPanel();//設定主面板的布局mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));//設定單選框區的布局netPanel.setLayout(new BoxLayout(netPanel, BoxLayout.X_AXIS));buttonGroup.add(rBtnNet1);buttonGroup.add(rBtnNet2);//將單選項按鈕加入ButtonGroup中,起到控制項互斥作用,選擇了1,2就變為不選中,選中了2,1便不選中。netPanel.add(rBtnNet1);netPanel.add(rBtnNet2);//就算加入了ButtonGroup,仍需把然rBtnNet1、rBtnNet2加入面板中。此處不可將buttonGroup加入JPanel。//設定IP地址及連接埠號碼的布局txtPanel.setLayout(new BoxLayout(txtPanel, BoxLayout.Y_AXIS));txtPanel.add(ipTxt);txtPanel.add(portTxt);//設定IP地址及連接埠號碼的布局btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.X_AXIS));btnPanel.add(confirmBtn);btnPanel.add(cancelBtn);//主面板添加組件mainPanel.add(netPanel);mainPanel.add(txtPanel);mainPanel.add(btnPanel);add(mainPanel);pack();setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLocation(300, 200);setResizable(false);        //實現監聽CustomListener customListener = new CustomListener();rBtnNet1.addActionListener(customListener);rBtnNet2.addActionListener(customListener);//為確定按鈕添加確定的事件, 即建立一個WorkSpace對象/*confirmBtn.addActionListener(new ConfirmButtonListener(this, editorFrame));*/confirmBtn.addActionListener(new ConfirmButtonListener(this,iotFrame));//為取消按鈕添加退出事件cancelBtn.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {System.exit(0);}});}          class CustomListener implements ActionListener{  public void actionPerformed(ActionEvent evt)   {     JRadioButton rButton=(JRadioButton)evt.getSource();     if(rButton==rBtnNet1)       {                            ipTxt.setText("192.168.1.110");         portTxt.setText("8000");        // container.validate();//如果容器本身和它所包含的組件布局及其大小發生變化的化,調用該方法後能夠使這種變化在介面上被反映出來       }               else if(rButton==rBtnNet2)       {                      ipTxt.setText("http://cn1913.88ip.org");         portTxt.setText("8000");        // container.validate();       }           }}                    /** * 確定按鈕的監聽器 * @author hp * */class ConfirmButtonListener implements ActionListener {private CheckNet checkNet;//private SpaceFrame spaceFrame;private IOTFrame iotFrame;//private EditorFrame editorFrame;//public ConfirmButtonListener(SpaceFrame spaceFrame, EditorFrame editorFrame) {//this.spaceFrame = spaceFrame;//this.editorFrame = editorFrame;//}public ConfirmButtonListener(CheckNet checkNet,IOTFrame iotFrame){this.checkNet = checkNet;this.iotFrame = iotFrame;};public void actionPerformed(ActionEvent arg0) {//調EditorFrame的initFrame方法初始化介面//editorFrame.initFrame(new WorkSpace(spaceFrame.getFolder(), editorFrame));//將EditorFrame設為可見iotFrame.setVisible(true);//editorFrame.setSize(900, 600);//讓工作選擇空間介面不可見   checkNet.setVisible(false);}}          }

聯繫我們

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