📄 registframe.java
字号:
//JCheckBoxDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.net.*;
public class RegistFrame
extends JDialog
implements ActionListener {
private JLabel sexLabel, trueNameLabel,nickNameLabel,secretLabel;
private ButtonGroup group;
JRadioButton[] radios; //单选按钮数组
private Color color; //设置字体颜色
private JTextField sexTF,trueNameTF,nickNameTF,secretTF;
private JPanel sexPanel = new JPanel(); //define panels
private JPanel imagePanel = new JPanel();
private JPanel fourInfoPanel = new JPanel();
private JPanel threeInfoPanel = new JPanel();
private JPanel exitBtnPanel = new JPanel();
private JButton okButton = new JButton();
private JLabel imageLabel = new JLabel("", SwingConstants.CENTER);
private ImageIcon copyrightImage = new ImageIcon();
private GridLayout gridLayout = new GridLayout(4, 2);
int ScreenWidth,ScreenHeight;
UDPClient udpClient = new UDPClient();
String sexStr,trueNameStr,nickNameStr,secretStr;
public RegistFrame() {
init();
setResizable(true);
pack();
ScreenWidth = getToolkit().getScreenSize().width;//将窗口置于屏幕中央,宽度与高度皆为屏幕一半
ScreenHeight = getToolkit().getScreenSize().height;
setLocation((ScreenWidth - getWidth()) / 2,(ScreenHeight - getHeight()) / 2);
show();
}
public static void main(String args[])
{
new RegistFrame();
}
private void init() {
setTitle("RegistFrame");
Container container = getContentPane(); //getContenPane
container.setLayout(new BorderLayout());
copyrightImage = new ImageIcon(RegistFrame.class.getResource("images\\84.gif"));
imageLabel.setIcon(copyrightImage);
imagePanel.add(imageLabel,null);
sexPanel.setLayout(new FlowLayout()); //Panel Layout
imagePanel.setLayout(new FlowLayout());
fourInfoPanel.setLayout(new BorderLayout());
threeInfoPanel.setLayout(gridLayout);
exitBtnPanel.setLayout(new FlowLayout());
sexLabel = new JLabel("性别:");
color = new Color(255, 0, 0);
sexLabel.setForeground(color);
sexPanel.add(sexLabel);
group = new ButtonGroup();
radios = new JRadioButton[] {
new JRadioButton("男"),
new JRadioButton("女"),
};
radios[0].setSelected(true);//将radios[0]初始化为选中状态
JRadioButtonHandler radioButtonHandler = new JRadioButtonHandler();
for (int i = 0; i < radios.length; i++) {
group.add(radios[i]);
sexPanel.add(radios[i]);
radios[i].addItemListener(radioButtonHandler); //监听单选按钮事件
}
trueNameLabel = new JLabel("真实姓名:");
trueNameLabel.setFont(new Font("Monospaced", Font.PLAIN, 16));
trueNameTF=new JTextField(7);
threeInfoPanel.add(trueNameLabel);
threeInfoPanel.add(trueNameTF);
nickNameLabel = new JLabel("妮称:");
nickNameLabel.setFont(new Font("Monospaced", Font.PLAIN, 16));
nickNameTF=new JTextField(7);
threeInfoPanel.add(nickNameLabel);
threeInfoPanel.add(nickNameTF);
secretLabel = new JLabel("密码:");
secretLabel.setFont(new Font("Monospaced", Font.PLAIN, 16));
secretTF=new JTextField(7);
threeInfoPanel.add(secretLabel);
threeInfoPanel.add(secretTF);
okButton.setText("确定");
okButton.setFont(new Font("Monospaced", Font.BOLD, 15));
okButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
okButton.addActionListener(this);
exitBtnPanel.add(okButton);
container.add(imagePanel,BorderLayout.WEST);
container.add(fourInfoPanel,BorderLayout.CENTER);
fourInfoPanel.add(sexPanel,BorderLayout.NORTH);
fourInfoPanel.add(threeInfoPanel,BorderLayout.CENTER);
container.add(exitBtnPanel,BorderLayout.SOUTH);
addWindowListener( // 按下关闭钮时结束程序
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
}
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}
private class JRadioButtonHandler
implements ItemListener {
public void itemStateChanged(ItemEvent event) {
byte[] msg = new byte[1];
if (event.getSource() == radios[0]) {
color = new Color(255, 0, 0);
// sexString="男";
}
if (event.getSource() == radios[1]) {
color = new Color(0, 255, 0);
// sexString="女";
}
sexLabel.setForeground(color); //设置字体颜色
}
}
// public void processWindowEvent(WindowEvent e) {
// if (e.getID() == WindowEvent.WINDOW_CLOSING) {
// dispose();
// }
// super.processWindowEvent(e);
// }
public void actionPerformed(ActionEvent e) { //
if (e.getSource() == okButton) {
// try {
// udpClient.go();
// } catch (IOException e1) {
// System.out.println ("Exception occurred with socket.");
// System.out.println (e1);
// // System.exit (1);
// }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -