The purpose and content of the experiment
1. Purpose of the experiment
- Master the generation and use of controls under the Swing graphical user interface.
- Master the layout design of the Java window.
2. Experimental content
Implement the following programs on the computer and observe the running conditions of the programs:
- Use a JDialog dialog box to display a greeting.
- Use the radio button group to carry out the aspiration selection process, and only one can be selected at a time.
experiment procedure
greeting program
package test8;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class jdialog {
public static void main(String[] args)throws Exception {
//编写主窗口
JButton but1 = new JButton("确定");
JFrame f = new JFrame("显示问候语实验");
f.setLayout(null);
f.setTitle("问候程序");
f.setSize(600, 150);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel txt = new JLabel("请输入你的名字");
txt.setLocation(100,10);
txt.setSize(100,30);
f.add(txt);
JTextField input1 = new JTextField(1);
input1.setSize(200, 30);
input1.setLocation(200, 10);
f.add(input1);
but1.setSize(60,30);
but1.setLocation(200,50);
f.add(but1);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认关闭按钮
f.setVisible(true);
//添加点击事件
but1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
String name = input1.getText().trim();
JDialog d1 = new JDialog();
d1.setSize(200, 100);
JTextField txt2 = new JTextField(name + ",你好!", 1);
txt2.setEditable(false);
d1.add(txt2);
d1.show();
}
});
}
}
The size and position can be changed by yourself, no adjustment will be made here
radio program
package test8;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class danxuan {
public static void main(String[] args)throws Exception {
//编写主窗口
JFrame f = new JFrame("单选实验");
f.setLayout(null);
f.setTitle("单选程序");
f.setSize(300, 150);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel txt = new JLabel("将来要当");
txt.setLocation(10,10);
txt.setSize(100,30);
f.add(txt);
JRadioButton choose1=new JRadioButton("教师",false);
JRadioButton choose2=new JRadioButton("工程师",false);
JRadioButton choose3=new JRadioButton("程序员",false);
choose1.setLocation(70, 10);
choose1.setSize(60, 30);
choose2.setLocation(140, 10);
choose2.setSize(80, 30);
choose3.setLocation(220, 10);
choose3.setSize(70, 30);
f.add(choose1);
f.add(choose2);
f.add(choose3);
JLabel out = new JLabel("你选择了将来要当:");
out.setSize(200,50);
out.setLocation(50,50);
f.add(out);
choose1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(choose1.isSelected()){
out.setText("你选择了将来要当:教师");
}
}
});
choose2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(choose2.isSelected()){
out.setText("你选择了将来要当:工程师");
}
}
});
choose3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(choose3.isSelected()){
out.setText("你选择了将来要当:程序员");
}
}
});
f.setVisible(true);
}
}