⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swingdemo.java

📁 Java程序设计大学教程程序源代码
💻 JAVA
字号:
/** * <p>Title:Swing组件演示程序 </p> * * <p>Copyright: Copyright (c) 2005</p> * * <p>Company: 刘艺</p> * * @author 刘艺 * @version 1.0 */package jbookch6;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.border.*;public class SwingDemo extends SimpleFrame {        /** Creates a new instance of SwingDemo */    public SwingDemo(int width,int height) {       //设置顶层容器         super(width,height);        setTitle("演示GUI容器、组件、事件及布局");                //JPanel容器采用缺省的FlowLayout布局        topPanel.add(nameLabel);        topPanel.add(nameText);        topPanel.add(new JLabel("学历"));        topPanel.add(comboBox);                //创建互斥按钮组        buttonGroup.add(rb1);        buttonGroup.add(rb2);        buttonGroup.add(rb3);                //设置边框        list.setBorder(BorderFactory.createTitledBorder("文化程度:"));        textArea.setBorder(BorderFactory.createTitledBorder("演示效果:"));                //设置背景色        textArea.setBackground(new Color(200,220,180));                //BOX容器采用缺省的BoxLayout布局        box.add(new JLabel("性别:"));        box.add(rb1);        box.add(rb2);        box.add(rb3);        box.add(checkBox);                //JFrame容器采用缺省的BorderLayout布局        Container c=this.getContentPane();        c.add(topPanel,"North");        c.add(list,"East");        c.add(box,"West");        c.add(button,"South");        c.add(new JScrollPane(textArea),"Center");                //演示JList组件选择列表项事件        list.addListSelectionListener(new ListSelectionListener(){            public void valueChanged(ListSelectionEvent e){                if (e.getValueIsAdjusting()) return;                textArea.append("\n"+(String)(list.getSelectedValue()));            }        });                //演示JCheckBox组件选择事件        checkBox.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent e){                String c="  未选";                if (checkBox.isSelected() )                    c="  已选";                textArea.append("\n"+(String)(checkBox.getText())+c);            }        });                //演示JComboBox组件选择列表项事件        comboBox.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent e){                textArea.append("\n"+(String)(comboBox.getSelectedItem()));            }        });                //演示JButton组件点击事件        button.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent e){                //演示消息框                JOptionPane.showMessageDialog(null,"演示消息框。",button.getText(),JOptionPane.INFORMATION_MESSAGE);            }        });                //演示JTextField组件文本改变事件        nameText.getDocument().addDocumentListener( new DocumentListener() {            public void insertUpdate(DocumentEvent e) { textArea.append("\n"+nameText.getText());}            public void removeUpdate(DocumentEvent e) {textArea.append("\n"+nameText.getText());}            public void changedUpdate(DocumentEvent e) {}        } );                //演示JRadioButton组件选择事件        rb1.addActionListener(rbListener);        rb2.addActionListener(rbListener);        rb3.addActionListener(rbListener);            }        //演示3个JRadioButton组件注册的一个共用监听器    private ActionListener  rbListener = new ActionListener(){        public void actionPerformed(ActionEvent e){            textArea.append("\n"+((JRadioButton)e.getSource()).getText());        }    };        public static void main(String args[]) {        try {            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");        } catch (Exception e){            System.err .println("程序异常:"+e.getMessage());        }        SwingDemo frame=new SwingDemo(400,300);        frame.setVisible(true);    }        private JPanel topPanel=new JPanel();    private JPanel listPanel=new JPanel();    private Box box=Box.createVerticalBox();    private JButton button=new JButton("OK");    private JCheckBox checkBox=new JCheckBox("少数民族");    private JRadioButton            rb1=new JRadioButton("男"),            rb2=new JRadioButton("女"),            rb3=new JRadioButton("不详",true);    private ButtonGroup buttonGroup=new ButtonGroup();    private JComboBox comboBox=new JComboBox(degrees);    private JList list=new JList(educationalBackground);    private JTextArea textArea=new JTextArea();    private JLabel nameLabel=new JLabel("姓名:");    private JTextField nameText=new JTextField("张三");        private static String[] degrees={"无","学士","硕士","工程硕士","MBA","博士"};    private static String[] educationalBackground={"小学","初中","高中","大学大专","大学本科","硕士研究生","博士研究生"};    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -