BlogJava 联系 聚合 管理  

Blog Stats

News

 

蓝冰飞寒个人独立博客地址,www.uphenan.com

随笔档案

文章档案


蓝冰飞寒

用心去做每一件事情

     课本的实例程序,我照着抄的,还是很多错误啊,第一次抄完有三十多个错误,然后开始改正,改了一个多小时吧,才弄好,刚刚运行成功呵呵。等会再写个小程序,是课后题,下午我上自习的时候在本上写下了那个程序,等会调试一下。呵呵,希望大体是正确的。
  
/*
示例程序:SwingDemo.java

*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;

public class SwingDemo extends SimpleFrame{
     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.setLayout(new BorderLayout());
        c.add(topPanel,"North");
        c.add(button,"South");
        c.add(box,"West");
        c.add(list,"East");
        
        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组件
                         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("男");
               private JRadioButton    rb2 = new JRadioButton("女");
                private 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= {"小学","初中","高中","大学大专","大学本科","硕士生研究生","博士研究生"};
              }
Tags - , ,
文章来源:http://www.tt-shopping.com/kevinlau/read.php/119.htm
posted on 2009-05-09 21:59 蓝冰飞寒 阅读(136) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: