1.按钮组件
package com.lzw; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CheckBoxTest extends JFrame{ /** * */ private static final long serialVersionUID = 1L; private JPanel panel1 = new JPanel(); private JPanel panel2 = new JPanel(); private JTextArea jt=new JTextArea(3,10); private JCheckBox jc1=new JCheckBox("1"); private JCheckBox jc2=new JCheckBox("2"); private JCheckBox jc3=new JCheckBox("3"); public CheckBoxTest(){ Container c=getContentPane(); setSize(200,160); setVisible(true); setTitle("复选框的使用"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); c.setLayout(new BorderLayout()); c.add(panel1, BorderLayout.NORTH); final JScrollPane scrollPane = new JScrollPane(jt); panel1.add(scrollPane); c.add(panel2, BorderLayout.SOUTH); panel2.add(jc1); jc1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(jc1.isSelected()) jt.append("复选框1被选中\n"); } }); panel2.add(jc2); jc2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(jc2.isSelected()) jt.append("复选框2被选中\n"); } }); panel2.add(jc3); jc3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(jc3.isSelected()) jt.append("复选框3被选中\n"); } }); } public static void main(String[] args) { new CheckBoxTest(); } }
package com.lzw; import java.awt.*; import java.awt.event.*; import java.net.*; import javax.swing.*; public class JButtonTest extends JFrame { /** * */ private static final long serialVersionUID = 1L; public JButtonTest() { URL url = JButtonTest.class.getResource("imageButtoo.jpg"); Icon icon = new ImageIcon(url); setLayout(new GridLayout(3, 2, 5, 5)); // 设置网格布局管理器 Container c = getContentPane(); // 创建容器 for (int i = 0; i < 5; i++) { // 创建按钮,同时设置按钮文字与图标 JButton J = new JButton("button" + i, icon); c.add(J); // 在容器中添加按钮 if (i % 2 == 0) { J.setEnabled(false); // 设置其中一些按钮不可用 } } JButton jb = new JButton(); // 实例化一个没有文字与图片的按钮 jb.setMaximumSize(new Dimension(90, 30)); // 设置按钮与图片相同大小 jb.setIcon(icon); // 为按钮设置图标 jb.setHideActionText(true); jb.setToolTipText("图片按钮"); // 设置按钮提示为文字 jb.setBorderPainted(false); // 设置按钮边界不显示 jb.addActionListener(new ActionListener() { // 为按钮添加监听事件 public void actionPerformed(ActionEvent e) { // 弹出确认对话框 JOptionPane.showMessageDialog(null, "弹出对话框"); } }); c.add(jb); // 将按钮添加到容器中 setTitle("创建带文字与图片的按钮"); setSize(350, 150); setVisible(true); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } public static void main(String args[]) { new JButtonTest(); } }
2.列表组件
1 package com.lzw; 2 3 import java.awt.*; 4 5 import javax.swing.*; 6 7 public class JComboBoxModelTest extends JFrame { 8 /** 9 * 10 */ 11 private static final long serialVersionUID = 1L; 12 JComboBox<String> jc = new JComboBox<>(new MyComboBox()); 13 JLabel jl = new JLabel("请选择证件:"); 14 15 public JComboBoxModelTest() { 16 setSize(new Dimension(160, 180)); 17 setVisible(true); 18 setTitle("在窗口中设置下拉列表框"); 19 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 20 Container cp = getContentPane(); 21 cp.setLayout(new FlowLayout()); 22 cp.add(jl); 23 cp.add(jc); 24 } 25 26 public static void main(String[] args) { 27 new JComboBoxModelTest(); 28 } 29 } 30 31 class MyComboBox extends AbstractListModel<String> implements ComboBoxModel<String> { 32 /** 33 * 34 */ 35 private static final long serialVersionUID = 1L; 36 String selecteditem = null; 37 String[] test = { "身份证", "军人证", "学生证", "工作证" }; 38 39 public String getElementAt(int index) { 40 return test[index]; 41 } 42 43 public int getSize() { 44 return test.length; 45 } 46 47 public void setSelectedItem(Object item) { 48 selecteditem = (String) item; 49 } 50 51 public Object getSelectedItem() { 52 return selecteditem; 53 } 54 55 public int getIndex() { 56 for (int i = 0; i < test.length; i++) { 57 if (test[i].equals(getSelectedItem())) 58 return i; 59 } 60 return 0; 61 } 62 }
package com.lzw; import java.awt.*; import javax.swing.*; public class JListTest extends JFrame { /** * */ private static final long serialVersionUID = 1L; public JListTest() { Container cp = getContentPane(); cp.setLayout(null); JList<String> jl = new JList<>(new MyListModel()); JScrollPane js = new JScrollPane(jl); js.setBounds(10, 10, 100, 100); cp.add(js); setTitle("在这个窗体中使用了列表框"); setSize(200, 150); setVisible(true); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } public static void main(String args[]) { new JListTest(); } } class MyListModel extends AbstractListModel<String> { /** * */ private static final long serialVersionUID = 1L; private String[] contents = { "列表1", "列表2", "列表3", "列表4", "列表5", "列表6" }; public String getElementAt(int x) { if (x < contents.length) return contents[x++]; else return null; } public int getSize() { return contents.length; } }
3.文本组件
package com.lzw; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class JTextFieldTest extends JFrame{ /** * */ private static final long serialVersionUID = 1L; public JTextFieldTest(){ setSize(250,100); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); Container cp=getContentPane(); getContentPane().setLayout(new FlowLayout()); final JTextField jt=new JTextField("aaa",20); final JButton jb=new JButton("清除"); cp.add(jt); cp.add(jb); jt.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { // TODO 自动生成方法存根 jt.setText("触发事件"); } }); jb.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { jt.setText(""); jt.requestFocus(); } }); setVisible(true); } public static void main(String[] args) { new JTextFieldTest(); } }
package com.lzw; import java.awt.*; import javax.swing.*; public class JTextAreaTest extends JFrame{ /** * */ private static final long serialVersionUID = 1L; public JTextAreaTest(){ setSize(200,100); setTitle("定义自动换行的文本域"); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); Container cp=getContentPane(); JTextArea jt=new JTextArea("文本域",6,6); jt.setLineWrap(true);//可以自动换行 cp.add(jt); setVisible(true); } public static void main(String[] args) { new JTextAreaTest(); } }