[Java GUI] Swing中JList和JRadioButton的联合使用

[Java GUI] Swing中JList和JRadioButton的联合使用_第1张图片

此例主要演示了JList和JRadioButton、ButtonGroup类的使用,用到的技巧主要是嵌套布局及匿名类。

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class SelectButtonDemo {
	public static void main( String[] args ) {
		new MyFrame( "Selection Button Demo" );
	}
}

class MyFrame extends JFrame {

	private static final long serialVersionUID = 3961792940094823675L;

	private JRadioButton[] btnSports = null;
	private ButtonGroup btnGroup = null;
	private JTextArea txtInfo = null;
	
	JList<String> cityList = null;
	String sportType;
	
	private static final String [] SPORTS = { "Football", "Basketball", "Running", "Bicycle" };
	private static final String [] CITY = { "Beijing", "Shanghai", "Guangzhou", "Xi\'an", "Tianjin", "Hangzhou" };
	
	MyFrame( String title ) {
		super( title );
		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		
		cityList = new JList<String>( CITY );
		JScrollPane cityPane = new JScrollPane( cityList );
		cityList.addListSelectionListener( new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				// do nothing
			}			
		});
		
		cityList.addMouseListener( new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JList<?> src = null;
				if( e.getClickCount() == 2 ) {
					if( e.getSource() instanceof JList<?> ) {
						src = (JList<?>) e.getSource();
						txtInfo.setText( sportType + " : " + src.getSelectedIndex() + " : " + src.getSelectedValue() + "\n" );
					}
				}
			}			
		});
		
		JPanel btnPanel = new JPanel( new GridLayout( 1, 4 ) );
		
		// initialize the button group
		btnSports = new JRadioButton[4];
		btnGroup = new ButtonGroup();
		for( int i = 0; i < 4; ++i ) {
			btnSports[i] = new JRadioButton( SPORTS[i] );
			btnSports[i].addItemListener( new ItemListener() {
				@Override
				public void itemStateChanged(ItemEvent e) {
					JRadioButton src = (JRadioButton)e.getItemSelectable();
					txtInfo.setText( src.getText() );
					sportType = src.getText();
				}				
			});
			btnGroup.add( btnSports[i] );
			btnPanel.add( btnSports[i] );
		}
		
		txtInfo = new JTextArea( 10, 5 );
		txtInfo.setLineWrap( true );
	
		JScrollPane sp = new JScrollPane( txtInfo );
		
		Container cp = getContentPane();
		cp.add( btnPanel, BorderLayout.NORTH );
		cp.add( sp, BorderLayout.CENTER );
		
		cp.add( cityPane, BorderLayout.WEST );
		
		setBounds( 400, 200, 300, 200 );
		setVisible( true );
		pack();
	}
}


你可能感兴趣的:(java,String,Class,import,button)