1、设计一个如图所示的界面,不需要提供组件的功能。

package Java;
import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.*;
public class 计算器 {
   JFrame f;
   JPanel p;
   JTextField t;
   GridLayout g;
   JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
   public 计算器(){
	   JFrame f=new JFrame("计算器");
	   JPanel p=new JPanel();
	   GridLayout g = new GridLayout(4,4);
	   JTextField t=new JTextField(50);
	   JButton b1 = new JButton("7");
	   JButton b2 = new JButton("8");
	   JButton b3 = new JButton("9");
	   JButton b4 = new JButton("/");
	   JButton b5 = new JButton("4");
	   JButton b6 = new JButton("5");
	   JButton b7 = new JButton("6");
	   JButton b8 = new JButton("*");
	   JButton b9 = new JButton("1");
	   JButton b10 = new JButton("2");
	   JButton b11 = new JButton("3");
	   JButton b12 = new JButton("-");
	   JButton b13 = new JButton("0");
	   JButton b14 = new JButton(".");
	   JButton b15 = new JButton("=");
	   JButton b16 = new JButton("+");
	   p.setLayout(g);
	   f.add(t,BorderLayout.NORTH);
	   f.add(p,BorderLayout.CENTER);
	   p.add(b1);
	   p.add(b2);
	   p.add(b3);
	   p.add(b4);
	   p.add(b5);
	   p.add(b6);
	   p.add(b7);
	   p.add(b8);
	   p.add(b9);
	   p.add(b10);
	   p.add(b11);
	   p.add(b12);
	   p.add(b13);
	   p.add(b14);
	   p.add(b15);
	   p.add(b16);
	   f.setVisible(true);
	   f.setSize(400,400);
   }
   public static void main(String[] args) {
		// TODO Auto-generated method stub
        new 计算器();	}

}

2、编写可改变背景颜色的窗口。

package Java;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyFrame {
	   JFrame f;
	   JPanel p1;
	static JPanel p2;
	static JButton b1;
	static JButton b2;
	static JButton b3;
	   public MyFrame(){
		   f  = new JFrame();
		   p1 = new JPanel();
		   p2 = new JPanel();
		   b1 = new JButton("粉色");
		   b2 = new JButton("蓝色");
		   b3 = new JButton("黄色");
		   f.add(p1,BorderLayout.NORTH);
		   p1.add(b1);
		   p1.add(b2);
		   p1.add(b3);
		   f.add(p2,BorderLayout.CENTER);
		   p1.setBackground(Color.white);
		   f.setSize(400,400);
		   f.setVisible(true);
	   }
		public static void main(String[] args) {
	            new MyFrame();
	b1.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent e) {
			p2.setBackground(Color.pink);
			
	    }
   });
	b2.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
            p2.setBackground(Color.blue);
		}		
   });
	b3.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {		
			p2.setBackground(Color.yellow);
		}
	});
  }
}

3、对本次作业进行总结,在编程过程中遇到哪些问题,如何解决,有哪些收获?

动手实践以后,对窗口的运用相较以往更加熟悉,而且运用了新学的知识,温故知新,两者都兼顾了,布局这个操作流程还有待多实践,当窗口、布局与事件处理都放在一起的时候就比较麻烦,有些不知所措,这次做的都是日常中常见的小插件,以前没觉得它怎么样,自己动手的时候才感叹人类的智慧,做出来的时候成就的喜悦冲淡了实践过程中的一些浮躁。最大的问题可能就是事件处理还是有些复杂,第二题的按钮改变颜色,一个会做,当数量多的时候就有些慌乱,一个按钮加一个事件,只能一步一步慢慢的用愚笨一点的方法来解决问题。

posted on 2019-05-22 22:21  怀稚  阅读(106)  评论(0编辑  收藏  举报