下拉框JComboBox
样子下面有附图
代码分析:
- 在容器中添加下拉框组件,我写了三种形式其中第三种用到了下拉框模板通过接口comboBoxModel设置模板形式,然后建立下拉框模板
setModel - 在组件后面我添加了一个按钮打印下拉框的内容以及其索引getSelectIndex
and getSelectItem,通过按钮的监听,打印其内容如下图 - 注意下拉框里的内容可以设置可以编辑 setEditable(true)
package swing;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
//任务 下拉框的应用
public class class11 extends JFrame {
public class11() {
setBounds(100, 100, 300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
/*第一种方法 JComboBox<String> combo=new JComboBox<String>();
combo.addItem("学生证");
combo.addItem("学生证");
combo.addItem("学生证");
*/
String items[]= {"学生证","工作证","身份证"};
/*第二种方法
* JComboBox<String> combo=new JComboBox<String>(items);
*
*/
//第三种方法要使用模板 comboBox接口
JComboBox<String> combo=new JComboBox<String>();
ComboBoxModel<String> aModel=new DefaultComboBoxModel<String>(items);
combo.setModel(aModel);
combo.setBounds(10, 10, 80, 30);
Container c=getContentPane();
c.setLayout(null);
c.add(combo);
JButton button=new JButton("打印");
//添加按钮事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//获取选中的索引
System.out.println("选中的索引:"+combo.getSelectedIndex());
System.out.println("选中的值:"+combo.getSelectedItem());
}
});
button.setBounds(100, 10, 60, 30);
c.add(button);
combo.setEditable(true);//下拉框中的值可以输入改变
setVisible(true);
}
public static void main(String[] args) {
new class11();
}
}
列表框JList
下面是列表框的简单创建
package swing;
import java.awt.Container;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
//列表框
public class class12 extends JFrame{
public class12() {
setBounds(10, 10, 300,150 );
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(null);//设置绝对布局
String items[]= {"元素1","元素2","元素3","元素4","元素5","元素6","元素7","元素8"};
JList list=new JList<String>(items);
//列表元素太多,窗体只有200像素所以设置一个滚动条
JScrollPane js=new JScrollPane(list);//为列表框设置滚动条
js.setBounds(10, 10, 80, 60);
c.add(js);
setVisible(true);
}
public static void main(String[] args) {
new class12();
}
}
- 如果使用模组的话会比较好一点,通过setModel()载入数据模型。
而这个model可以使用addElement()添加元素比上面在数组里面单独修改要好的多
-列表框中的元素可以选中通过setSelectModel设置选择模式(有三个参数) - 另外我添加了按钮打印用户选中的元素
package swing;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
//列表框
public class class12 extends JFrame{
public class12() {
setBounds(10, 10, 300,150 );
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(null);//设置绝对布局
String items[]= {"元素1","元素2","元素3","元素4","元素5","元素6","元素7","元素8"};
DefaultListModel<String> model =new DefaultListModel<String>();
for(String item:items) {
model.addElement(item);
}
model.addElement("添加元素");
JList<String> list=new JList<String>();
list.setModel(model);//列表框载入数据模型
// list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//单选
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);//随便选
JButton button=new JButton("确认");
button.setBounds(120, 10, 80, 60);
button.addActionListener(new ActionListener() {//添加监听效果
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//获取你选中的列表框元素
java.util.List<String> vaules=list.getSelectedValuesList();//这里的list的是util包下的列表
for(String vaule:vaules) {
System.out.println(vaule);
}
System.out.println("-----------------");
}
});
c.add(button);
//列表元素太多,窗体只有200像素所以设置一个滚动条
JScrollPane js=new JScrollPane(list);//为列表框设置滚动条
js.setBounds(10, 10, 80, 60);
c.add(js);
setVisible(true);
}
public static void main(String[] args) {
new class12();
}
}
文本框JTextField
分析: 通过JTextField创建后面有一个参数column栏设置文本框的字符大小
我们可以设置文本框里的字体以及里面的内容通过getText获取文本框里的内容,清空后的文本框没有光标我们需要获取光标也就是焦点即requestFocus
package swing;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
//文本框
public class class13 extends JFrame {
public class13() {
setBounds(100, 100, 500, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(new FlowLayout());
//创建文本框
/* JTextField text=new JTextField(80);
*/
JTextField text=new JTextField();
text.setColumns(30);//设置文本框长度 80个字符
text.setFont(new Font("黑体",Font.PLAIN,20));
text.setText("hello world");//设置文本
c.add(text);
JButton button =new JButton("确认");
button.setBounds(100, 10, 60, 50);
c.add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("文本框的内容为:" +text.getText());
text.setText("");//清空文本框
//但是清空后文本框的光标不会自动跳出来所以要设置一下光标
text.requestFocus();//获取光标
}
});
setVisible(true);
}
public static void main(String[] args) {
new class13();
}
}
密码框JPassWordField
和之前的文本框差不多但是注意的是我这里改变了密码框的格式,使用了setEchochar设置了五角星的回显形式
package swing;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
public class class14 extends JFrame{
public class14() {
setBounds(100, 100, 300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(new FlowLayout());
JPasswordField password=new JPasswordField();
password.setColumns(10);//密码长度20
//password.setFont(new Font("Arial",Font.BOLD,18));
//默认的密码格式是一个黑的原点可以设置其格式
password.setEchoChar('\u2605');//设置回显字符
password.addActionListener(new ActionListener() {//添加动作监听 ,回车
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
char []ch=password.getPassword();//字符数组获取密码
String str=new String(ch);
System.out.println(str);
}
});
c.add(password);
setVisible(true);
}
public static void main(String[] args) {
new class14();
}
}
文本域JTextArea
package swing;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
//文本域
public class class15 extends JFrame{
public class15() {
setBounds(100, 100, 200, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c=getContentPane();
c.setLayout(new FlowLayout());
JTextArea jt=new JTextArea();
jt.setRows(5);//设置行列
jt.setColumns(10);
jt.append("添加文本内容");
jt.setText("这是一个文本域");
jt.insert("插入的内容", 2);//后面一个字符表示插入的位置
//jt.setFont(new Font("Arial",Font.BOLD,20));
JScrollPane js=new JScrollPane(jt);
c.add(js);
setVisible(true);
}
public static void main(String[] args) {
new class15();
}
}
心情日记: 昨晚敲c++代码眼皮给我揉肿了!今天一天都不舒服
——才下眉头,却上心头。搞到很晚的确是这样没办法我爱java.