홍익대 Java/수업
홍대 자바 수업 : 리스트형(스크롤형) 버튼 텍스트 필드 컬러와 배경 색 바꾸기 .
Yi Junho
2009. 7. 29. 17:15
반응형
import javax.swing.*; import javax.swing.event.*; import java.util.*; public class ListTest extends JFrame { private JTextField text; private JList colorList; Random ra =new Random(System.currentTimeMillis()); private Container container; private final String colorNames[] = { "Black", "Blue", "Cyan", "Dark Gray", "Gray", "Green", "Light Gray", "Magenta", "Orange", "Pink", "Red", "White", "Yellow" }; private final Color colors[] = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW }; public ListTest() { super( "Color Test" ); container = getContentPane(); container.setLayout( new FlowLayout() ); colorList = new JList( colorNames ); colorList.setVisibleRowCount( 5 ); //보이는 버튼수 colorList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );//하나만 선택 container.add( new JScrollPane( colorList ) ); text=new JTextField(10); container.add(text); text.setFont( new Font( "굴림체", Font.PLAIN,38) );//폰트 체 등등 . colorList.addListSelectionListener( new ListSelectionListener() { // anonymous inner class public void valueChanged( ListSelectionEvent event ) { int a =ra.nextInt(13);//난수 text.setText(colorNames[colorList.getSelectedIndex()]); container.setBackground(colors [ colorList.getSelectedIndex() ]);//colorList.getSelectedIndex().toString(); text.setBackground( colors[ a ] ); if (a>=12) { text.setForeground( colors[ a-6 ] ); } else { text.setForeground( colors[ a+1 ] ); } } } // end anonymous inner class ); // end call to addListSelectionListener setSize( 350, 150 ); setVisible( true ); } // end ListTest constructor public static void main( String args[] ) { ListTest application = new ListTest(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } } // end class ListTest //리스트 목록 뽑아 오기 for (int i=0; iSystem.out.println(colorList.getModel().getElementAt(i).toString()); }
반응형