java组件小计--JRadioButton单选框

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

你可能感兴趣的:(java)