/** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 适配器模式 Adapter Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc SwingUI.java * * */ package com.javapatterns.adapter; import java.awt.Color; import java.awt.BorderLayout; import java.awt.event.*; import javax.swing.*; /** * * @param * @author geovindu * * */ public class SwingUI extends JFrame implements ActionListener{ JLabel text, clicked,dutext; JButton button, clickButton; JPanel panel; private boolean m_clickMeMode = true; /** * @label Uses * @directed * */ private WindowAdapter lnkWindowAdapter; public SwingUI() { text = new JLabel("我很高兴!"); dutext=new JLabel("geovindu,"+"Geovin Du,涂聚文"); button = new JButton("理我"); button.addActionListener(this); panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.setBackground(Color.white); getContentPane().add(panel); panel.add(BorderLayout.CENTER, text); panel.add(BorderLayout.EAST,dutext); panel.add(BorderLayout.SOUTH, button); } /** * * @param * * */ public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (m_clickMeMode) { text.setText("我很烦!"); button.setText("别理我"); dutext.setText("Geovin Du,涂聚文"); m_clickMeMode = false; } else { text.setText("我很高兴!"); button.setText("理我"); dutext.setText("涂聚文"); m_clickMeMode = true; } } }
调用测试:
//适配器模式 SwingUI frame=new SwingUI(); frame.setTitle("我是涂聚文"); WindowListener listener = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.out.println("关窗了!"); System.exit(0); } }; frame.addWindowListener(listener); frame.pack(); frame.setVisible(true);
输出: