5.24Java网格布局和多个事件监听
作者:互联网
import java.awt.*; import javax.swing.*; public class comput { JFrame f; JPanel p; JTextField t; JButton b[]; GridLayout g; String a[]= {"7","8","9","/", "4","5","6","*", "1","2","3","-", "0",".","=","+"}; public comput(){ f = new JFrame(); p = new JPanel(); b = new JButton[16]; t = new JTextField(10); g = new GridLayout(4,4); p.setLayout(g); for(int i=0;i<16;i++){ b[i] = new JButton (a[i]); p.add(b[i]); } f.setSize(400, 300);; f.add(p); f.setTitle("计算器"); f.setVisible(true); f.add(t,BorderLayout.NORTH); f.add(p,BorderLayout.CENTER); } public static void main(String[] args) { new comput(); } }
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class color1 { JFrame f; JPanel p; JButton b1,b2,b3; public color1() { f = new JFrame(); p = new JPanel(); b1 = new JButton("粉红色"); b2 = new JButton("橘色"); b3 = new JButton("浅绿色"); b1.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e){ p.setBackground(Color.pink);}}); b2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub p.setBackground(Color.orange);}}); b3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub p.setBackground(Color.green); }}); f.setSize(400,400); f.add(p); p.add(b1); p.add(b2); p.add(b3); f.setVisible(true); } public static void main(String[] args) { new color1(); } }
标签:Java,ActionListener,网格,JButton,5.24,b2,import,new,public 来源: https://www.cnblogs.com/huangjiaxin/p/10917802.html