1.尝试开发一个窗体,如图所示。
import java.awt.*;
import javax.swing.*;
public class UseCase1 extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private JComboBox<String> comboBox;
public static void main(String[] args) {
new UseCase1();
}
public UseCase1() {
super();
setVisible(true);
setSize(200,150);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
comboBox = new JComboBox<>();
comboBox.addItem("红");
comboBox.addItem("黄");
comboBox.addItem("蓝");
comboBox.addItem("黑");
getContentPane().add(comboBox, BorderLayout.NORTH);
final JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
final JCheckBox checkBox = new JCheckBox();
checkBox.setText("男");
panel.add(checkBox);
final JCheckBox checkBox_1 = new JCheckBox();
checkBox_1.setText("女");
panel.add(checkBox_1);
final JPanel panel_1 = new JPanel();
getContentPane().add(panel_1, BorderLayout.SOUTH);
final JButton okButton = new JButton();
okButton.setText("确定");
panel_1.add(okButton);
final JButton button_1 = new JButton();
button_1.setText("取消");
panel_1.add(button_1);
}
}
2.尝试创建一个窗体,选择合适的布局管理器,并在窗体中设置一个下拉列表框,初始状态下拉列表框中没有项目,并设置一个按钮,为按钮设置动作事件监听器,当用户单击该按钮时,下拉列表框中相应添加数组中的内容。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UseCase2 extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private String[] contents={"列表1","列表2","列表3","列表4","列表5"};
private JComboBox<String> jc=new JComboBox<>();
private JButton jb=new JButton("提交");
int count=0;
public UseCase2() {
setSize(200, 100);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.add(jc);
cp.add(jb);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(count<contents.length){
jc.addItem(contents[count++]);
}
}
});
}
public static void main(String[] args) {
new UseCase2();
}
}
3.尝试开发一个登录窗体,包括用户名、密码以及提交按钮和重置按钮,当用户输入用户名mr、密码mrsoft时,弹出登录成功提示对话框。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UseCase3 extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public UseCase3(){
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setTitle("登录窗体");
setBounds(300, 200, 300, 150);
Container cp=getContentPane();
cp.setLayout(null);
JLabel jl=new JLabel("用户名:");
jl.setBounds(10, 10, 200, 18);
final JTextField name=new JTextField();
name.setBounds(80, 10, 150, 18);
JLabel jl2=new JLabel("密码:");
jl2.setBounds(10, 50, 200, 18);
final JPasswordField password=new JPasswordField();
password.setBounds(80, 50, 150, 18);
cp.add(jl);
cp.add(name);
cp.add(jl2);
cp.add(password);
JButton jb=new JButton("确定");
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
if(name.getText().trim().length()==0||new String(password.getPassword()).trim().length()==0){
JOptionPane.showMessageDialog(null, "用户名密码不允许为空");
return;
}
if(name.getText().trim().equals("mr")&&new String(password.getPassword()).trim().equals("mrsoft")){
JOptionPane.showMessageDialog(null, "登录成功");
}
else{
JOptionPane.showMessageDialog(null, "用户名或密码错误");
}
}
});
jb.setBounds(80, 80, 60, 18);
cp.add(jb);
final JButton button = new JButton();
button.setText("重置");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成方法存根
name.setText("");
password.setText("");
}
});
button.setBounds(150, 80, 60, 18);
getContentPane().add(button);
}
public static void main(String[] args) {
new UseCase3();
}
}