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

📄 swingdemo.java

📁 设置一个简单的图形用户交互界面
💻 JAVA
字号:
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 <code>SwingDemo</code>.
   
    
    public SwingDemo(int width,int height) 
    {
    	super(width,height);
    	setTitle("演示GUI容器,组件,事件及布局");
    
    
    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.add(new JLabel("性别:"));
    box.add(rb1);
    box.add(rb2);	
    box.add(rb3);	
    box.add(checkBox);
    
    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");
    
    list.addListSelectionListener(new ListSelectionListener(){
    	public void valueChanged(ListSelectionEvent e){
    		if(e.getValueIsAdjusting()  ) return;
    		textArea.append("\n"+(String)(list.getSelectedValue()  )   );
    	}
    } );
    
    
    checkBox.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent e){
    		String c="  未选";
    		if(checkBox.isSelected() )
    			c="  已选";
    		textArea.append("\n"+(String)(checkBox.getText() )+c);
    	}
    	} );
    	
    	comboBox.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			textArea.append("\n"+
    				         (String)(comboBox.getSelectedItem() ) );
    			
    		}
    	} );
    	
    	
    	button.addActionListener ( new ActionListener() {
    		public void actionPerformed(ActionEvent e){
    			JOptionPane.showMessageDialog(null,
    			           "演示消息框。",button.getText(),
    			           JOptionPane.INFORMATION_MESSAGE  );
    		}
    	});
    	
    	
    	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) {} 
    		} );  
    			
    			
    	rb1.addActionListener(rbListener);	
    	rb2.addActionListener(rbListener);	
    	rb3.addActionListener(rbListener);	
    		
}

        private ActionListener rbListener=new ActionListener(){
        	public void actionPerformed(ActionEvent e){
        		textArea.append("\n"+
        			( (JRadioButton)e.getSource() ).getText() );
        	}
        };
        
        			
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        // TODO code application logic here
        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 + -