关闭

Java:简单的Swing用户界面和监听按钮点击事件

标签: swingjavabuttonstringsystemgtk
44938人阅读 评论(1) 收藏 举报
分类:

下面的程序,显示一个按钮和一个标签。每次点击按钮时,标签都将更新。

package hustspy;

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

public class hello implements ActionListener {
    private static String labelPrefix = "Number of button clicks: ";
    private int numClicks = 0;
    final JLabel label = new JLabel(labelPrefix + "0    ");

    //Specify the look and feel to use.  Valid values:
    //null (use the default), "Metal", "System", "Motif", "GTK+"
    final static String LOOKANDFEEL = "System";

    public Component createComponents() {
        JButton button = new JButton("I'm a Swing button!");
        button.setMnemonic(KeyEvent.VK_I);
        button.addActionListener(this);
        label.setLabelFor(button);

        /*
         * An easy way to put space between a top-level container
         * and its contents is to put the contents in a JPanel
         * that has an "empty" border.
         */
        JPanel pane = new JPanel(new GridLayout(0, 1));
        pane.add(button);
        pane.add(label);
        pane.setBorder(BorderFactory.createEmptyBorder(
                                        30, //top
                                        30, //left
                                        10, //bottom
                                        30) //right
                                        );

        return pane;
    }

    public void actionPerformed(ActionEvent e) {
        numClicks++;
        label.setText(labelPrefix + numClicks);
    }

    private static void initLookAndFeel() {
        String lookAndFeel = null;

        if (LOOKANDFEEL != null) {
            if (LOOKANDFEEL.equals("Metal")) {
                lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
            } else if (LOOKANDFEEL.equals("System")) {
                lookAndFeel = UIManager.getSystemLookAndFeelClassName();
            } else if (LOOKANDFEEL.equals("Motif")) {
                lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
            } else if (LOOKANDFEEL.equals("GTK+")) { //new in 1.4.2
                lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
            } else {
                System.err.println("Unexpected value of LOOKANDFEEL specified: "
                                   + LOOKANDFEEL);
                lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
            }

            try {
                UIManager.setLookAndFeel(lookAndFeel);
            } catch (ClassNotFoundException e) {
                System.err.println("Couldn't find class for specified look and feel:"
                                   + lookAndFeel);
                System.err.println("Did you include the L&F library in the class path?");
                System.err.println("Using the default look and feel.");
            } catch (UnsupportedLookAndFeelException e) {
                System.err.println("Can't use the specified look and feel ("
                                   + lookAndFeel
                                   + ") on this platform.");
                System.err.println("Using the default look and feel.");
            } catch (Exception e) {
                System.err.println("Couldn't get specified look and feel ("
                                   + lookAndFeel
                                   + "), for some reason.");
                System.err.println("Using the default look and feel.");
                e.printStackTrace();
            }
        }
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Set the look and feel.---设置外观,可以忽略
        initLookAndFeel();

        //Make sure we have nice window decorations.
    	//设置为false的话,即为不改变外观
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.
        JFrame frame = new JFrame("SwingApplication");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        hello app = new hello();
        Component contents = app.createComponents();
        frame.getContentPane().add(contents, BorderLayout.CENTER);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}


2
1
查看评论

Java语言学习--Swing中Button事件监听

一、前言今天在使用Java Swing中的JButton的事件触发机制时遇到了许多问题,简单的了解了一下。二、事件监听机制下面用一张图分析事件监听的机制(图片来自网络): 三、代码分析3.1 分析一1.事件源注册监听器JButton newButton = new JButton(); new...
  • qq_33429968
  • qq_33429968
  • 2017-06-18 21:01
  • 4669

Swing中添加事件响应的三种处理方式

在Swing中,事件响应是通过监听器对象来处理事件的方式实行的,这种方式被称为事件委托模型. 以JButton举例,它内部有一个名为listenerList的链表,在点击按钮时,会产生一个ActionEvent事件,此后内部会依次调用位于listenerList中的每一个actionListe...
  • moridehuixiang
  • moridehuixiang
  • 2015-04-30 14:36
  • 2486

java swing 单击事件mouseClicked与一般事件actionPerformed区别

java swing 单击事件mouseClicked与一般事件actionPerformed区别 分类: swing 2011-10-18 15:43 692人阅读 评论(0) 收藏 举报 //鼠标单击事件无论什么时候都监听,即使按钮已经不能用了,事件依然走; //...
  • litoupu
  • litoupu
  • 2012-11-12 21:56
  • 15405

Swing触发事件的3种方法

/** * 方法1 * 利用一个ActionListener来监听事件源产生的事件 * 用多个if来判断事件源 */ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListe...
  • qq_36238595
  • qq_36238595
  • 2017-04-19 21:17
  • 887

Swing 的按钮循环添加点击事件并被公共访问

package Other;import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.JButton; import javax.swing.JFrame;public cla...
  • max_wcsdn
  • max_wcsdn
  • 2017-12-18 13:10
  • 69

Java Swing界面编程(27)---JRadioButton事件处理

在单选按钮操作中,可以使用ItemListener接口进行事件的监听。 package com.beyole.util; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ItemEvent;...
  • xuejiawei123
  • xuejiawei123
  • 2014-06-13 18:40
  • 3824

Swing编程方面步骤之二各个按钮触发事件的处理原理

参考一 addActionListener()方法的学习
  • u012270113
  • u012270113
  • 2014-08-17 00:03
  • 3485

比较Java Swing中三种注册事件的方法

Swing 是目前Java中不可缺少的窗口工具组,是建立图形化用户界面(GUI)程序的强大工具。Java Swing组件自动产生各种事件来响应用户行为。Java将事件封装成事件类,并且为每个事件类定义了一个事件监听器。一个组件注册事件监听器方法,表明该组件要响应指定事件。也就是说...
  • chenssy
  • chenssy
  • 2012-03-25 17:40
  • 16128

JAVA Swing JButton按钮的事件响应

同样废话不多说,上代码:import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.*; public class Example1 ext...
  • yiwenyoucod
  • yiwenyoucod
  • 2017-04-05 20:12
  • 900

java中添加按钮并添加响应事件

有些包会帮你自动引入,关于面板,容器等百度一下。private Button LogInbtn = new Button("登陆"); final static JFrame buyerpagemain = new JFrame(); final Container conten...
  • qq_26816591
  • qq_26816591
  • 2016-07-15 10:36
  • 8302
    个人资料
    • 访问:604297次
    • 积分:7751
    • 等级:
    • 排名:第3263名
    • 原创:192篇
    • 转载:6篇
    • 译文:0篇
    • 评论:99条
    博客专栏
    文章分类
    最新评论