窗口实训1
计算器
package shixun8;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.Scanner;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.Scanner;
import javax.swing.*;
public class MyFrame {
JFrame jf;
JPanel jp;
JButton[] b;
String a[]={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};
JTextField jt1;
GridLayout c;
public MyFrame() {
jf=new JFrame("");
jp=new JPanel();
b=new JButton[a.length];
jp.setLayout(null);
jt1=new JTextField();
c=new GridLayout(4,4,5,5);
jt1.setBounds(10,20,30,50);
jf.setLocation(600,400);
jf.setSize(300,300);
jf.add(jp,BorderLayout.CENTER);
jf.add(jt1,BorderLayout.NORTH);
jp.setLayout(c);
for(int i=0;i<16;i++){
b[i]=new JButton(a[i]);
jp.add(b[i]);
}
jf.setVisible(true); }
public static void main(String[] args) {
new MyFrame();
public class MyFrame {
JFrame jf;
JPanel jp;
JButton[] b;
String a[]={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};
JTextField jt1;
GridLayout c;
public MyFrame() {
jf=new JFrame("");
jp=new JPanel();
b=new JButton[a.length];
jp.setLayout(null);
jt1=new JTextField();
c=new GridLayout(4,4,5,5);
jt1.setBounds(10,20,30,50);
jf.setLocation(600,400);
jf.setSize(300,300);
jf.add(jp,BorderLayout.CENTER);
jf.add(jt1,BorderLayout.NORTH);
jp.setLayout(c);
for(int i=0;i<16;i++){
b[i]=new JButton(a[i]);
jp.add(b[i]);
}
jf.setVisible(true); }
public static void main(String[] args) {
new MyFrame();
}
}
运行结果:
![](https://img2018.cnblogs.com/blog/1632032/201905/1632032-20190529104656479-110845054.png)
颜色变换
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MyFrame1 implements ActionListener {
JFrame jf;
JPanel jp,jp2;
JButton b1,b2,b3;
public MyFrame1(){
jf=new JFrame("窗口");
jp=new JPanel();
jp2=new JPanel();
b1=new JButton("红色");
b2=new JButton("蓝色");
b3=new JButton("绿色");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
jf.setLayout(new BorderLayout());
jf.add(jp,BorderLayout.NORTH);
jf.add(jp2,BorderLayout.CENTER);
jp.add(b1);
jp.add(b2);
jp.add(b3);
jp2.setBackground(Color.PINK);
jf.setVisible(true);
jf.setSize(500, 400);
jf.setLocation(500,500);
}
public static void main(String[]args) {
new MyFrame1();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==b1)
jp2.setBackground(Color.red);
if(e.getSource()==b2)
jp2.setBackground(Color.blue);
if(e.getSource()==b3)
jp2.setBackground(Color.green);
}}
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MyFrame1 implements ActionListener {
JFrame jf;
JPanel jp,jp2;
JButton b1,b2,b3;
public MyFrame1(){
jf=new JFrame("窗口");
jp=new JPanel();
jp2=new JPanel();
b1=new JButton("红色");
b2=new JButton("蓝色");
b3=new JButton("绿色");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
jf.setLayout(new BorderLayout());
jf.add(jp,BorderLayout.NORTH);
jf.add(jp2,BorderLayout.CENTER);
jp.add(b1);
jp.add(b2);
jp.add(b3);
jp2.setBackground(Color.PINK);
jf.setVisible(true);
jf.setSize(500, 400);
jf.setLocation(500,500);
}
public static void main(String[]args) {
new MyFrame1();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==b1)
jp2.setBackground(Color.red);
if(e.getSource()==b2)
jp2.setBackground(Color.blue);
if(e.getSource()==b3)
jp2.setBackground(Color.green);
}}
运行结果
![](https://img2018.cnblogs.com/blog/1632032/201905/1632032-20190529104713940-1239013123.png)
![](https://img2018.cnblogs.com/blog/1632032/201905/1632032-20190529104727872-190922746.png)