9.javase_GUI(swing)

news/2024/10/21 7:49:28/

一.GUI
(1)GUI是:用图形的方式,来显示计算机操作的界面。
(2)javax.swing包
在awt的基础上,建立的一套图形界面系统,提供了更多的组件,而且完全由Java实现。增强了移植性,属轻量级控件
组件:是具有图形表示的对象,该图形表示可以显示在屏幕上并且可以与用户交互
(3)常用 GUI 组件
GUI组件
3.1 基本组件(JButton,JLabel,JTextField,JTextArea)
3.2容器组件( Window<–Frame<–JFrame || Panel<–JPanel)
(4)JFrame
4.1 JFrame概述
是一个顶层窗口
构造方法:
JFrame()构造一个最初不可见的新窗体
成员方法
void setVisible?(boolean b):显示或隐藏此窗体具体取决于参数b的值
void setSize?(int width, int height):调整此组件的大小,使其宽度为width,高度为height,单位是像素
void setTitle(String title):设置窗体标题
void setLocationRelativeTo?(Component c):设置位置,值为null,则窗体位于屏幕中央
void setDefaultCloseOperation?(int operation):设置窗体关闭时默认操作整数3表示:窗口关闭时退出应用程序
void setAlwaysOnTop(boolean alwaysOnTop):设置此窗口是否应始终位于其他窗口之上
4.2 JFrame简单案例

 public class JFrameDemo01 {public static void main(String[] args) {//1.构造一个最初不可见的新窗体JFrame jf = new JFrame();//2.设置窗体标题jf.setTitle("百度一下,你就知道");//3.设置窗体大小jf.setSize(400, 300);//4.设置窗体关闭时默认操作(整数3表示:窗口关闭时退出应用程序)jf.setDefaultCloseOperation(3);//5.设置位置,值为null,则窗体位于屏幕中央jf.setLocationRelativeTo(null);//6.设置此窗口是否应始终位于其他窗口之上jf.setAlwaysOnTop(true);//7.显示或隐藏此窗体具体取决于参数b的值jf.setVisible(true);}}

(5)JButton
5.1 JButton概述
按钮的实现
构造方法
JButton?(String text):创建一个带文本的按钮
成员方法
void setSize?(int width, int height):设置大小
void setLocation?(int x, int y):设置位置(x坐标,y坐标)
void setBounds?(int x, int y, int width, int height):设置位置和大小
和窗体相关操作
取消窗体默认布局:窗体对象.setLayout(null);
把按钮添加到窗体:窗体对象.add(按钮对象);
5.2 JButton简单案例

public class JButtonDemo {public static void main(String[] args) {//1.创建窗体对象JFrame jf = new JFrame();jf.setTitle("窗口中添加按钮");jf.setSize(400, 300);jf.setDefaultCloseOperation(3);jf.setLocationRelativeTo(null);jf.setAlwaysOnTop(true);//2.取消窗体的默认布局jf.setLayout(null); //3.创建一个带文本的按钮JButton btn = new JButton("我是按钮");//4.设置大小和设置位置(x坐标,y坐标)btn.setBounds(100,100,100,20);//5.把按钮添加到窗体jf.add(btn);//6.设置窗体可见jf.setVisible(true);}}

(6)JLabel
6.1 JLabel概述
短文本字符串或图像的显示区域
构造方法
JLabel(String text):使用指定的文本创建 JLabel实例
JLabel(Icon image):使用指定的图像创建 JLabel实例
ImageIcon?(String filename):从指定的文件创建ImageIcon【文件路径:绝对路径和相对路径】
绝对路径:完整的路径名【D:\IdeaProjects\javase_code\itheima-api-gui\images\mn.png】
相对路径:必须使用取自其他路径名的信息进行解释【itheima-api-gui\images\mn.png】
成员方法
void setBounds?(int x, int y, int width, int height):设置位置和大小
6.2 JLabel简单案例

public class JLabelDemo {public static void main(String[] args) {//1.创建窗体对象JFrame jf = new JFrame();jf.setTitle("显示文本和图像");jf.setSize(400, 300);jf.setDefaultCloseOperation(3);jf.setLocationRelativeTo(null);jf.setAlwaysOnTop(true);jf.setLayout(null);//2.使用指定的文本创建 JLabel实例JLabel jLabel = new JLabel("好好学习");jLabel.setBounds(0,0,100,20);//3.使用指定的图像创建 JLabel实例,从指定的文件创建ImageIconJLabel jLabel2 = new JLabel(new ImageIcon("itheima-api-gui\\images\\mn.png"));jLabel2.setBounds(50,50,100,143);//4.把文本添加到窗体jf.add(jLabel);jf.add(jLabel2);//5.设置窗体可见jf.setVisible(true);}}

(7)用户登录界面
/用户登录/

public class UserLogin {public static void main(String[] args) {//1.创建窗体对象JFrame jf = new JFrame();jf.setTitle("用户登录");jf.setSize(400, 300);jf.setDefaultCloseOperation(3);jf.setLocationRelativeTo(null);jf.setAlwaysOnTop(true);jf.setLayout(null);//2.显示用户名文本JLabel usernameLable = new JLabel("用户名");usernameLable.setBounds(50,50,50,20);jf.add(usernameLable);//3.用户名输入框JTextField usernameField = new JTextField();usernameField.setBounds(150,50,180,20);jf.add(usernameField);//4.显示密码文本JLabel passwordLable = new JLabel("密码");passwordLable.setBounds(50,100,50,20);jf.add(passwordLable);//5.密码输入框JPasswordField passwordField = new JPasswordField();passwordField.setBounds(150,100,180,20);jf.add(passwordField);//6.登录按钮JButton loginButton = new JButton("登录");loginButton.setBounds(50,200,280,20);jf.add(loginButton);//7.设置窗体可见jf.setVisible(true);}}

(8)聊天室界面

 public class ChatRoom {public static void main(String[] args) {//1.创建窗体对象JFrame jf = new JFrame();jf.setTitle("聊天室");jf.setSize(400, 300);jf.setDefaultCloseOperation(3);jf.setLocationRelativeTo(null);jf.setAlwaysOnTop(true);jf.setLayout(null);//2.显示聊天信息的文本域JTextArea messageArea = new JTextArea();messageArea.setBounds(10,10,360,200);jf.add(messageArea);//3.输入聊天信息的文本框JTextField messageField = new JTextField();messageField.setBounds(10,230,180,20);jf.add(messageField);//4.发送按钮JButton sendButton = new JButton("发送");sendButton.setBounds(200,230,70,20);jf.add(sendButton);//5.清空聊天按钮JButton clearButton = new JButton("清空聊天");clearButton.setBounds(280,230,100,20);jf.add(clearButton);//6.设置窗体可见jf.setVisible(true);}}

(9)考勤查询并使用日历控件

日历控件类:

import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.*;/*** 日期选择器,可以指定日期的显示格式*/
public class DateChooser extends JPanel {private static final long serialVersionUID = 4529266044762990227L;private Date initDate;private Calendar now = Calendar.getInstance();private Calendar select;private JPanel monthPanel;// 月历private JP1 jp1;// 四块面板,组成private JP2 jp2;private JP3 jp3;private JP4 jp4;private Font font = new Font("宋体", Font.PLAIN, 12);private final LabelManager lm = new LabelManager();private SimpleDateFormat sdf;private boolean isShow = false;private Popup pop;private JComponent showDate;public static DateChooser getInstance() {return new DateChooser();}public static DateChooser getInstance(Date date) {return new DateChooser(date);}public static DateChooser getInstance(String format) {return new DateChooser(format);}public static DateChooser getInstance(Date date, String format) {return new DateChooser(date, format);}/*** Creates a new instance of DateChooser*/private DateChooser() {this(new Date());}private DateChooser(Date date) {this(date, "yyyy年MM月dd日");}private DateChooser(String format) {this(new Date(), format);}private DateChooser(Date date, String format) {initDate = date;sdf = new SimpleDateFormat(format);select = Calendar.getInstance();select.setTime(initDate);initPanel();}/*** 是否允许用户选择*/public void setEnabled(boolean b) {super.setEnabled(b);showDate.setEnabled(b);}/*** 得到当前选择框的日期*/public Date getDate() {return select.getTime();}public String getStrDate() {return sdf.format(select.getTime());}public String getStrDate(String format) {sdf = new SimpleDateFormat(format);return sdf.format(select.getTime());}// 根据初始化的日期,初始化面板private void initPanel() {monthPanel = new JPanel(new BorderLayout());monthPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));JPanel up = new JPanel(new BorderLayout());up.add(jp1 = new JP1(), BorderLayout.NORTH);up.add(jp2 = new JP2(), BorderLayout.CENTER);monthPanel.add(jp3 = new JP3(), BorderLayout.CENTER);monthPanel.add(up, BorderLayout.NORTH);monthPanel.add(jp4 = new JP4(), BorderLayout.SOUTH);this.addAncestorListener(new AncestorListener() {public void ancestorAdded(AncestorEvent event) {}public void ancestorRemoved(AncestorEvent event) {}// 只要祖先组件一移动,马上就让popup消失public void ancestorMoved(AncestorEvent event) {hidePanel();}});}public void register(final JComponent showDate) {this.showDate = showDate;showDate.setRequestFocusEnabled(true);showDate.addMouseListener(new MouseAdapter() {public void mousePressed(MouseEvent me) {showDate.requestFocusInWindow();}});this.setBackground(Color.WHITE);this.add(showDate, BorderLayout.CENTER);this.setPreferredSize(new Dimension(90, 25));this.setBorder(BorderFactory.createLineBorder(Color.GRAY));showDate.addMouseListener(new MouseAdapter() {public void mouseEntered(MouseEvent me) {if (showDate.isEnabled()) {showDate.setCursor(new Cursor(Cursor.HAND_CURSOR));showDate.setForeground(Color.RED);}}public void mouseExited(MouseEvent me) {if (showDate.isEnabled()) {showDate.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));showDate.setForeground(Color.BLACK);}}public void mousePressed(MouseEvent me) {if (showDate.isEnabled()) {showDate.setForeground(Color.CYAN);if (isShow) {hidePanel();} else {showPanel(showDate);}}}public void mouseReleased(MouseEvent me) {if (showDate.isEnabled()) {showDate.setForeground(Color.BLACK);}}});showDate.addFocusListener(new FocusListener() {public void focusLost(FocusEvent e) {hidePanel();}public void focusGained(FocusEvent e) {}});}// 根据新的日期刷新private void refresh() {jp1.updateDate();jp2.updateDate();jp3.updateDate();jp4.updateDate();SwingUtilities.updateComponentTreeUI(this);}// 提交日期private void commit() {// TODO add other components hereif (showDate instanceof JTextField) {((JTextField) showDate).setText(sdf.format(select.getTime()));} else if (showDate instanceof JLabel) {((JLabel) showDate).setText(sdf.format(select.getTime()));}hidePanel();}// 隐藏日期选择面板private void hidePanel() {if (pop != null) {isShow = false;pop.hide();pop = null;}}// 显示日期选择面板private void showPanel(Component owner) {if (pop != null) {pop.hide();}Point show = new Point(0, showDate.getHeight());SwingUtilities.convertPointToScreen(show, showDate);Dimension size = Toolkit.getDefaultToolkit().getScreenSize();int x = show.x;int y = show.y;if (x < 0) {x = 0;}if (x > size.width - 295) {x = size.width - 295;}if (y < size.height - 170) {} else {y -= 188;}pop = PopupFactory.getSharedInstance().getPopup(owner, monthPanel, x, y);pop.show();isShow = true;}/*** 最上面的面板用来显示月份的增减*/private class JP1 extends JPanel {private static final long serialVersionUID = -5638853772805561174L;JLabel yearleft, yearright, monthleft, monthright, center,centercontainer;public JP1() {super(new BorderLayout());this.setBackground(new Color(160, 185, 215));initJP1();}private void initJP1() {yearleft = new JLabel("  <<", JLabel.CENTER);yearleft.setToolTipText("上一年");yearright = new JLabel(">>  ", JLabel.CENTER);yearright.setToolTipText("下一年");yearleft.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));yearright.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));monthleft = new JLabel("  <", JLabel.RIGHT);monthleft.setToolTipText("上一月");monthright = new JLabel(">  ", JLabel.LEFT);monthright.setToolTipText("下一月");monthleft.setBorder(BorderFactory.createEmptyBorder(2, 30, 0, 0));monthright.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 30));centercontainer = new JLabel("", JLabel.CENTER);centercontainer.setLayout(new BorderLayout());center = new JLabel("", JLabel.CENTER);centercontainer.add(monthleft, BorderLayout.WEST);centercontainer.add(center, BorderLayout.CENTER);centercontainer.add(monthright, BorderLayout.EAST);this.add(yearleft, BorderLayout.WEST);this.add(centercontainer, BorderLayout.CENTER);this.add(yearright, BorderLayout.EAST);this.setPreferredSize(new Dimension(295, 25));updateDate();yearleft.addMouseListener(new MouseAdapter() {public void mouseEntered(MouseEvent me) {yearleft.setCursor(new Cursor(Cursor.HAND_CURSOR));yearleft.setForeground(Color.RED);}public void mouseExited(MouseEvent me) {yearleft.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));yearleft.setForeground(Color.BLACK);}public void mousePressed(MouseEvent me) {select.add(Calendar.YEAR, -1);yearleft.setForeground(Color.WHITE);refresh();}public void mouseReleased(MouseEvent me) {yearleft.setForeground(Color.BLACK);}});yearright.addMouseListener(new MouseAdapter() {public void mouseEntered(MouseEvent me) {yearright.setCursor(new Cursor(Cursor.HAND_CURSOR));yearright.setForeground(Color.RED);}public void mouseExited(MouseEvent me) {yearright.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));yearright.setForeground(Color.BLACK);}public void mousePressed(MouseEvent me) {select.add(Calendar.YEAR, 1);yearright.setForeground(Color.WHITE);refresh();}public void mouseReleased(MouseEvent me) {yearright.setForeground(Color.BLACK);}});monthleft.addMouseListener(new MouseAdapter() {public void mouseEntered(MouseEvent me) {monthleft.setCursor(new Cursor(Cursor.HAND_CURSOR));monthleft.setForeground(Color.RED);}public void mouseExited(MouseEvent me) {monthleft.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));monthleft.setForeground(Color.BLACK);}public void mousePressed(MouseEvent me) {select.add(Calendar.MONTH, -1);monthleft.setForeground(Color.WHITE);refresh();}public void mouseReleased(MouseEvent me) {monthleft.setForeground(Color.BLACK);}});monthright.addMouseListener(new MouseAdapter() {public void mouseEntered(MouseEvent me) {monthright.setCursor(new Cursor(Cursor.HAND_CURSOR));monthright.setForeground(Color.RED);}public void mouseExited(MouseEvent me) {monthright.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));monthright.setForeground(Color.BLACK);}public void mousePressed(MouseEvent me) {select.add(Calendar.MONTH, 1);monthright.setForeground(Color.WHITE);refresh();}public void mouseReleased(MouseEvent me) {monthright.setForeground(Color.BLACK);}});}private void updateDate() {center.setText(select.get(Calendar.YEAR) + "年"+ (select.get(Calendar.MONTH) + 1) + "月");}}private class JP2 extends JPanel {private static final long serialVersionUID = -8176264838786175724L;public JP2() {this.setPreferredSize(new Dimension(295, 20));}protected void paintComponent(Graphics g) {g.setFont(font);g.drawString("星期日 星期一 星期二 星期三 星期四 星期五 星期六", 5, 10);g.drawLine(0, 15, getWidth(), 15);}private void updateDate() {}}private class JP3 extends JPanel {private static final long serialVersionUID = 43157272447522985L;public JP3() {super(new GridLayout(6, 7));this.setPreferredSize(new Dimension(295, 100));initJP3();}private void initJP3() {updateDate();}public void updateDate() {this.removeAll();lm.clear();Date temp = select.getTime();Calendar select = Calendar.getInstance();select.setTime(temp);select.set(Calendar.DAY_OF_MONTH, 1);int index = select.get(Calendar.DAY_OF_WEEK);int sum = (index == 1 ? 8 : index);select.add(Calendar.DAY_OF_MONTH, 0 - sum);for (int i = 0; i < 42; i++) {select.add(Calendar.DAY_OF_MONTH, 1);lm.addLabel(new MyLabel(select.get(Calendar.YEAR), select.get(Calendar.MONTH), select.get(Calendar.DAY_OF_MONTH)));}for (MyLabel my : lm.getLabels()) {this.add(my);}select.setTime(temp);}}private class MyLabel extends JLabel implements Comparator<MyLabel>,MouseListener, MouseMotionListener {private static final long serialVersionUID = 3668734399227577214L;private int year, month, day;private boolean isSelected;public MyLabel(int year, int month, int day) {super("" + day, JLabel.CENTER);this.year = year;this.day = day;this.month = month;this.addMouseListener(this);this.addMouseMotionListener(this);this.setFont(font);if (month == select.get(Calendar.MONTH)) {this.setForeground(Color.BLACK);} else {this.setForeground(Color.LIGHT_GRAY);}if (day == select.get(Calendar.DAY_OF_MONTH)) {this.setBackground(new Color(160, 185, 215));} else {this.setBackground(Color.WHITE);}}public boolean getIsSelected() {return isSelected;}public void setSelected(boolean b, boolean isDrag) {isSelected = b;if (b && !isDrag) {int temp = select.get(Calendar.MONTH);select.set(year, month, day);if (temp == month) {SwingUtilities.updateComponentTreeUI(jp3);} else {refresh();}}this.repaint();}protected void paintComponent(Graphics g) {if (day == select.get(Calendar.DAY_OF_MONTH)&& month == select.get(Calendar.MONTH)) {// 如果当前日期是选择日期,则高亮显示g.setColor(new Color(160, 185, 215));g.fillRect(0, 0, getWidth(), getHeight());}if (year == now.get(Calendar.YEAR)&& month == now.get(Calendar.MONTH)&& day == now.get(Calendar.DAY_OF_MONTH)) {// 如果日期和当前日期一样,则用红框Graphics2D gd = (Graphics2D) g;gd.setColor(Color.RED);Polygon p = new Polygon();p.addPoint(0, 0);p.addPoint(getWidth() - 1, 0);p.addPoint(getWidth() - 1, getHeight() - 1);p.addPoint(0, getHeight() - 1);gd.drawPolygon(p);}if (isSelected) {// 如果被选中了就画出一个虚线框出来Stroke s = new BasicStroke(1.0f, BasicStroke.CAP_SQUARE,BasicStroke.JOIN_BEVEL, 1.0f,new float[]{2.0f, 2.0f}, 1.0f);Graphics2D gd = (Graphics2D) g;gd.setStroke(s);gd.setColor(Color.BLACK);Polygon p = new Polygon();p.addPoint(0, 0);p.addPoint(getWidth() - 1, 0);p.addPoint(getWidth() - 1, getHeight() - 1);p.addPoint(0, getHeight() - 1);gd.drawPolygon(p);}super.paintComponent(g);}public boolean contains(Point p) {return this.getBounds().contains(p);}private void update() {repaint();}public void mouseClicked(MouseEvent e) {}public void mousePressed(MouseEvent e) {isSelected = true;update();}public void mouseReleased(MouseEvent e) {Point p = SwingUtilities.convertPoint(this, e.getPoint(), jp3);lm.setSelect(p, false);commit();}public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public void mouseDragged(MouseEvent e) {Point p = SwingUtilities.convertPoint(this, e.getPoint(), jp3);lm.setSelect(p, true);}public void mouseMoved(MouseEvent e) {}public int compare(MyLabel o1, MyLabel o2) {Calendar c1 = Calendar.getInstance();c1.set(o1.year, o2.month, o1.day);Calendar c2 = Calendar.getInstance();c2.set(o2.year, o2.month, o2.day);return c1.compareTo(c2);}}private class LabelManager {private List<MyLabel> list;public LabelManager() {list = new ArrayList<MyLabel>();}public List<MyLabel> getLabels() {return list;}public void addLabel(MyLabel my) {list.add(my);}public void clear() {list.clear();}@SuppressWarnings("unused")public void setSelect(MyLabel my, boolean b) {for (MyLabel m : list) {if (m.equals(my)) {m.setSelected(true, b);} else {m.setSelected(false, b);}}}public void setSelect(Point p, boolean b) {// 如果是拖动,则要优化一下,以提高效率if (b) {// 表示是否能返回,不用比较完所有的标签,能返回的标志就是把上一个标签和// 将要显示的标签找到了就可以了boolean findPrevious = false, findNext = false;for (MyLabel m : list) {if (m.contains(p)) {findNext = true;if (m.getIsSelected()) {findPrevious = true;} else {m.setSelected(true, b);}} else if (m.getIsSelected()) {findPrevious = true;m.setSelected(false, b);}if (findPrevious && findNext) {return;}}} else {MyLabel temp = null;for (MyLabel m : list) {if (m.contains(p)) {temp = m;} else if (m.getIsSelected()) {m.setSelected(false, b);}}if (temp != null) {temp.setSelected(true, b);}}}}private class JP4 extends JPanel {private static final long serialVersionUID = -6391305687575714469L;public JP4() {super(new BorderLayout());this.setPreferredSize(new Dimension(295, 20));this.setBackground(new Color(160, 185, 215));SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");final JLabel jl = new JLabel("今天: " + sdf.format(new Date()));jl.setToolTipText("点击选择今天日期");this.add(jl, BorderLayout.CENTER);jl.addMouseListener(new MouseAdapter() {public void mouseEntered(MouseEvent me) {jl.setCursor(new Cursor(Cursor.HAND_CURSOR));jl.setForeground(Color.RED);}public void mouseExited(MouseEvent me) {jl.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));jl.setForeground(Color.BLACK);}public void mousePressed(MouseEvent me) {jl.setForeground(Color.WHITE);select.setTime(new Date());refresh();commit();}public void mouseReleased(MouseEvent me) {jl.setForeground(Color.BLACK);}});}private void updateDate() {}}
}
 public class AttendanceQuery02 {public static void main(String[] args) {//1.创建窗体对象JFrame jf = new JFrame();jf.setTitle("考勤查询");jf.setSize(400, 300);jf.setDefaultCloseOperation(3);jf.setLocationRelativeTo(null);jf.setAlwaysOnTop(true);jf.setLayout(null);//2.显示考勤日期的文本JLabel dateLable = new JLabel("考勤日期");dateLable.setBounds(50,20,100,20);jf.add(dateLable);//3.显示开始时间文本JLabel startDateLable = new JLabel("开始时间");startDateLable.setBounds(50,70,100,20);jf.add(startDateLable);//4.创建日历控件DateChooser dateChooser1 = DateChooser.getInstance("yyyy/MM/dd");DateChooser dateChooser2 = DateChooser.getInstance("yyyy/MM/dd");//5.开始时间输入框JTextField startDateField = new JTextField();startDateField.setBounds(50,100,100,20);dateChooser1.register(startDateField); //日历控件绑定到,开始时间输入框中jf.add(startDateField);//6.显示结束时间文本JLabel endDateLable = new JLabel("结束时间");endDateLable.setBounds(250,70,100,20);jf.add(endDateLable);//7.结束时间输入框JTextField endDateField = new JTextField();endDateField.setBounds(250,100,100,20);dateChooser2.register(endDateField); //日历控件绑定到,结束时间输入框中jf.add(endDateField);//8.确定按钮JButton confirmButton = new JButton("确定");confirmButton.setBounds(250,180,60,20);jf.add(confirmButton);//9.设置窗体可见jf.setVisible(true);}}

(10)事件监听机制
10.1.事件监听机制概述
事件源: 事件发生的地方。可以是按钮,窗体,图片等;
事件: 发生了什么事情。例如:鼠标点击事件,键盘按下事件等;
事件绑定: 把事件绑定到事件源上,当发生了某个事件,则触发对应的处理逻辑;
事件源对象. addXXXListener(事件);
10.2 事件监听机制案例

 public class ActionListenerDemo {public static void main(String[] args) {//1.创建窗体对象JFrame jf = new JFrame();jf.setTitle("事件监听机制");jf.setSize(400, 300);jf.setDefaultCloseOperation(3);jf.setLocationRelativeTo(null);jf.setAlwaysOnTop(true);jf.setLayout(null);//2.创建按钮JButton jButton = new JButton("你点我啊");jButton.setBounds(0, 0, 100, 100);jf.add(jButton);//3.给按钮绑定事件jButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("你点我啊"); //事件触发执行逻辑}});//4.设置窗体可见jf.setVisible(true);}}

http://www.ppmy.cn/news/2681.html

相关文章

芒果改进YOLOv5系列:全网首发最新原创打造RepGhostNeXt结构,基于重参数化结构,实现硬件高效的RepGhost模块、打造全新YOLOv5检测器

💡该教程为改进进阶指南,包含大量的原创首发改进方式, 所有文章都是全网首发原创改进内容🚀💡本篇文章 基于 YOLOv5、YOLOv7等网络改进YOLO系列:全网首发最新原创打造RepGhostNeXt结构,基于重参数化结构,实现硬件高效的RepGhost 模块、打造全新YOLOv5检测器。重点:�…

SpringBoot自动配置的原理-@SpringBootApplication

文章目录1自动配置原理1.1 SpringBootConfiguration1.2.ComponentScan1.3 EnableAutoConfiguration2 为什么不使用 Import 直接引入自动配置类学完这篇文章&#xff0c;可以了解到 SpringBoot 自动配置原理1自动配置原理 SpringBootConfiguration 是一个组合注解&#xff0c;由…

【面试题】2023 中级前端面试题

前言 从前端学习到找一份合适的工作&#xff0c;大大小小的面试必不可少&#xff0c;所以我对初级前端面试题进行了初步整理&#xff0c;也方便自己查阅&#xff0c;也希望对小伙伴们有所帮助&#xff01; 给大家推荐一个实用面试题库 1、前端面试题库 &#xff08;面试必备&…

计算机毕业设计Java普通中学教职工信息管理系统(源码+系统+mysql数据库+lw文档)

计算机毕业设计Java普通中学教职工信息管理系统(源码系统mysql数据库lw文档&#xff09; 计算机毕业设计Java普通中学教职工信息管理系统(源码系统mysql数据库lw文档&#xff09;本源码技术栈&#xff1a; 项目架构&#xff1a;B/S架构 开发语言&#xff1a;Java语言 开发软…

Linux邮件服务Postfix部署

我们看下邮件协议&#xff1a; 简单邮件传输协议&#xff08;SMTP&#xff09;&#xff1a;用于发送和中转出的电子邮件。使用TCP/25端口。 邮局协议版本&#xff08;POP3&#xff09;&#xff1a;用于将邮件存储到本地&#xff0c;占用服务器的TCP/110端口。 Internet 消息访问…

【lc刷题 day4】栈的压入、弹出序列 从上到下打印二叉树 二叉搜索树的后序遍历数列

剑指offer 31.栈的压入、弹出序列 medium class Solution {public boolean validateStackSequences(int[] pushed, int[] popped) {Stack<Integer> snew Stack<>();int i0;for(int j0;j<pushed.length;j){s.push(pushed[j]);while(!s.isEmpty()&&poppe…

【Pytorch】第 3 章 :进行数值估计的蒙特卡洛方法

&#x1f50e;大家好&#xff0c;我是Sonhhxg_柒&#xff0c;希望你看完之后&#xff0c;能对你有所帮助&#xff0c;不足请指正&#xff01;共同学习交流&#x1f50e; &#x1f4dd;个人主页&#xff0d;Sonhhxg_柒的博客_CSDN博客 &#x1f4c3; &#x1f381;欢迎各位→点赞…

解读小红书2022年母婴行业报告:心智种草的流量密码

母婴用户代际更迭&#xff0c;90后晋升为母婴消费主力军。新一代宝爸宝妈的关注点在哪里&#xff1f;品牌该如何通过小红书满足ta们的进阶需求&#xff0c;为母婴消费注入新活力&#xff1f; 本文将解读小红书官方发布的《2022年母婴行业人群洞察报告》&#xff0c;基于上千名用…