GUI编程
2.Awt
介绍:
1.包含了很多的类和接口;GUI:图形用户编程
2.元素:窗口,按钮,文本框
3.java.awt
4.组件
2.2
创建java图形界面对象
Frame frame = new Frame;
设置 可视化
frame.setVisible(true);
设置窗口大小
frame.setSize;
设置背景
wode.setBackground();
设置弹窗位置
frame.setLocation();
设置是否固定大小
frame.setResizable(true);
还可以将上面的方法进行封装利用构造函数对Frame进行集成重写有参构造
class MyFrame extends Frame{
public MyFrame(int x,int y,int w,int h,Color color){
super("Myframe+"+(++id));
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
}
2.Panel
注:可以看成一个空间,但是不能单独存在
Panel是相对于Frame的在frame中的
Panel panel = new Panel;
panel.setBounds(200,100,100,100);//panel设置坐标,相对于frame
panel.setBackgroud(1,1,1);
frame.add(panel);//将panel加到frame上
2.3.布局管理器
流式布局
/*int right = FlowLayout.RIGHT;
FlowLayout flowLayout = new FlowLayout(right);
frame.setLayout(flowLayout);*/
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
边界布局——东西南北中
frame.add(new Button,BorderLayout.EAST);//将新建的按钮放在frame的右边
frame.add(new Button,BorderLayout.WEST);
frame.add(new Button,BorderLayout.SOUTH);
frame.add(new Button,BorderLayout.NORTH);
frame.add(new Button,BorderLayout.CENTER);
表格布局
frame.setLayout(new GridLayout(3,2));//将frame设置为3行2列布局
2.4.事件监听
按下按钮触发事件
Button button = new Button();
//因为,addActionListener()需要一个 ActionListener,所以我们需要构造一个 ActionListener
MyActionListener myActionListener = new MyActionListener();
button.addActionListener(myActionListener);
class MyActionListener implements ActionListener{//对这个接口的实现
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
或者
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("sa");
}
});
2.5、输入框 TextField 监听
查看例子
public static void main(String[] args){
Frame frame = new Frame();
TextField textField = new TextField();
frame.add(textField);
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();
System.out.println(field.getText()); //获得输入框的文本
}
});
textField.setEchoChar('*');
frame.setVisible(true);
textField.setVisible(true);
}
5.画笔
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics g) {
//画笔,需要有颜色,画笔可以画画
//g.setColor(Color.red);
//g.drawOval(100,100,100,100);
g.fillOval(100,100,100,100); //实心的园
// g.setColor(Color.GREEN);
g.fillRect(150,200,200,200);
//养成习惯,画笔用完,将他还原到最初的颜色
}
}
2.8鼠标监听
目的:想要实现鼠标画画
示例
class TestMouseListener {
public static void main(String[] args) {
new MyFrame();
}
}
//自己的类
class MyFrame extends Frame{
//画画需要画笔,需要监听鼠标当前的位置,需要集合来存储这个点
ArrayList points;
public void paint(Graphics g) {
//画画,监听鼠标的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();
g.fillOval(point.x,point.y,10,10);
}
}
public MyFrame() {
setBounds(200,200,400,300);
//存鼠标点击的点
points = new ArrayList<>();
setVisible(true);
//鼠标监听器,正对这个窗口
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame = (MyFrame) e.getSource();
//这个我们点击的时候,就会在界面上产生一个点!画
//这个点就是鼠标的点;
points.add(new Point(e.getX(),e.getY()));
//每次点击鼠标都需要重新画一遍
frame.repaint();//刷新
}
});
}
}
2.9窗口监听
frame.addWindowFocusListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
});
2.10键盘监听
示例
vpublic static void main(String[] args) {
Frame frame = new Frame();
frame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode(); //不需需要去记录这个数值,直接使用静态属性 VK_XXX
System.out.println(keyCode);
if (keyCode == KeyEvent.VK_UP) {
System.out.println("你按下了上键");
}
//根据按下不同操作,产生不同结果;
}
});
}