Swing开发之JButton篇

2024-01-04 02:48
文章标签 开发 swing jbutton

本文主要是介绍Swing开发之JButton篇,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

按钮大概是使用最为普遍的用户界面组件。按钮通常带有某种边框,且可以被鼠标或快捷键激活。能够激活它们来完成某个功能,而且很多其他Swing组件都是AbstractButton类的扩展,而AbstractButton类是Swing按钮的基类。

JButton共有4个构造函数:

JButton():建立一个按钮。
JButton(Icon icon):建立一个有图像的按钮。
JButton(String icon):建立一个有文字的按钮。
JButton(String text,Icon icon):建立一个有图像与文字的按钮。

JButton中常用方法说明:
addActionListener(ActionListener l)
          将一个 ActionListener 添加到按钮中。
getActionCommand()
          返回此按钮的动作命令。
getIcon()
          返回默认图标。
getMnemonic()
          返回当前模型中的键盘助记符。
getText()
          返回按钮的文本
setActionCommand(String actionCommand)
          设置此按钮的动作命令。
setBorderPainted(boolean b)
          设置 borderPainted 属性。
setEnabled(boolean b)
          启用(或禁用)按钮。
setHorizontalAlignment(int alignment)
          设置图标和文本的水平对齐方式
setHorizontalTextPosition(int textPosition)
          设置文本相对于图标的横向位置。
setIcon(Icon defaultIcon)
          设置按钮的默认图标。
setMnemonic(int mnemonic)
          设置当前模型上的键盘助记符。
setPressedIcon(Icon pressedIcon)
          设置按钮的按下图标。
setRolloverEnabled(boolean b)
          设置 rolloverEnabled 属性,若需要翻转效果,该属性必须为 true。
setRolloverIcon(Icon rolloverIcon)
          设置按钮的翻转图标。
setRolloverSelectedIcon(Icon rolloverSelectedIcon)
          设置按钮的翻转选择图标。
setText(String text)
          设置按钮的文本。
setVerticalAlignment(int alignment)
          设置图标和文本的垂直对齐方式。

在实际开发中为了保持界面风格统一,经常会重写JButton类。

例1:按钮中增加图片,增加鼠标时间效果

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; import javax.swing.BorderFactory; 
import javax.swing.Icon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; public class ButtonwithImageIcon extends JFrame { public static void main(String[] args) { ButtonwithImageIcon that = new ButtonwithImageIcon(); that.setVisible(true); } public ButtonwithImageIcon() { setSize(450, 350); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(new ButtonPanel(), BorderLayout.SOUTH); } 
} class ButtonPanel extends JPanel { public ButtonPanel() { JButton btn = new JButton("Push Me", new BoxIcon(Color.blue, 2)); //设置按钮的翻转图标。 
btn.setRolloverIcon(new BoxIcon(Color.cyan, 3)); //设置按钮的按下图标。 btn.setPressedIcon(new BoxIcon(Color.yellow, 4)); //设置文本相对于图标的横向位置。 
btn.setHorizontalTextPosition(JButton.LEFT); //设置按钮边框样式 btn.setBorder(BorderFactory.createEtchedBorder()); add(btn); } } 
class BoxIcon implements Icon { private Color color; private int borderWidth; BoxIcon(Color color, int borderWidth) { this.color = color; this.borderWidth = borderWidth; } public int getIconWidth() { return 20; } public int getIconHeight() { return 20; } public void paintIcon(Component c, Graphics g, int x, int y) { g.setColor(Color.black); g.fillRect(x, y, getIconWidth(), getIconHeight()); g.setColor(color); g.fillRect(x + borderWidth, y + borderWidth, getIconWidth() - 2 * borderWidth, getIconHeight() - 2 * borderWidth); } 
} 


例2:按钮中增加HTML样式

import javax.swing.JButton; 
import javax.swing.JFrame; 
public class HTMLButton { public static void main(String args[]) { JFrame frame = new JFrame("DefaultButton"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String htmlButton = "<html><sup>HTML</sup> <sub><em>Button</em></sub><br>" + "<font color=/"#FF0080/"><u>Multi-line</u></font>"; JButton button4 = new JButton(htmlButton); frame.add(button4); frame.setSize(300, 200); frame.setVisible(true); } 
} 


例3:设置默认按钮

import javax.swing.JFrame; 
import javax.swing.JRootPane; public class SettingDefaultButtonJRootPane { public static void main(String args[]) { JFrame frame = new JFrame("DefaultButton"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button4 = new JButton("AAA"); frame.add(button4,"Center"); frame.add(new JButton("BBB"),"South"); 
JRootPane rootPane = frame.getRootPane(); //设置默认按钮 rootPane.setDefaultButton(button4); frame.setSize(300, 200); frame.setVisible(true); } 
} 


例4 : 按钮事件

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.InputEvent; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; public class ButtonActionSample { public static void main(String args[]) { JFrame frame = new JFrame("Button Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Select Me"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("I was selected."); } }; MouseListener mouseListener = new MouseAdapter() { public void mousePressed(MouseEvent mouseEvent) { int modifiers = mouseEvent.getModifiers(); if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) { // Mask may not be set properly prior to Java 2 // See SwingUtilities.isLeftMouseButton() for workaround System.out.println("Left button pressed."); } if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) { System.out.println("Middle button pressed."); } if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) { System.out.println("Right button pressed."); } } public void mouseReleased(MouseEvent mouseEvent) { if (SwingUtilities.isLeftMouseButton(mouseEvent)) { System.out.println("Left button released."); } if (SwingUtilities.isMiddleMouseButton(mouseEvent)) { System.out.println("Middle button released."); } if (SwingUtilities.isRightMouseButton(mouseEvent)) { System.out.println("Right button released."); } System.out.println(); } }; button.addActionListener(actionListener); button.addMouseListener(mouseListener); Container contentPane = frame.getContentPane(); contentPane.add(button, BorderLayout.SOUTH); frame.setSize(300, 100); frame.setVisible(true); } 
} 


 

这篇关于Swing开发之JButton篇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/567929

相关文章

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

基于Python开发批量提取Excel图片的小工具

《基于Python开发批量提取Excel图片的小工具》这篇文章主要为大家详细介绍了如何使用Python中的openpyxl库开发一个小工具,可以实现批量提取Excel图片,有需要的小伙伴可以参考一下... 目前有一个需求,就是批量读取当前目录下所有文件夹里的Excel文件,去获取出Excel文件中的图片,并