标签:int text 绿色 dac round frame awt white panel
今天第一题的实现计算机的那个,其实一开始是真的没啥思路的,布局的分类应用也不是很熟。后面老师演示之后才有了思路,然后再看发现真的理解起来不难,先将基础的弄好,再一步步来就好做了。然后就是第二个,我觉得我实现的应该没有符合老师的要求,但是我不知道怎么去改动了。我做的就一个面板,三个按钮然后分别实现它们的功能,然后老师作业上的好像是两个面板。emmmm,我暂时只做得到这里了。
1 package aaa; 2 import java.awt.BorderLayout; 3 import java.awt.Color; 4 import java.awt.GridLayout; 5 import javax.swing.*; 6 public class Computer { 7 JFrame f; 8 JPanel p; 9 JLabel l; 10 JTextField t; 11 JButton b[]; 12 GridLayout gl; 13 String title[]= {"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"}; 14 public Computer() { 15 f=new JFrame(); 16 t=new JTextField(); 17 p=new JPanel(); 18 l=new JLabel(); 19 p.setBackground(Color.white ); 20 gl=new GridLayout(4,4); 21 p.setLayout(gl); 22 b=new JButton[title.length]; 23 for(int i=0;i<title.length;i++) { 24 b[i]=new JButton(title[i]); 25 p.add(b[i]); 26 } 27 f.add(l); 28 f.add(t,BorderLayout.NORTH); 29 f.add(p,BorderLayout.CENTER); 30 f.add(p); 31 f.setSize(500,400); 32 f.setVisible(true); 33 } 34 public static void main(String[] args){ 35 new Computer(); 36 37 } 38 }
package aaa; import java.awt.Color; import java.awt.event.*; import javax.swing.*; public class Frame { JFrame f; JPanel p,p1; JButton b1,b2,b3; public Frame() { f=new JFrame(); p=new JPanel(); b1=new JButton("红色"); b1.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { JPanel p1=new JPanel(); p.setSize(300, 200); p.setBackground(Color.RED); f.add(p1); } } ); b2=new JButton("绿色"); b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JPanel p2=new JPanel(); p.setSize(300, 200); p.setBackground(Color.GREEN); f.add(p2); } }); b3=new JButton("蓝色"); b3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JPanel p3=new JPanel(); p.setSize(300, 200); p.setBackground(Color.blue); f.add(p3); } }); p.setBackground(Color.WHITE); p.add(b1); p.add(b2); p.add(b3); f.add(p); f.setSize(300, 200); f.setVisible(true); } public static void main(String[] args) { new Frame(); } }
标签:int text 绿色 dac round frame awt white panel
原文地址:https://www.cnblogs.com/rfvh/p/10908360.html