JButton的一个例子
2011-05-25 10:08 Rollen Holt 阅读(4988) 评论(0) 编辑 收藏 举报/** * 这个程序利用GridLayout来实现一个界面,然后当按每一个按钮的时候,按钮的颜色发生变化 */ /** * @author Rollen Holt * @version 25/5/2011 */ import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Gridlayout extends JFrame implements ActionListener{ JButton buttonBlue; JButton buttonRed; JButton buttonWhite; JButton buttonGreen; JButton buttonYellow; JButton buttonBlack; JButton buttonOrange; JButton buttonPurple; JButton buttonPink; JButton buttonLightBlue; Panel panel; Gridlayout(){ setTitle("GridLayout"); setSize(500,500); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buttonBlue=new JButton("Blue"); buttonRed=new JButton("Red"); buttonWhite=new JButton("White"); buttonGreen=new JButton("Green"); buttonYellow=new JButton("Yellow"); buttonBlack=new JButton("Black"); buttonOrange=new JButton("Orange"); buttonPurple=new JButton("Purple"); buttonPink=new JButton("Pink"); buttonLightBlue=new JButton("LightBlue"); panel=new Panel(); panel.setLayout(new GridLayout(5,2)); panel.add(buttonBlue); panel.add(buttonRed); panel.add(buttonWhite); panel.add(buttonBlack); panel.add(buttonGreen); panel.add(buttonYellow); panel.add(buttonOrange); panel.add(buttonLightBlue); panel.add(buttonPink); panel.add(buttonPurple); add(panel); buttonBlue.addActionListener(this); buttonRed.addActionListener(this); buttonLightBlue.addActionListener(this); buttonWhite.addActionListener(this); buttonPink.addActionListener(this); buttonGreen.addActionListener(this); buttonOrange.addActionListener(this); buttonPurple.addActionListener(this); buttonYellow.addActionListener(this); buttonBlack.addActionListener(this); } public void actionPerformed(ActionEvent event){ if(event.getSource()==buttonBlue){ buttonBlue.setBackground(Color.BLUE); } if(event.getSource()==buttonBlack){ buttonBlack.setBackground(Color.BLACK); } if(event.getSource()==buttonRed){ buttonRed.setBackground(Color.RED); } if(event.getSource()==buttonPink){ buttonPink.setBackground(Color.PINK); } if(event.getSource()==buttonOrange){ buttonOrange.setBackground(Color.ORANGE); } if(event.getSource()==buttonYellow){ buttonYellow.setBackground(Color.YELLOW); } if(event.getSource()==buttonPurple){ buttonPurple.setBackground(new Color(128,0,255)); } if(event.getSource()==buttonLightBlue){ buttonLightBlue.setBackground(Color.CYAN); } if(event.getSource()==buttonWhite){ buttonWhite.setBackground(Color.WHITE); } if(event.getSource()==buttonGreen){ buttonGreen.setBackground(Color.GREEN); } } public static void main(String[] args){ new Gridlayout(); } }
==============================================================================
本博客已经废弃,不在维护。新博客地址:http://wenchao.ren
我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
实我是一个程序员
==============================================================================