画笔
package com.dongfang.lesson03;
import java.awt.*;
import java.util.Date;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,600,400);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
//super.paint(g);
//画笔 需要有颜色 可以画画
g.setColor(Color.BLUE);
g.drawOval(100,100,100,100);//空心圆
g.fillOval(300,300,100,100);//实心圆
g.setColor(Color.RED);
g.fillRect(100,300,100,100);
//养成习惯 画笔用完 将它还原到最初的颜色
}
}
鼠标监听
目的:实现鼠标画画
-
鼠标监听事件----->存点的集合
-
鼠标监听事件----->画笔----->画板
package com.dongfang.lesson03;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
//鼠标监听事件
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("画图");
}
}
//自己的类
class MyFrame extends Frame{
//需要监听鼠标当前的位置 需要集合来存储这个点
ArrayList points;
public MyFrame(String title){
super(title);
setBounds(200,200,400,300);
//存鼠标点击的点
points = new ArrayList<>();
setVisible(true);
//鼠标监听器针对这个窗口
this.addMouseListener(new MyMouseListener());
}
@Override
public void paint(Graphics g) {
//画画需要监听鼠标的事件
Iterator iterator = points.iterator();
while(iterator.hasNext()){
Point point = (Point)iterator.next();
g.setColor(Color.CYAN);
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) {//e代表当前的鼠标
MyFrame frame = (MyFrame) e.getSource();
//点击的时候,就会在界面上产生一个点
//这个点就是鼠标的点
frame.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都要重新画一遍
frame.repaint();//刷新
}
}
}
窗口监听
package com.dongfang.lesson03;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setBackground(Color.blue);
setBounds(100,100,200,200);
setVisible(true);
//addWindowListener(new MyWindowListener());
this.addWindowListener(new WindowAdapter(){
//匿名内部类
//关闭窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("关闭");
//System.exit(0);
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
WindowFrame source = (WindowFrame) e.getSource();
source.setTitle("激活的窗口");
System.out.println("激活");
}
});
}
class MyWindowListener extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
setVisible(false);//隐藏窗口 通过按钮隐藏窗口
System.exit(0);//0正常退出 1非正常退出
}
}
}
键盘监听
package com.dongfang.lesson03;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter(){
//键盘按下
@Override
public void keyPressed(KeyEvent e) {
//获得键盘按下的键是哪一个 当前的码
int keyCode = e.getKeyCode();
System.out.println(keyCode);//按键的码 不需要去记录这个数值 直接使用静态属性VK_XX
if (keyCode == KeyEvent.VK_UP){
System.out.println("UP");
}
//根据按下的不同的键 产生不同的结果
}
});
}
}
Swing
窗口、面板
package com.dongfang.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo01 {
//init();初始化
public void init(){
//顶级窗口
JFrame jFrame = new JFrame("JFrame窗口");
jFrame.setVisible(true);
jFrame.setBounds(100,100,200,200);
jFrame.setBackground(Color.cyan);
//设置文字Jlabel
JLabel label = new JLabel("文字");
jFrame.add(label);
//容器实例化
//关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JFrameDemo01().init();
}
}
标签居中
package com.dongfang.lesson04;
import javax.swing.*;
import java.awt.*;
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJFrame2().init();
}
}
class MyJFrame2 extends JFrame{
public void init(){
this.setVisible(true);
this.setBounds(100,100,200,200);
//设置文字Jlabel
JLabel label = new JLabel("文字");
this.add(label);
//让文本标签居中
label.setHorizontalAlignment(SwingConstants.CENTER);//设置水平对齐
//获得一个容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.yellow);
}
}
弹窗
Dialog用来被弹出,默认就有关闭事件
package com.dongfang.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setVisible(true);
this.setSize(700,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西 容器
Container container = this.getContentPane();
//绝对布局
container.setLayout(null);
//按钮
JButton button = new JButton("点击弹出一个对话框");//创建
button.setBounds(30,30,200,50);
//点击这个按钮的时候,弹出一个弹窗
button.addActionListener(new ActionListener(){//监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
container.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo() {
this.setVisible(true);
this.setBounds(100,100,500,500);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
container.add(new Label("弹窗的窗口"));
}
}
标签:java,Day02,GUI,编程,void,import,new,100,public
From: https://www.cnblogs.com/dongfangyulv/p/18427912