可以看成是一个空间,但是不能单独存在,内嵌在Frame里面
//这里解决了关闭事件
public class testGUI {
//Panel可以看成是一个空间,但不能单独存在,需要嵌套在Frame里面
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(153, 34, 19));
//panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(Color.green);
//frame.add(panle)
frame.add(panel);//将面板加入frame里面
frame.setVisible(true);
//监听事件,监听窗口关闭,System.exit(0)
//适配器模式,这样就只要重写一种方法了
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P2NoRbEg-1649143567142)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220331165602254.png)]
public class testGUI {
//Panel可以看成是一个空间,但不能单独存在,需要嵌套在Frame里面
public static void main(String[] args) {
Frame frame = new Frame();
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//设置流式布局
frame.setLayout(new FlowLayout());//默认为中间
// frame.setLayout(new FlowLayout(FlowLayout.LEFT));//从左边开始,一排满了就下一排
frame.setSize(400,400);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dydHXRyJ-1649143567145)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220401161958641.png)]
public class testGUI {
//Panel可以看成是一个空间,但不能单独存在,需要嵌套在Frame里面
public static void main(String[] args) {
Frame frame = new Frame();
Button west = new Button("west");
Button east = new Button("east");
Button north = new Button("north");
Button south = new Button("south");
Button center = new Button("center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kGunlnwR-1649143567147)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220401165934743.png)]
public class testGUI {
//Panel可以看成是一个空间,但不能单独存在,需要嵌套在Frame里面
public static void main(String[] args) {
Frame frame = new Frame();
Button bnt1 = new Button("bnt1");
Button bnt2 = new Button("bnt2");
Button bnt3 = new Button("bnt3");
Button bnt4 = new Button("bnt4");
Button bnt5 = new Button("bnt5");
Button bnt6 = new Button("bnt6");
frame.setLayout(new GridLayout(2,3));//两行三列的报个布局
frame.add(bnt1);
frame.add(bnt2);
frame.add(bnt3);
frame.add(bnt4);
frame.add(bnt5);
frame.add(bnt6);
frame.pack();//Java函数,用于自动布局,自动写大小等
frame.setVisible(true);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8LLWQWvm-1649143567149)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220401170750673.png)]
:
public class testGUI {
//Panel可以看成是一个空间,但不能单独存在,需要嵌套在Frame里面
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setSize(400,300);
frame.setLocation(500,500);
frame.setLayout(new GridLayout(2,1));
frame.setBackground(Color.black);
//四个面版
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2,2));
p1.add(new Button("east1"),BorderLayout.EAST);
p1.add(new Button("west1"),BorderLayout.WEST);
p2.add(new Button("b1"));
p2.add(new Button("b2"));
p1.add(p2,BorderLayout.CENTER);
p3.add(new Button("east2"),BorderLayout.EAST);
p3.add(new Button("west2"),BorderLayout.WEST);
p4.add(new Button("1"));
p4.add(new Button("2"));
p4.add(new Button("3"));
p4.add(new Button("4"));
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oAR1s6tF-1649143567151)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220401202456249.png)]
事件监听:当某个事情发生的时候,干什么?
public class testGUI {
public static void main(String[] args) {
//按下按钮,触发一些事件
Frame frame = new Frame();
Button button = new Button();
//因为addActionListener()需要一个AddActionListener,所以我们需要构造一个AddActionListener
myactionListener mal = new myactionListener();
button.addActionListener(mal);
frame.add(button,BorderLayout.CENTER);
frame.setVisible(true);
frame.pack();
myClos(frame);
}
public static void myClos(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
//事件监听
class myactionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaaaa");
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xMrYWddB-1649143567152)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220401204546556.png)]
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.BatchUpdateException;
public class testGUI {
public static void main(String[] args) {
//写一个开始和结束的button
Frame frame = new Frame("start-end");
Button b1 = new Button("start");
Button b2 = new Button("end");
//可以显示地定义触发时会返回的命令信息,如果不显示定义,则会走默认值
//可以多个按钮只写一个监听类
//setActionCommand()显示设置按钮信息
b2.setActionCommand("b2-is-click");
myListener ml = new myListener();
b1.addActionListener(ml);
b2.addActionListener(ml);
frame.setLayout(new GridLayout(2,1));
frame.add(b1);
frame.add(b2);
frame.pack();
frame.setVisible(true);
}
}
//事件监听
class myListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//getActionCommand()获取按钮信息
System.out.println("it is cliscked "+e.getActionCommand());
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bXEwp9zJ-1649143567154)(C:\Users\lu’hai’peng\AppData\Roaming\Typora\typora-user-images\image-20220401205632879.png)]
public class testGUI {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textfield = new TextField();
add(textfield);//在frame中添加textfiled
//监听这个文本框输入的文字
MyActionLisener myActionLisener = new MyActionLisener();
//按下enter,就会触发这个输入框的事件
textfield.addActionListener(myActionLisener);
//设置替换编码,比如输入密码显示的是*
textfield.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionLisener implements ActionListener{
@Override
public void actionPerfor