如何在窗口中操作?首先了解窗口有哪些组件.
组件:
窗口 、弹窗、面板、按钮、图片、鼠标、文本框、列表框、监听事件、键盘事件、破解工具
1.窗口Frame是父类窗口
2.布局
.... 流式布局 对按钮(Button)的布局 .setLayout(new FlowLayout(FlowLayout.xxxx));
.... 东南西北中 对按钮(Button)的布局 .add(xxx,BorderLayout.xxx);
.... 表格 对按钮(Button)的布局 .setLayout(new GridLayout());
public class ExDemo1 {
public static void main(String[] args) {
/**
* 组件构成: 窗口、弹窗、面板、文本框、列表框、按钮、图片、监听事件、鼠标、键盘事件、破解工具、
*/
//1.窗口及标题
Frame frame = new Frame("开心");
//设置窗口的大小
frame.setSize(500, 500);
//窗口的初始位置 x、y分别以左上角为原点的坐标
frame.setLocation(200, 200);
//窗口的初始位置,大小还可以通过setBounds()方法完成
//frame.setBounds(100,100,400,400);
//设置窗口的背景 Color.blue
frame.setBackground(new Color(249, 124, 78));
//窗口的大小不可变
frame.setResizable(false);
//窗口的可见性
frame.setVisible(true);
//关闭窗口
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
Panel 无法单独显示,必须添加到某个容器中
public class ExDemo3 {
public static void main(String[] args) {
/**
* 2.面板
*/
Frame frame = new Frame("快乐");
//设置布局
frame.setLayout(null);
//窗口的大小
frame.setBounds(200,200,400,400);
//窗口的背景
frame.setBackground(Color.green);
//窗口的可见性
frame.setVisible(true);
//创建面板的对象
Panel panel = new Panel();
//面板的坐标相对于窗口设置的
panel.setBounds(50,50,100,100);
//面板的背景
panel.setBackground(Color.blue);
//将面板添加到窗口 面板不能单独存在一个空间里
frame.add(panel);// Panel extends Container implements Accessible Container extends Component
//窗口关闭
//监听事件:监听窗口关闭事件
//适配器模式:
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
流式布局
public class ExDemo4 {
public static void main(String[] args) {
/**
* 流式布局
*/
Frame frame = new Frame("布局");
//窗口的大小
frame.setBounds(100,100,400,400);
//窗口的背景
frame.setBackground(Color.green);
//窗口的可见
frame.setVisible(true);
//组件-按钮
Button button1 = new Button("button1");
Button button2 = new Button("button2");
//设置为流式布局
frame.setLayout(new FlowLayout(FlowLayout.LEFT));//布局左边
frame.setLayout(new FlowLayout());//没有参数默认为中间
frame.setLayout(new FlowLayout(FlowLayout.CENTER));//布局中间
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//布局右边
frame.setLocation(new Point(20,20));
//添加到窗口中
frame.add(button1);// Button extends Component
frame.add(button2);
//关闭窗口 监听窗口事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//关闭程序
System.exit(0);
}
});
}
}
布局: 东西南北中
public class ExDemo5 {
public static void main(String[] args) {
/**
* 东西南北中 布局
*/
Frame frame = new Frame("东西南北中");
//窗口的大小
frame.setBounds(100,100,400,400);
//窗口的可见性
frame.setVisible(true);
//组件-按钮
Button east = new Button("east");
Button west = new Button("west");
Button south = new Button("south");
Button north = new Button("north");
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.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
表格布局
public class ExDemo6 {
public static void main(String[] args) {
/**
* 表格布局
*/
Frame frame = new Frame("表格布局");
//窗口的大小
frame.setBounds(100,100,400,400);
//窗口的可见性
frame.setVisible(true);
//组件-按钮
Button b1 = new Button("b1");
Button b2 = new Button("b2");
Button b3 = new Button("b3");
Button b4 = new Button("b4");
Button b5 = new Button("b5");
Button b6 = new Button("b6");
//表格布局
frame.setLayout(new GridLayout(3,2));
//往窗口中添加按钮
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.add(b4);
frame.add(b5);
frame.add(b6);
//关闭窗口
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
画笔
public class TestPaint {
public static void main(String[] args) {
/**
* 画笔 需要在超类里面找Paint(Graphics g)方法
*/
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame {
@Override
public void paint(Graphics g) {
//画笔的颜色
// g.setColor(Color.black);
//画的一个圆形
g.fillOval(100, 100, 100, 100);
g.draw3DRect(100, 100, 200, 200, true);//画一个3D矩形
g.setColor(Color.blue);
g.fillRect(50, 50, 300, 300);
//用完画笔记得还原本身的颜色
}
public void loadFrame() {
//窗口的大小
setBounds(100, 100, 400, 400);
//窗口的可见
setVisible(true);
//关闭窗口
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
事件监听
public class ExDemo8 {
public static void main(String[] args) {
/**
* 事件监听
*/
Frame frame = new Frame("事件监听");
//窗口的大小
frame.setBounds(100, 100, 400, 400);
//窗口的可见
frame.setVisible(true);
Button button = new Button();
//因为,addActionListener()需要一个 ActionListener,所以我们需要构造一个 ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button, BorderLayout.CENTER);
frame.pack();//Java函数
windowClose(frame); //关闭窗口
}
//关闭窗体的事件
private static void windowClose(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("你好");
}
}
TextField 监听
public class TestText01 {
public static void main(String[] args) {
/**
* 输入框 TextField 监听
*/
MyFrame myFrame = new MyFrame();
}
}
class MyFrame extends Frame {
public MyFrame() {
//窗口的可见
setVisible(true);
//Java函数
pack();
TextField textField = new TextField();
//窗口中添加文本框
add(textField);
//监听这个文本框输入的文字
MyActionListener2 myActionListener2 = new MyActionListener2();
//按下Enter 就会触发这个输入框事件
textField.addActionListener(myActionListener2);
//设置替换编码
textField.setEchoChar('*');
//关闭程序
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
}
class MyActionListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//获得一些资源
TextField textField = (TextField) e.getSource();
//输出输入框的文本
System.out.println(textField.getText());
//录入你输入的内容 并且清空输入框文本的上一次内容
textField.setText("");//这边既可以是null 也可以是空字符("") 源码text = (t != null) ? t : "";
}
}
鼠标监听
public static void main(String[] args) {
/**
* 鼠标监听
*/
new MyMouse("画图");
}
}
class MyMouse extends Frame {
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
ArrayList points;
public MyMouse(String title) {
super(title);
//存储鼠标点
points = new ArrayList<>();
//窗口的大小
setBounds(200, 200, 600, 500);
//窗口的可见
setVisible(true);
//鼠标监听器
this.addMouseListener(new MyMouseListener());
//关闭窗口
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
//画画监听鼠标的的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()) {
Point point = (Point) iterator.next();
g.setColor(Color.BLUE);
g.fillOval(point.x, point.y, 10, 10);
}
}
//添加一个点到界面上
public void addPaint(Point point) {
points.add(point);
}
private class MyMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
//鼠标每点击一下就会产生一个点
MyMouse myMouse = (MyMouse) e.getSource();
//这个点就是鼠标的点
myMouse.addPaint(new Point(e.getX(), e.getY()));
//每点击鼠标就重新画一个圆
myMouse.repaint();
}
}
窗口监听
public class TestWindow {
public static void main(String[] args) {
/**
* 窗口监听
*/
new WindowFrame();
}
}
class WindowFrame extends Frame {
public WindowFrame() {
//设置窗口的大小
setBounds(100, 100, 200, 200);
//窗口的背景
setBackground(Color.green);
//窗口的可见
setVisible(true);
//关闭程序
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
this.addWindowListener(
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("被激活了");
System.out.println("windowActivated");
}
}
);
}
}
键盘监听
public class TestKeyListener {
public static void main(String[] args) {
/**
* 键盘事件
*/
new KeyFrame();
}
}
class KeyFrame extends Frame {
public KeyFrame() {
//窗口的大小
setBounds(100, 100, 200, 200);
//窗口的可见
setVisible(true);
//关闭窗口
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//结束程序
System.exit(0);
}
});
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
//输出键盘的码值
System.out.println(keyCode);
//判断键盘的件是否是上键
if (keyCode == KeyEvent.VK_UP) {
System.out.println("你按下了上键");
}
}
});
}
}