JAVA的图形界面下有两组控件,一组是awt,一组是swing。
一般都是使用swing
1 JLabel 标签 JLabel用于显示文字,java GUI 显示图片是通过在label上设置图标实现的
2 setIcon 使用JLabel显示图片 普通按钮
3 JButton 按钮
4 JCheckBox 复选框 使用isSelected来获取是否选中了
JCheckBox bCheckBox = new JCheckBox("物理英雄");
//设置 为 默认被选中
bCheckBox.setSelected(true);
bCheckBox.setBounds(50, 50, 130, 30);
JCheckBox bCheckBox2 = new JCheckBox("魔法 英雄");
bCheckBox2.setBounds(50, 100, 130, 30);
//判断 是否 被 选中
System.out.println(bCheckBox2.isSelected());
5 JRadioButton 单选框 使用isSelected来获取是否选中了
JRadioButton b1 = new JRadioButton("物理英雄");
// 设置 为 默认被选中
b1.setSelected(true);
b1.setBounds(50, 50, 130, 30);
JRadioButton b2 = new JRadioButton("魔法 英雄");
b2.setBounds(50, 100, 130, 30);
System.out.println(b2.isSelected());
6 ButtonGroup 按钮组
ButtonGroup 对按钮进行分组,把不同的按钮,放在同一个分组里 ,同一时间,只有一个 按钮 会被选中
7 JComboBox 下拉框
使用getSelectedItem来获取被选中项
使用setSelectedItem() 来指定要选中项
//下拉框出现的条目
String[] heros = new String[] { "卡特琳娜", "库奇" };
JComboBox cb = new JComboBox(heros);
cb.setBounds(50, 50, 80, 30);
8 JOptionPane 对话框
JOptionPane 用于弹出对话框
JOptionPane.showConfirmDialog(f, "是否 使用外挂 ?");
表示询问,第一个参数是该对话框以哪个组件对齐
JOptionPane.showInputDialog(f, "请输入yes,表明使用外挂后果自负");
接受用户的输入
JOptionPane.showMessageDialog(f, "你使用外挂被抓住! 罚拣肥皂3次!");
显示消息
int option;
option = JOptionPane.showConfirmDialog(f,"是否使用外挂");
if(JOptionPane.OK_OPTION == option) {
String answer = JOptionPane.showInputDialog(f, "请输入yes,表明使用外挂后果自负");
if ("yes".equals(answer))
JOptionPane.showMessageDialog(f, "你使用外挂被抓住! 罚拣肥皂3次!");
}//挺有意思的,以后看到,要留意一下
9 JTextField 文本框
setText 设置文本
getText 获取文本
JTextField 是单行文本框,如果要输入多行数据,请使用JTextArea
tfPassword.grabFocus(); 表示让密码输入框获取焦点
JLabel lName = new JLabel("账号:");
// 输入框
JTextField tfName = new JTextField("");
tfName.setText("请输入账号");//设置框内的文字
tfName.setPreferredSize(new Dimension(80, 30));//设置框的大小
JLabel lPassword = new JLabel("密码:");
// 输入框
JTextField tfPassword = new JTextField("");
tfPassword.setText("请输入密码");
tfPassword.setPreferredSize(new Dimension(80, 30));
f.add(lName);
f.add(tfName);
f.add(lPassword);
f.add(tfPassword);
10 JPasswordField 密码框
与文本框不同,获取密码框里的内容,推荐使用getPassword,该方法会返回一个字符数组,而非字符串
JLabel l = new JLabel("密码:");
// 密码框
JPasswordField pf = new JPasswordField("");
pf.setText("&48kdh4@#");
pf.setPreferredSize(new Dimension(80, 30));
char[] password = pf.getPassword();
String p = String.valueOf(password);
System.out.println(p);
f.add(l);
f.add(pf);
11 JTextArea 文本域
和文本框JTextField不同的是,文本域可以输入多行数据
如果要给文本域初始文本,通过\n来实现换行效果
JTextArea通常会用到append来进行数据追加
如果文本太长,会跑出去,可以通过setLineWrap(true) 来做到自动换行
JLabel l = new JLabel("文本域:");
JTextArea ta = new JTextArea();
ta.setPreferredSize(new Dimension(200, 150));
//\n换行符
ta.setText("抢人头!\n抢你妹啊抢!\n");
//追加数据
ta.append("我去送了了了了了了了了了了了了了了了了了了了了了了了了");
//设置自动换行
ta.setLineWrap(true);
f.add(l);
f.add(ta);
12 JProgressBar 进度条
JProgressBar pb = new JProgressBar();
//进度条最大100
pb.setMaximum(100);
//当前进度是50
pb.setValue(50);
//显示当前进度
pb.setStringPainted(true);
13 JFileChooser 文件选择器
JFileChooser 表示文件选择器
使用FileFilter用于仅选择.txt文件
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(); 用于打开文件
fc.showSaveDialog(); 用于保存文件
JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
// TODO Auto-generated method stub
return ".txt";
}
@Override
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".txt");
}
});//赶脚这个东西有点高级