10.画板下的鼠标事件,键盘事件,窗口事件
1.键盘事件:KeyListener 2.鼠标事件:MouseListener 3.窗口WindowListener
package KuangStudy; import java.awt.*; public class Test { public static void main(String[] args) { Frame frame=new Frame("傻子"); frame.setSize(700,500); Student student=new Student(); frame.setBackground(Color.CYAN); frame.add(student); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
package KuangStudy; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class Student extends Panel { public Student (){ this.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { System.out.println("您点击了"+e.getX()+","+e.getY()); } }); } public void paint(Graphics g){ g.fillOval(200,200,100,100); Color c=new Color(255,5,5); g.setColor(c); } }
11.利用画板操作模拟画板
基本思路: 1.如果想要多个数据同时存储在画板上,那么我们就需要使用数组的操作 进行储存位置。 2.ArrayList : 进行集合的储存操作 3.MouseListener 鼠标事件
JavaBean
package KuangStudy; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; public class Student extends Panel { ArrayList<Integer> arrayList_x=new ArrayList(); ArrayList <Integer>arrayList_y=new ArrayList(); int x; int y; public Student (){ this.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { x=e.getX(); y=e.getY(); arrayList_x.add(x); arrayList_y.add(y); System.out.println("您点击了"+e.getX()+","+e.getY()); repaint(); } }); } public void paint(Graphics g){ Color c=new Color(255,5,5); g.setColor(c); for (int i = 0; i <arrayList_x.size(); i++) { g.fillOval(arrayList_x.get(i),arrayList_y.get(i),10,10); } } }
Test
package KuangStudy; import java.awt.*; import java.awt.event.ActionListener; import java.util.ArrayList; public class Test { public static void main(String[] args) { Frame frame=new Frame("傻子"); frame.setSize(700,500); Student student=new Student(); frame.setBackground(Color.CYAN); frame.add(student); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
(二)、SWING
1.JFrame(高阶画板)
基本思路: 1.窗体关闭 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 2.把标签的位置放到中间 label_one.setHorizontalAlignment(SwingConstants.CENTER);
package KuangStudy; import javax.swing.*; public class first extends JFrame { public first(){ this.setSize(300,300); this.setLocationRelativeTo(null); this.setTitle("JFrame"); this.setVisible(true); JLabel label_one=new JLabel("欢迎来到我的世界"); this.add(label_one); //把标签的位置放到中间 label_one.setHorizontalAlignment(SwingConstants.CENTER); //关闭事件的操作 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } }
package KuangStudy; public class Test { public static void main(String[] args) { first first_one=new first(); } }
2.JDialog(对话框)
基本思路: 1.弹窗 JDialog 也是一个窗口,也需要设置相应的大小,宽高,展现 (有默认的关闭事件,不用再次添加) JDialog jDialog_one=new JDialog(); 2.按钮: 按钮也能进行位置和大小的设置 button_dialog.setBounds(30,30,50,50);
package KuangStudy; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class first extends JFrame { public first(){ this.setSize(300,300); this.setLocationRelativeTo(null); this.setTitle("JFrame"); this.setVisible(true); //把其添加到窗体中 JLabel label_one=new JLabel("欢迎来到我的世界ya"); this.add(label_one); //把文本放到中间的位置 label_one.setHorizontalAlignment(SwingConstants.CENTER); //关闭事件的操作 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); JButton button_dialog=new JButton("点我弹窗"); //为按钮设置长和宽以及位置 button_dialog.setBounds(30,30,50,50); this.add(button_dialog); //添加按钮事件的适配器 button_dialog.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JDialog jDialog_one=new JDialog(); jDialog_one.setBounds(100,100,100,100); jDialog_one.add(label_one); jDialog_one.setVisible(true); //JOptionPane.showMessageDialog(null,"yes"); } }); } }
package KuangStudy; public class Test { public static void main(String[] args) { first first_one=new first(); } }
3.ICON(标签_图标)
基本思路: 1. Icon 是一个接口 2. 具体实现:需要在第一个方法中,填写腰围和什么图形 3. 声明图标以及水平线的位置. 4. JLabel jLabel_one=new JLabel("是我呀",first_one(实现接口的对象),SwingConstants.CENTER); 5. 这里没有定义x,y的具体位置
package KuangStudy; import javax.swing.*; import java.awt.*; public class first extends JFrame implements Icon { private int width; private int height; public first(){ } public first(int width,int height){ this.width=width; this.height=height; } //运用方法对其进行初始化的操作 public void init(){ //设置宽度 first first_one=new first(50,50); //设置图片标签 JLabel jLabel_one=new JLabel("是我呀",first_one,SwingConstants.CENTER); this.add(jLabel_one); } //进行绘画 @Override public void paintIcon(Component c, Graphics g, int x, int y) { g.fillOval(x,y,width,height); } //返回宽度 @Override public int getIconWidth() { return this.width; } //返回高度 @Override public int getIconHeight() { return this.height; } }
package KuangStudy; public class Test { public static void main(String[] args) { first first_one=new first(); first_one.init(); first_one.setSize(500,500); first_one.setLocationRelativeTo(null); first_one.setVisible(true); } }