Java——下拉列表框,复选框,列表框,文件选择框
import java.awt.Container; import java.awt.GridLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Vector; import javax.swing.BorderFactory; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; class MyComboBox{ private JFrame frame = new JFrame("窗体"); //定义窗体 private Container cont = frame.getContentPane(); //得到窗体容器 private JComboBox jcb1 = null; //定义下拉列表框 private JComboBox jcb2 = null; //定义下拉列表框 public MyComboBox(){ this.frame.setLayout(new GridLayout(2,2)); String nations[] = {"中国","美国","韩国","法国","英国"}; Vector<String> v = new Vector<String>(); //定义一个Vector集合 v.add("元素1"); v.add("元素2"); v.add("元素3"); this.jcb1 = new JComboBox(nations); this.jcb2 = new JComboBox(v); //定义一个列表框的边框显示条 jcb1.setBorder(BorderFactory.createTitledBorder("哪个国家?")); jcb2.setBorder(BorderFactory.createTitledBorder("Vector?")); jcb1.setMaximumRowCount(3); //最多显示3个选项 jcb2.setMaximumRowCount(3); cont.add(this.jcb1); cont.add(this.jcb2); cont.add(new JLabel("下拉列表框")); this.frame.setSize(330,200); this.frame.setVisible(true); this.frame.addWindowListener(new WindowAdapter(){ //加入事件监听 public void windowClosing(WindowEvent arg0) { //窗口关闭时触发,按下关闭按钮 // TODO 自动生成的方法存根 System.out.println("windowClosing-->窗口关闭"); System.exit(1); } }); } } //主类 //Function : MyComboBox_demo public class MyComboBox_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 new MyComboBox(); } }
import java.awt.Container; import java.awt.GridLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Vector; import javax.swing.AbstractListModel; import javax.swing.BorderFactory; import javax.swing.ComboBoxModel; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; class MyComboBoxModel extends AbstractListModel implements ComboBoxModel{ String nations[] = {"中国","美国","韩国","法国","英国"}; String item = null; @Override public int getSize() { // TODO 自动生成的方法存根 return this.nations.length; } @Override public Object getElementAt(int index) { // TODO 自动生成的方法存根 return this.nations[index]; } @Override public void setSelectedItem(Object anItem) { // TODO 自动生成的方法存根 this.item = (String) anItem; } @Override public Object getSelectedItem() { // TODO 自动生成的方法存根 return this.item; } } class MyComboBox{ private JFrame frame = new JFrame("窗体"); //定义窗体 private Container cont = frame.getContentPane(); //得到窗体容器 private JComboBox jcb1 = null; //定义下拉列表框 private JComboBox jcb2 = null; //定义下拉列表框 public MyComboBox(){ this.frame.setLayout(new GridLayout(2,2)); // String nations[] = {"中国","美国","韩国","法国","英国"}; // Vector<String> v = new Vector<String>(); //定义一个Vector集合 // v.add("元素1"); // v.add("元素2"); // v.add("元素3"); // this.jcb1 = new JComboBox(nations); // this.jcb2 = new JComboBox(v); this.jcb1 = new JComboBox(new MyComboBoxModel()); //实例化JComboBox //定义一个列表框的边框显示条 jcb1.setBorder(BorderFactory.createTitledBorder("哪个国家?")); // jcb2.setBorder(BorderFactory.createTitledBorder("Vector?")); jcb1.setMaximumRowCount(3); //最多显示3个选项 // jcb2.setMaximumRowCount(3); cont.add(this.jcb1); // cont.add(this.jcb2); cont.add(new JLabel("下拉列表框")); this.frame.setSize(330,200); this.frame.setVisible(true); this.frame.addWindowListener(new WindowAdapter(){ //加入事件监听 public void windowClosing(WindowEvent arg0) { //窗口关闭时触发,按下关闭按钮 // TODO 自动生成的方法存根 System.out.println("windowClosing-->窗口关闭"); System.exit(1); } }); } } public class MyComboBox_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 new MyComboBox(); } }
import java.awt.Container; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Vector; import javax.swing.AbstractListModel; import javax.swing.Action; import javax.swing.BorderFactory; import javax.swing.ComboBoxModel; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; class MyComboBox implements ItemListener,ActionListener{ private JFrame frame = new JFrame("窗体"); //定义窗体 private Container cont = frame.getContentPane(); //得到窗体容器 private JComboBox jcb1 = null; //定义下拉列表框 private JLabel label = new JLabel("标签"); //定义标签 private String fontSize[] = {"10","11","12"}; public MyComboBox(){ this.frame.setLayout(new GridLayout(2,2)); this.jcb1 = new JComboBox(this.fontSize); //实例化JComboBox //定义一个列表框的边框显示条 jcb1.setBorder(BorderFactory.createTitledBorder("请选择显示文字大小")); jcb1.addItemListener(this); //加入选项监听 jcb1.addActionListener(this); //加入动作监听 jcb1.setMaximumRowCount(3); //最多显示3个选项 jcb1.setEditable(true); //设置可编辑文本 jcb1.configureEditor(jcb1.getEditor(), "12"); //定义默认值 this.changeFontSize(12); //设置默认字体 cont.add(this.jcb1); cont.add(label); cont.add(new JLabel("下拉列表框")); this.frame.setSize(330,200); this.frame.setVisible(true); this.frame.addWindowListener(new WindowAdapter(){ //加入事件监听 public void windowClosing(WindowEvent arg0) { //窗口关闭时触发,按下关闭按钮 // TODO 自动生成的方法存根 System.out.println("windowClosing-->窗口关闭"); System.exit(1); } }); } @Override public void actionPerformed(ActionEvent e) { //输入信息时触发 // TODO 自动生成的方法存根 String itemSize = (String) this.jcb1.getSelectedItem(); //得到选项 int Size = 12; try{ Size = Integer.parseInt(itemSize); //字符串转整数 }catch(Exception ex){ this.jcb1.getEditor().setItem("输入的不是数字"); } this.changeFontSize(Size); if(!this.isExists(itemSize)){ this.jcb1.addItem(jcb1.getSelectedItem()); //不存在,加入下拉选项 } } @Override public void itemStateChanged(ItemEvent e) { // TODO 自动生成的方法存根 } public void changeFontSize(int size){ //改变文字大小 Font font = new Font("Serief",Font.BOLD,size); //定义Font对象 this.label.setFont(font); //设置文字大小 } public boolean isExists(String item){ boolean flag = false; for(int i =0;i<this.jcb1.getItemCount();i++){ if(item.equals(this.jcb1.getItemAt(i))){ flag = true; } } return flag; } } public class MyComboBox_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 new MyComboBox(); } }
import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; class MyCheckBox implements ItemListener{ private JFrame frame = new JFrame("窗体"); private Container cont = frame.getContentPane(); private JCheckBox jrb1 = new JCheckBox("单选1"); private JCheckBox jrb2 = new JCheckBox("单选2"); private JCheckBox jrb3 = new JCheckBox("单选3"); private JPanel pan = new JPanel(); public MyCheckBox(){ //定义一个面板的边框显示条 pan.setBorder(BorderFactory.createTitledBorder("请选择按钮")); pan.setLayout(new GridLayout(1,3)); pan.add(this.jrb1); pan.add(this.jrb2); pan.add(this.jrb3); jrb1.addItemListener(this); //加入事件监听 jrb2.addItemListener(this); //加入事件监听 jrb3.addItemListener(this); //加入事件监听 cont.add(pan); this.frame.setSize(330,80); this.frame.setVisible(true); this.frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { //窗口关闭时触发,按下关闭按钮 // TODO 自动生成的方法存根 System.out.println("windowClosing-->窗口关闭"); System.exit(1); } }); } @Override public void itemStateChanged(ItemEvent e) { // TODO 自动生成的方法存根 if(jrb1.isSelected()){ System.out.println("jrb1"); } if(jrb2.isSelected()){ System.out.println("jrb2"); } if(jrb3.isSelected()){ System.out.println("jrb3"); } } } public class JCheckBox_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 new MyCheckBox(); } }
import java.awt.Container; import java.awt.GridLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Vector; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JRadioButton; import javax.swing.ListSelectionModel; class MyList{ private JFrame frame = new JFrame("窗体"); //定义窗体 private Container cont = frame.getContentPane(); //得到窗体容器 private JList list1 = null; //定义列表框 private JList list2 = null; //定义列表框 public MyList(){ this.frame.setLayout(new GridLayout(1,3)); //定义排版,1行2列 String nations[] = {"中国","美国","韩国","法国","英国"}; Vector<String> v = new Vector<String>(); //定义一个Vector集合 v.add("Vector1"); v.add("Vector2"); v.add("Vector3"); this.list1 = new JList(nations); //实例化JList this.list2 = new JList(v); //实例化JList //定义一个列表框的边框显示条 list1.setBorder(BorderFactory.createTitledBorder("哪个国家?")); list2.setBorder(BorderFactory.createTitledBorder("Vector?")); //第一个列表框一次可以选择多个选项 list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); //第二个列表框每次可以选择一个选项 list2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); cont.add(this.list1); //加入面板 cont.add(this.list2); //加入面板 this.frame.setSize(330,200); this.frame.setVisible(true); this.frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { //窗口关闭时触发,按下关闭按钮 // TODO 自动生成的方法存根 System.out.println("windowClosing-->窗口关闭"); System.exit(1); } }); } } public class JList_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 new MyList(); } }
import java.awt.Container; import java.awt.GridLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Vector; import javax.swing.AbstractListModel; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.ListSelectionModel; class MyListModel extends AbstractListModel{ private String nations[] = {"中国","美国","韩国","法国","英国"}; @Override public int getSize() { // TODO 自动生成的方法存根 return this.nations.length; } @Override public Object getElementAt(int index) { //返回指定标号的元素 // TODO 自动生成的方法存根 if(index<this.nations.length){ return this.nations[index]; }else{ return null; } } } class myList{ private JFrame frame = new JFrame("窗体"); //定义窗体 private Container cont = frame.getContentPane(); //得到窗体容器 private JList list1 = null; //定义列表框 private JList list2 = null; //定义列表框 public myList(){ this.list1 = new JList(new MyListModel()); this.list2 = new JList(new MyListModel()); //定义一个列表框的边框显示条 list1.setBorder(BorderFactory.createTitledBorder("哪个国家?")); list2.setBorder(BorderFactory.createTitledBorder("Vector?")); //第一个列表框一次可以选择多个选项 list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); //第二个列表框每次可以选择一个选项 list2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); cont.add(this.list1); //加入面板 cont.add(this.list2); //加入面板 this.frame.setSize(330,200); this.frame.setVisible(true); this.frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { //窗口关闭时触发,按下关闭按钮 // TODO 自动生成的方法存根 System.out.println("windowClosing-->窗口关闭"); System.exit(1); } }); } } public class JModel_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 new MyList(); } }
监听只能加一个
import java.awt.Container; import java.awt.GridLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Vector; import javax.swing.AbstractListModel; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; class MyListModel extends AbstractListModel{ private String nations[] = {"中国","美国","韩国","法国","英国"}; @Override public int getSize() { // TODO 自动生成的方法存根 return this.nations.length; } @Override public Object getElementAt(int index) { //返回指定标号的元素 // TODO 自动生成的方法存根 if(index<this.nations.length){ return this.nations[index]; }else{ return null; } } } class myList implements ListSelectionListener{ private JFrame frame = new JFrame("窗体"); //定义窗体 private Container cont = frame.getContentPane(); //得到窗体容器 private JList list1 = null; //定义列表框 // private JList list2 = null; //定义列表框 public myList(){ this.list1 = new JList(new MyListModel()); // this.list2 = new JList(new MyListModel()); //定义一个列表框的边框显示条 list1.setBorder(BorderFactory.createTitledBorder("哪个国家?")); // list2.setBorder(BorderFactory.createTitledBorder("Vector?")); //第一个列表框一次可以选择多个选项 list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); //第二个列表框每次可以选择一个选项 // list2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list1.addListSelectionListener(this); //加入事件监听 // list2.addListSelectionListener(this); //加入事件监听 cont.add(new JScrollPane(this.list1)); //加入滚动条 cont.add(this.list1); //加入面板 // cont.add(this.list2); //加入面板 this.frame.setSize(330,200); this.frame.setVisible(true); this.frame.addWindowListener(new WindowAdapter(){ //加入事件监听 public void windowClosing(WindowEvent arg0) { //窗口关闭时触发,按下关闭按钮 // TODO 自动生成的方法存根 System.out.println("windowClosing-->窗口关闭"); System.exit(1); } }); } public void valueChanged(ListSelectionEvent e) { // TODO 自动生成的方法存根 int temp[] = list1.getSelectedIndices(); //取得全部选定 System.out.println("选定的内容:"); for(int i=0;i<temp.length;i++){ System.out.println(list1.getModel().getElementAt(i)+"、"); } } } //主类 //Function : JModel_demo public class JModel_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 new myList(); } }
import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.PrintStream; import java.util.Scanner; import javax.imageio.stream.FileImageInputStream; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; class Note implements ActionListener{ private JFrame frame = new JFrame("窗体"); //定义窗体 private JTextArea area = new JTextArea("JTextArea"); //定义文本区 private JButton open = new JButton("打开文件"); //打开文件 private JButton save = new JButton("保存文件"); //保存文件 private JLabel label = new JLabel("现在没有打开的文件"); private JPanel butpan = new JPanel(); public Note(){ this.butpan.add(open); this.butpan.add(save); //设置窗体中的布局管理器为BorderLayout,所有的组件水平和垂直间距为3 frame.setLayout(new BorderLayout(3,3)); frame.add(this.label,BorderLayout.NORTH); frame.add(this.butpan,BorderLayout.SOUTH); frame.add(this.area, BorderLayout.CENTER); this.frame.setSize(330,180); this.frame.setVisible(true); this.frame.addWindowListener(new WindowAdapter(){ //加入事件监听 public void windowClosing(WindowEvent arg0) { //窗口关闭时触发,按下关闭按钮 // TODO 自动生成的方法存根 System.out.println("windowClosing-->窗口关闭"); System.exit(1); } }); this.open.addActionListener(this); this.save.addActionListener(this); // this.frame.setSize(330,180); // this.frame.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { //按键事件监听 // TODO 自动生成的方法存根 File file = null; int result = 0; JFileChooser fileChooser = new JFileChooser(); if(e.getSource() == this.open){ this.area.setText(""); fileChooser.setApproveButtonText("确定"); //定义“确定“按钮” fileChooser.setDialogTitle("打开文件"); //定义文件选择框标题 result = fileChooser.showOpenDialog(this.frame); //显示打开对话框 if(result == JFileChooser.APPROVE_OPTION){ file = fileChooser.getSelectedFile(); this.label.setText("打开的文件名称为:"+file.getName()); }else if(result == JFileChooser.CANCEL_OPTION){ this.label.setText("没有选择任何文件"); }else{ this.label.setText("操作出现错误"); } if(file != null){ try{ Scanner scan = new Scanner(new FileInputStream(file));//设置输入流 scan.useDelimiter("\n"); //设置换行为分隔符 while(scan.hasNext()){ //循环读取 this.area.append(scan.next()); //读取内容到文本区 this.area.append("\n"); //设置换行 } scan.close(); //关闭 }catch(Exception ex){ this.label.setText("文件读取出错"); } } } if(e.getSource() == this.save){ result = fileChooser.showSaveDialog(this.frame); //显示保存文件框 if(result == JFileChooser.APPROVE_OPTION){ file = fileChooser.getSelectedFile(); this.label.setText("存储的文件名称为:"+file.getName()); }else if(result == JFileChooser.CANCEL_OPTION){ this.label.setText("没有选择任何文件"); }else{ this.label.setText("操作出现错误"); } if(file != null){ try{ PrintStream out = new PrintStream(new FileOutputStream(file)); out.print(this.area.getText()); out.close(); }catch(Exception ex){ this.label.setText("文件保存出错"); } } } } } //主类 //Function : JFileChooser_demo public class JFileChooser_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 new Note(); } }
本文只发表于博客园和tonglin0325的博客,作者:tonglin0325,转载请注明原文链接:https://www.cnblogs.com/tonglin0325/p/5316446.html