Java中的GUI组件和布局管理器、事件处理(改)

1、实现如图所示效果:

 

 代码如下:

package cn1;

import java.awt.FlowLayout;
import java.awt.TextArea;
import java.awt.TextField;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Class1 {

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
			  createAndshowGUI();
			}
		});
	}

	private static void createAndshowGUI() {
		// TODO Auto-generated method stub
		JFrame f=new JFrame("常用组件实例");
		f.setLayout(new FlowLayout(FlowLayout.CENTER));
		f.setSize(320,320);
		f.setLocationRelativeTo(null);
		f.setResizable(false);
		JLabel la7=new JLabel("文本框:");
		JTextField t1=new JTextField(10);
		JLabel la1=new JLabel("按钮:");
		JButton bn=new JButton("确认");
		JLabel la2=new JLabel("选择框:");
		JCheckBox yinyue=new JCheckBox("喜欢音乐");
		JCheckBox lvyou=new JCheckBox("喜欢旅游");
		JCheckBox tiyu=new JCheckBox("喜欢体育");
		JLabel la3=new JLabel("单选按钮:");
		ButtonGroup group=new ButtonGroup();
		JRadioButton man=new JRadioButton("男");
		JRadioButton woman=new JRadioButton("女");
		group.add(man);
		group.add(woman);
		JLabel la4=new JLabel("下拉列表:");
		JComboBox<String> combox=new JComboBox<>();
		combox.addItem("美女图库");
		combox.addItem("美女图库");
		combox.addItem("美女图库");
		combox.addItem("美女图库");
		combox.addItem("美女图库");
		JLabel la5=new JLabel("文本框:");
		JTextArea t=new JTextArea(8,20);
		JLabel la6=new JLabel("密码框:");
		JTextField t2=new JTextField(10);
		f.add(la7);
		f.add(t1);
		f.add(la1);
		f.add(bn);f.add(la2);f.add(yinyue);
		f.add(lvyou);f.add(tiyu);f.add(la3);
		f.add(man);f.add(woman);f.add(la4);
		f.add(combox);f.add(la5);f.add(t);
		f.add(la6);f.add(t2);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

 2、实现下图的效果:

 

 代码如下:

package cn2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Input {
    public static String s;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(()->createAndShowGUI());
    }

    private static void createAndShowGUI() {
        JFrame f=new JFrame("事件处理方式1示例");
        f.setLayout(new BorderLayout());
        f.setSize(500,400);
//        f.setResizable(false);
        f.setLocationRelativeTo(null);
        JTextArea t1=new JTextArea(16,38);
        JTextField t2=new JTextField(20);
        JButton b=new JButton("数据转移");
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String a=t2.getText();
                s+=a+"\n";
                t1.setText(s);
                t2.setText("");
            }
        });
        JPanel p=new JPanel();
        JPanel p1=new JPanel();
        p.add(t2);p.add(b);
        p1.add(t1);
        f.add(p1,BorderLayout.CENTER);f.add(p,BorderLayout.PAGE_END);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

 3.实现下图效果:

 

 

 

 

 代码如下:

package cn3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Mouse {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(()->createAndShowGUI());
    }

    private static void createAndShowGUI() {
        JFrame f=new JFrame("适配器设计模式示例2");
        f.setLayout(new BorderLayout());
        f.setSize(500,600);
        f.setLocationRelativeTo(null);
        JButton b1=new JButton("红色");
        JButton b2=new JButton("绿色");
        JButton b3=new JButton("蓝色");
//        f.setBackground(Color.yellow);
        JPanel p=new JPanel(new FlowLayout());
        p.setSize(500,600);
        b1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                super.mouseEntered(e);
                p.setBackground(Color.red);
            }
        });
        b2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                super.mouseEntered(e);
                p.setBackground(Color.green);
            }
        });
        b3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                super.mouseEntered(e);
                p.setBackground(Color.blue);
            }
        });
        p.add(b1);p.add(b2);p.add(b3);
//        f.setResizable(false);
        f.add(p);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

 4、实现下图效果:

 

代码如下:

package cn4;

import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Guess {
public static int a;
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				createAndshowGUI();
			}
		});
	}

	protected static void createAndshowGUI() {
		// TODO Auto-generated method
		JFrame f=new JFrame();
		JPanel p=new JPanel(new GridLayout(4,1));
		f.setBounds(520, 520, 250, 200);
		f.setLayout(new FlowLayout(FlowLayout.CENTER));
		JButton bn1 = new JButton("获取一个随机数");
		bn1.setHorizontalAlignment(SwingUtilities.CENTER);
		f.add(bn1);
		bn1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				a = (int) Math.floor(Math.random()*100+1);
				bn1.setEnabled(false);
			}
		});
		JLabel la=new JLabel("输入您猜的数(1~100)");
		p.add(la);
		la.setHorizontalAlignment(SwingConstants.CENTER);
		la.setOpaque(true);
		la.setBackground(Color.green);
		JTextField text = new JTextField(10);

		text.setHorizontalAlignment(SwingConstants.CENTER);
		p.add(text);


		JButton bn=new JButton("确认");


		p.add(bn);
		bn.setSize(5,5);
		bn.setEnabled(false);
		bn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int b= Integer.parseInt(text.getText());
				if (a>b){
					la.setText("对不起,您猜小了!");
				}else if(a==b){
					la.setText("恭喜恭喜,您猜中了");
				}else {
					la.setText("对不起,您猜大了");
				}
			}
		});

//		getKeyChar():  char           返回这个事件中和键相关的字符
//		getKeyCode():  int             返回这个事件中和键相关的整数键

//		keyPressed(e: KeyEvent)         在源组件上按下一个键后被调用
//		KeyReleased(e: KeyEvent)       在源组件上释放一个键后被调用
//		KeyTyped(e: KeyEvent)           在源组件上按下一个键然后释放该键后被调用
		text.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				super.keyPressed(e);
				bn.setEnabled(true);
			}
	});
		f.add(p);
		f.setVisible(true);
		f.setResizable(false);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
}

 5、实现下图效果:

 

代码如下:

package cn5;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Register {
    public static  String user="woziji";
    public static  String password="123";
    public static void main(String[] args) {
        SwingUtilities.invokeLater(()->createAndShowGUI());
    }

    private static void createAndShowGUI() {
        JFrame f=new JFrame("登录");
        f.setLayout(new FlowLayout());
        JPanel p=new JPanel(new GridLayout(2,2));
        f.setSize(290,150);
        f.setLocationRelativeTo(null);
        JLabel l1=new JLabel("用户名:");
        JTextField t1=new JTextField(10);
        JLabel l2=new JLabel("密码");
        JTextField t2=new JTextField(10);
        JButton b1=new JButton("登录");
        JButton b2=new JButton("退出");
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String a=t1.getText();
                String b=t2.getText();
                if (a.equals(user)&&b.equals(password)){
                    JOptionPane.showMessageDialog(null,"欢迎登陆");
                }else{
                    JOptionPane.showMessageDialog(null,"用户名或密码错误!");
                }

            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,"欢迎下次再来!");
                System.exit(0);
            }
        });
        p.add(l1);p.add(t1);
        p.add(l2);p.add(t2);
        f.add(p);
        f.add(b1);
        f.add(b2);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

*适配器类的作用

实现接口类的同时,省去不用的空方法

                        

*Java语言中,AWT与SWing的区别与联系是什么?

AWT是一种重量级组件,使用较为麻烦,美观功能有限

Swing是一种轻量级组件,由Java语言开发,底层以AWT为基础,使跨平台应用可以使用任何可插拔的外观风格

        

*简述如何实现GUI中的事件监听机制。

首先创建事件源,之后根据要监听的事件源创建指定类型监听器进行事件处理,监听器是一个特殊的Java类,必须实现XxxListenter接口用于监听事件,然后通过addXxxListenter()方法为指定事件添加特定类型监听器。当事件源上发生监听的事件后,就会触发绑定的事件监听器,然后由监听器中的方法进行相应处理。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

篆愁君的烦恼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值