1. 首页
  2. IT资讯

Java初学者的小实验,有源码哦

“u003Cdivu003Eu003Cpu003Eu003Cspanu003E题目: u003Cu002Fspanu003Eu003Cu002Fpu003Eu003Cpu003Eu003Cspanu003E 编写一个程序,创建一个AWT面板,该面板包含三个不同颜色名称的三个按钮,单击每个按钮时应使窗口的背景颜色显示为相应的颜色。程序运行的结果如下: u003Cu002Fspanu003Eu003Cu002Fpu003Eu003Cpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002F288c00040e5a47c755c2″ img_width=”539″ img_height=”370″ alt=”Java初学者的小实验,有源码哦” inline=”0″u003Eu003Cu002Fpu003Eu003Cp class=”pgc-img-caption”u003E图一u003Cbru003Eu003Cu002Fpu003Eu003Cpu003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002F288b000323ca0a5b5c1f” img_width=”447″ img_height=”392″ alt=”Java初学者的小实验,有源码哦” inline=”0″u003Eu003Cu002Fpu003Eu003Cp class=”pgc-img-caption”u003E图二u003Cu002Fpu003Eu003Cpu003Eu003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002F26f300028b46515e85a0″ img_width=”450″ img_height=”452″ alt=”Java初学者的小实验,有源码哦” inline=”0″u003Eu003Cu002Fpu003Eu003Cp class=”pgc-img-caption”u003E图三u003Cu002Fpu003Eu003Cpu003Eu003Cimg src=”http:u002Fu002Fp3.pstatp.comu002Flargeu002F288d0000bbc51e4ae229″ img_width=”448″ img_height=”450″ alt=”Java初学者的小实验,有源码哦” inline=”0″u003Eu003Cu002Fpu003Eu003Cp class=”pgc-img-caption”u003E图四u003Cbru003Eu003Cu002Fpu003Eu003Cpu003E源程序如下: u003Cu002Fpu003Eu003Cpu003Eimport java.awt.*; u003Cu002Fpu003Eu003Cpu003Eimport java.awt.event.*; u003Cu002Fpu003Eu003Cpu003Eimport javax.swing.JButton; u003Cu002Fpu003Eu003Cpu003Eclass ChangeBgColor extends Frame implements ActionListener { u003Cu002Fpu003Eu003Cpu003Eprivate static final long serialVersionUID = 1L; Panel p; u003Cu002Fpu003Eu003Cpu003EJButton btn1,btn2,btn3; ChangeBgColor() { u003Cu002Fpu003Eu003Cpu003E p = new Panel(); btn1 = new JButton(“红色”); u003Cu002Fpu003Eu003Cpu003E btn1.setBackground(Color.red ); btn2 = new JButton(“蓝色”); u003Cu002Fpu003Eu003Cpu003Ebtn2.setBackground(Color.blue); u003Cu002Fpu003Eu003Cpu003E btn3 = new JButton(“黄色”); u003Cu002Fpu003Eu003Cpu003E btn3.setBackground(Color.yellow); this.add(p); p.add(btn1); p.add(btn2); p.add(btn3); u003Cu002Fpu003Eu003Cpu003E btn1.addActionListener(this); btn2.addActionListener(this); btn3.addActionListener(this); } u003Cu002Fpu003Eu003Cpu003Epublic static void main(String args[]) { u003Cu002Fpu003Eu003Cpu003E ChangeBgColor mainFrame = new ChangeBgColor(); mainFrame.setTitle(“基本GUI 编程”); mainFrame.setSize(400, 400); u003Cu002Fpu003Eu003Cpu003EmainFrame.setLocation(100, 100); u003Cu002Fpu003Eu003Cpu003E mainFrame.setBackground(Color.black); mainFrame.setVisible(true); u003Cu002Fpu003Eu003Cpu003E mainFrame.addWindowListener(new MyClosing()); } u003Cu002Fpu003Eu003Cpu003Epublic void actionPerformed(ActionEvent e) { u003Cu002Fpu003Eu003Cpu003E if (e.getSource() == btn1) { u003Cu002Fpu003Eu003Cpu003Ep.setBackground(Color.red); } u003Cu002Fpu003Eu003Cpu003E else if(e.getSource() == btn2) { u003Cu002Fpu003Eu003Cpu003E p.setBackground(Color.blue); } else u003Cu002Fpu003Eu003Cpu003E p.setBackground(Color.yellow); } } u003Cu002Fpu003Eu003Cpu003Eclass MyClosing implements WindowListener { u003Cu002Fpu003Eu003Cpu003E public void windowActivated(WindowEvent e) { } u003Cu002Fpu003Eu003Cpu003E public void windowClosed(WindowEvent e) { } u003Cu002Fpu003Eu003Cpu003E public void windowClosing(WindowEvent e) { u003Cu002Fpu003Eu003Cpu003E System.exit(0); } u003Cu002Fpu003Eu003Cpu003E public void windowDeactivated(WindowEvent e) { } u003Cu002Fpu003Eu003Cpu003E public void windowDeiconified(WindowEvent e) { } u003Cu002Fpu003Eu003Cpu003E public void windowIconified(WindowEvent e) { } u003Cu002Fpu003Eu003Cpu003E public void windowOpened(WindowEvent e) { } }u003Cu002Fpu003Eu003Cpu003E想要更多的企业求职加分项目案例,关注Java学习交流群285154486,每天会讲解分享项目,答疑解惑。u003Cu002Fpu003Eu003Cu002Fdivu003E”

原文始发于:Java初学者的小实验,有源码哦

主题测试文章,只做测试使用。发布者:熱鬧獨處,转转请注明出处:http://www.cxybcw.com/26620.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code