5.22JAVA上机作业
作者:互联网
1 package com.wsy.work; 2 3 import java.awt.GridLayout; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 7 import javax.swing.JButton; 8 import javax.swing.JFrame; 9 import javax.swing.JPanel; 10 import javax.swing.JTextField; 11 12 public class Compute { 13 JFrame jf; 14 JPanel jp1; 15 JPanel jp2; 16 JButton[] jbs; 17 JTextField jtf; 18 public Compute() 19 { 20 jf = new JFrame("计算器"); 21 jp1 = new JPanel(); 22 jp2 = new JPanel(); 23 jf.setLayout(new GridLayout(2, 1)); 24 jp2.setLayout(new GridLayout(4, 4)); 25 jbs = new JButton[16]; 26 jtf = new JTextField(25); 27 jp1.add(jtf); 28 jf.add("North",jp1); 29 jbs[0] = new JButton("7"); 30 jbs[1] = new JButton("8"); 31 jbs[2] = new JButton("9"); 32 jbs[3] = new JButton("/"); 33 jbs[4] = new JButton("4"); 34 jbs[5] = new JButton("5"); 35 jbs[6] = new JButton("6"); 36 jbs[7] = new JButton("*"); 37 jbs[8] = new JButton("1"); 38 jbs[9] = new JButton("2"); 39 jbs[10] = new JButton("3"); 40 jbs[11] = new JButton("-"); 41 jbs[12] = new JButton("0"); 42 jbs[13] = new JButton("."); 43 jbs[14] = new JButton("="); 44 jbs[15] = new JButton("+"); 45 for(int i =0; i<jbs.length; i++) 46 { 47 jbs[i].addActionListener(new ActionListener(){ 48 49 @Override 50 public void actionPerformed(ActionEvent arg0) { 51 JButton jb = (JButton)arg0.getSource(); 52 String s = jb.getText(); 53 jtf.setText(s); 54 } 55 56 }); 57 jp2.add(jbs[i]); 58 } 59 jf.add("center", jp2); 60 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 61 jf.setSize(300, 200); 62 jf.setVisible(true); 63 } 64 65 public static void main(String[] args) { 66 new Compute(); 67 } 68 }
1 package com.wsy.work; 2 3 import java.awt.Color; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 7 import javax.swing.JButton; 8 import javax.swing.JFrame; 9 import javax.swing.JPanel; 10 11 public class ChangeColor { 12 JFrame jf; 13 JPanel[] jps; 14 JButton[] jbs; 15 public ChangeColor() 16 { 17 jf = new JFrame(); 18 jps = new JPanel[2]; 19 for(int i =0; i<jps.length; i++) 20 { 21 jps[i] = new JPanel(); 22 } 23 jbs = new JButton[3]; 24 jbs[0] = new JButton("红色"); 25 jbs[1] = new JButton("绿色"); 26 jbs[2] = new JButton("蓝色"); 27 for(int i =0; i<jbs.length; i++) 28 { 29 jps[0].add(jbs[i]); 30 } 31 jf.add("North", jps[0]); 32 jf.add("Center", jps[1]); 33 jbs[0].addActionListener(new ActionListener(){ 34 @Override 35 public void actionPerformed(ActionEvent e){ 36 jps[1].setBackground(Color.RED); 37 }}); 38 jbs[1].addActionListener(new ActionListener(){ 39 @Override 40 public void actionPerformed(ActionEvent e){ 41 jps[1].setBackground(Color.GREEN); 42 }}); 43 jbs[2].addActionListener(new ActionListener(){ 44 @Override 45 public void actionPerformed(ActionEvent e){ 46 jps[1].setBackground(Color.BLUE); 47 }}); 48 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 49 jf.setSize(300, 200); 50 jf.setVisible(true); 51 } 52 public static void main(String[] args) { 53 new ChangeColor(); 54 } 55 56 }
标签:jbs,上机,javax,JButton,JPanel,5.22,import,new,JAVA 来源: https://www.cnblogs.com/sucker/p/10906200.html