Java でイベントを追加するいくつかの方法 (codebrother の記事から若干の変更を加えて転載):
/** * Java事件监听处理——自身类实现ActionListener接口,作为事件监听器 * * @author codebrother */ class EventListener1 extends JFrame implements ActionListener { private JButton btBlue, btDialog; public EventListener1() { setTitle("Java GUI 事件监听处理"); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("蓝色"); btDialog = new JButton("弹窗"); // 将按钮添加事件监听器 btBlue.addActionListener(this); btDialog.addActionListener(this); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // ***************************事件处理*************************** @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == btBlue) { Container c = getContentPane(); c.setBackground(Color.BLUE); } else if (e.getSource() == btDialog) { JDialog dialog = new JDialog(); dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); } } } /** * Java事件监听处理——内部类处理 * * @author codebrother */ class EventListener3 extends JFrame { private JButton btBlue, btDialog; // 构造方法 public EventListener3() { setTitle("Java GUI 事件监听处理"); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("蓝色"); btDialog = new JButton("弹窗"); // 添加事件监听器对象(面向对象思想) btBlue.addActionListener(new ColorEventListener()); btDialog.addActionListener(new DialogEventListener()); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // 内部类ColorEventListener,实现ActionListener接口 class ColorEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { Container c = getContentPane(); c.setBackground(Color.BLUE); } } // 内部类DialogEventListener,实现ActionListener接口 class DialogEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { JDialog dialog = new JDialog(); dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); } } } /** * Java事件监听处理——匿名内部类处理 * * @author codebrother */ class EventListener2 extends JFrame { private JButton btBlue, btDialog; public EventListener2() { setTitle("Java GUI 事件监听处理"); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("蓝色"); btDialog = new JButton("弹窗"); // 添加事件监听器(此处即为匿名类) btBlue.addActionListener(new ActionListener() { // 事件处理 @Override public void actionPerformed(ActionEvent e) { Container c = getContentPane(); c.setBackground(Color.BLUE); } }); // 并添加事件监听器 btDialog.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JDialog dialog = new JDialog(); dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); } }); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } /** * Java事件监听处理——外部类处理 * * @author codebrother */ class EventListener4 extends JFrame { private JButton btBlue, btDialog; public EventListener4() { setTitle("Java GUI 事件监听处理"); setBounds(100, 100, 500, 350); setLayout(new FlowLayout()); btBlue = new JButton("蓝色"); btDialog = new JButton("弹窗"); // 将按钮添加事件监听器 btBlue.addActionListener(new ColorEventListener(this)); btDialog.addActionListener(new DialogEventListener()); add(btBlue); add(btDialog); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } // 外部类ColorEventListener,实现ActionListener接口 class ColorEventListener implements ActionListener { private EventListener4 el; ColorEventListener(EventListener4 el) { this.el = el; } @Override public void actionPerformed(ActionEvent e) { Container c = el.getContentPane(); c.setBackground(Color.BLUE); } } // 外部类DialogEventListener,实现ActionListener接口 class DialogEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { JDialog dialog = new JDialog(); dialog.setBounds(300, 200, 400, 300); dialog.setVisible(true); } } public class ActionListenerTest { public static void main(String args[]) { new EventListener2(); } }
Java でイベント監視を追加する 4 つの方法のコード例の詳細については、PHP 中国語 Web サイトの関連記事に注目してください。

この記事では、Javaプロジェクト管理、自動化の構築、依存関係の解像度にMavenとGradleを使用して、アプローチと最適化戦略を比較して説明します。

この記事では、MavenやGradleなどのツールを使用して、適切なバージョン化と依存関係管理を使用して、カスタムJavaライブラリ(JARファイル)の作成と使用について説明します。

この記事では、カフェインとグアバキャッシュを使用してJavaでマルチレベルキャッシュを実装してアプリケーションのパフォーマンスを向上させています。セットアップ、統合、パフォーマンスの利点をカバーし、構成と立ち退きポリシー管理Best Pra

この記事では、キャッシュや怠zyなロードなどの高度な機能を備えたオブジェクトリレーショナルマッピングにJPAを使用することについて説明します。潜在的な落とし穴を強調しながら、パフォーマンスを最適化するためのセットアップ、エンティティマッピング、およびベストプラクティスをカバーしています。[159文字]

Javaのクラスロードには、ブートストラップ、拡張機能、およびアプリケーションクラスローダーを備えた階層システムを使用して、クラスの読み込み、リンク、および初期化が含まれます。親の委任モデルは、コアクラスが最初にロードされ、カスタムクラスのLOAに影響を与えることを保証します


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

WebStorm Mac版
便利なJavaScript開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)
