51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Java学习之路-GUI-类似表单界面

完成个人信息收集的窗口的界面。要求使用到所有学习过的组件。单选按钮的实现自己完成。

package GUI;

import javax.swing.; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener;

public class PersonalInfoUI {

public PersonalInfoUI() {
JFrame jf = new JFrame("Personal Info");
jf.setSize(250, 520);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
jf.setResizable(false);

JPanel jp = new JPanel(new FlowLayout());

//姓名
JLabel jLabel_Name = new JLabel();
jLabel_Name.setText("Name: ");
jLabel_Name.setFont(new Font(null, Font.PLAIN, 20));

//性别
JLabel jLabel_Gender = new JLabel();
jLabel_Gender.setText("Gender: ");
jLabel_Gender.setFont(new Font(null, Font.PLAIN, 18));

//爱好
JLabel jLabel_Hobby = new JLabel();
jLabel_Hobby.setText("Hobby: ");
jLabel_Hobby.setFont(new Font(null, Font.PLAIN, 18));

//学历
JLabel jLabel_Education = new JLabel();
jLabel_Education.setText("Education: ");
jLabel_Education.setFont(new Font(null, Font.PLAIN, 18));

//简介
JLabel jLabel_Introduction = new JLabel();
jLabel_Introduction.setText("Introduction: ");
jLabel_Introduction.setFont(new Font(null, Font.PLAIN, 18));

//姓名输入
JTextField jTextField_Name = new JTextField(10);
jTextField_Name.setFont(new Font(null, Font.PLAIN, 18));

//爱好输入
JTextField jTextField_Hobby = new JTextField(15);
jTextField_Hobby.setFont(new Font(null, Font.PLAIN, 18));
jTextField_Hobby.setEnabled(false);     //设置默认不可编辑

//性别 单选按钮
JRadioButton jRadioButton_Gender_Male = new JRadioButton("Male");
JRadioButton jRadioButton_Gender_Female = new JRadioButton("Female");

//性别 按钮组, 将俩按钮加入
ButtonGroup buttonGroup_Gender = new ButtonGroup();
buttonGroup_Gender.add(jRadioButton_Gender_Male);
buttonGroup_Gender.add(jRadioButton_Gender_Female);

//性别 设置男性为选中
jRadioButton_Gender_Male.setSelected(true);

//爱好 设置多个爱好复选框
JCheckBox jCheckBox_Hobby_Music = new JCheckBox("Music");
JCheckBox jCheckBox_Hobby_Sports = new JCheckBox("Sports");
JCheckBox jCheckBox_Hobby_Movies = new JCheckBox("Movies");
JCheckBox jCheckBox_Hobby_Reading = new JCheckBox("Reading");
JCheckBox jCheckBox_Hobby_Other = new JCheckBox("Other");

//爱好 当Other被选中时允许编辑框编辑
jCheckBox_Hobby_Other.addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent changeEvent) {
        JCheckBox checkBox = (JCheckBox)changeEvent.getSource();
        if(checkBox.isSelected()){
            jTextField_Hobby.setEnabled(true);
        }else{
            jTextField_Hobby.setEnabled(false);
        }
    }
});

//学历 创建下拉列表条目
String[] list_Education = new String[]{"Primary school","Junior high school","Senior high school","Undergraduate","Junior college"};

//学历 创建下拉列表
final JComboBox<String> jComboBox_Education = new JComboBox<String>(list_Education);

//简介 文本区域
final JTextArea jTextArea_Introduction = new JTextArea(9,15);
jTextArea_Introduction.setLineWrap(true);
jTextArea_Introduction.setFont(new Font(null, Font.PLAIN, 18));

//简介 为文本区域添加滚动条, 并设置为永不可见
JScrollPane jsp = new JScrollPane(jTextArea_Introduction);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

//确定按钮
JButton post = new JButton("P O S T !");

post.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("Name is " + jTextField_Name.getText());
        System.out.println("Gender is " + (jRadioButton_Gender_Male.isSelected() == true ? "Male" : "Female"));
        System.out.println("Hobbies are " + (jCheckBox_Hobby_Music.isSelected() == true ? "Music " : "") +
                (jCheckBox_Hobby_Sports.isSelected() == true ? "Sports " : "") +
                (jCheckBox_Hobby_Movies.isSelected() == true ? "Movies " : "") +
                (jCheckBox_Hobby_Reading.isSelected() == true ? "Reading " : "") +
                (jCheckBox_Hobby_Other.isSelected() == true ? jTextField_Hobby.getText() : ""));
        System.out.println("Education is " + jComboBox_Education.getSelectedItem());
        System.out.println("Introduction is " + jTextArea_Introduction.getText());
    }
});

//依次将控件加入Panel
jp.add(jLabel_Name);
jp.add(jTextField_Name);

jp.add(jLabel_Gender);
jp.add(jRadioButton_Gender_Male);
jp.add(jRadioButton_Gender_Female);

jp.add(jLabel_Hobby);
jp.add(jCheckBox_Hobby_Music);
jp.add(jCheckBox_Hobby_Sports);
jp.add(jCheckBox_Hobby_Movies);
jp.add(jCheckBox_Hobby_Reading);
jp.add(jCheckBox_Hobby_Other);
jp.add(jTextField_Hobby);

jp.add(jLabel_Education);
jp.add(jComboBox_Education);

jp.add(jLabel_Introduction);
jp.add(jTextArea_Introduction);

jp.add(post);

jf.setContentPane(jp);
jf.setVisible(true);

}

public static void main (String[] args){

new PersonalInfoUI();

}

}


赞(2)
未经允许不得转载:工具盒子 » Java学习之路-GUI-类似表单界面