创建圆形的SWING BUTTON

Java Swing实现画板,可以打开,保存,基本图形,画笔设定 Java Swing实现画板 https://github.com/nekoer723/drawpic 源码已上传 基本要求: 新建一个空白图形文件 打开或者保存一个图形文件 绘制基本图形 直线、矩形、圆、椭圆 任意线 添加文字 设定颜色与文字风格 优化: 设定画笔 橡皮擦 操作撤销 画线与图形的预览 1.面板UI设计与实现 1.设计 总窗口 布局flowlayout 设置关闭方式 窗体大小不变 菜单栏 3个面板(Jpanel),jp1,jp2,jp 阅读详情
CREATING ROUND SWING BUTTONS

This tip is about round Swing buttons. Actually, the information in this tip applies to a button of any arbitrary shape but to keep the discussion simple, we're just going to use a round shape.

There are two things you need to do when creating a round button. The first is to override the appropriate painting methods in order to paint the round shape. The second is to set things up so that the button responds only when you click within the round button (not just within the rectangle that contains the round button).

Here's an example program that implements a round button:

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

public class RoundButton extends JButton {
  public RoundButton(String label) {
    super(label);

// These statements enlarge the button so that it 
// becomes a circle rather than an oval.
    Dimension size = getPreferredSize();
    size.width = size.height = Math.max(size.width, 
      size.height);
    setPreferredSize(size);

// This call causes the JButton not to paint 
   // the background.
// This allows us to paint a round background.
    setContentAreaFilled(false);
  }

// Paint the round background and label.
  protected void paintComponent(Graphics g) {
    if (getModel().isArmed()) {
// You might want to make the highlight color 
   // a property of the RoundButton class.
      g.setColor(Color.lightGray);
    } else {
      g.setColor(getBackground());
    }
    g.fillOval(0, 0, getSize().width-1, 
      getSize().height-1);

// This call will paint the label and the 
   // focus rectangle.
    super.paintComponent(g);
  }

// Paint the border of the button using a simple stroke.
  protected void paintBorder(Graphics g) {
    g.setColor(getForeground());
    g.drawOval(0, 0, getSize().width-1, 
      getSize().height-1);
  }

// Hit detection.
  Shape shape;
  public boolean contains(int x, int y) {
// If the button has changed size, 
   // make a new shape object.
    if (shape == null || 
      !shape.getBounds().equals(getBounds())) {
      shape = new Ellipse2D.Float(0, 0, 
        getWidth(), getHeight());
    }
    return shape.contains(x, y);
  }

// Test routine.
  public static void main(String[] args) {
// Create a button with the label "Jackpot".
    JButton button = new RoundButton("Jackpot");
    button.setBackground(Color.green);

// Create a frame in which to show the button.
    JFrame frame = new JFrame();
    frame.getContentPane().setBackground(Color.yellow);
    frame.getContentPane().add(button);
    frame.getContentPane().setLayout(new FlowLayout());
    frame.setSize(150, 150);
    frame.setVisible(true);
  }
}

The RoundButton class extends JButton because we want to keep most of functionality of a JButton. In the RoundButton constructor, the method setContentAreaFilled() is called. This causes the button to paint the focus rectangle, but not to paint the background.

Now we need to paint the circular background. That's done by overriding the paintComponent() method. That method uses Graphics.fillOval() to paint a solid circle. Then paintComponent() calls super.paintComponent() to paint the label on top of the solid circle.

This example also overrides paintBorder() in order to paint a border around the round button. If you don't want a border, you would not override this method. This method calls Graphics.drawOval() to paint a thin border around the circle.

This example also overrides paintBorder() in order to paint a border around the round button. If you don't want a border, you would not override this method. This method calls Graphics.drawOval() to paint a thin border around the circle.

Note: In JDKTM 1.2.2, there's a small bug in how JButton behaves when you drag the mouse in and out of its perimeter. Ideally, if you clickon the circular button and then drag the mouse outside of the button's perimeter, the button should change its appearance. When you drag the mouse back into the circular button's perimeter, the button should restore its appearance. Unfortunately, the code that implements this behavior does not call the contains() method. Instead it just uses the 'bounds' of the button (this is the smallest rectangular area containing the button). Notice that if you drag the mouse slightly beyond the circular perimeter, that is, outside of the circle but not outside the bounds, the button won't change it's appearance. There is no workaround for this minor bug except to implement all of the functionality yourself.

<!--TIP TWO -->


swing圆角矩形按钮 swing默认按钮样式不敢恭维,好在扩展性不错。 立即下载

相关推荐

圆形按钮(JAVA)

圆形按钮(JAVA)

JAVA按钮编程_Java编程实现swing圆形按钮实例代码

Swing是一个为Java设计的GUI工具包。Swing是JAVA基础类的一部分。Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表。Swing提供许多比AWT更好的屏幕显示元素。它们用纯Java写成,所以同Java本身一样可以跨平台运行,这一点不像AWT。它们是JFC的一部分。它们支持可更换的面板和主题(各种操作系统默认的特有主题),然而不是真的使用原生平台提供的设备,而是...

weixin_36354355的博客 725

Java编程实现swing圆形按钮实例代码

主要介绍了Java编程实现swing圆形按钮实例代码,涉及两个简单的Java实现按钮的代码,其中一个具有侦测点击事件的简单功能,具有一定借鉴价值,需要的朋友可以参考。

制作圆形Swing按钮(中文版)

       这是一个关于制作圆形Swing按钮的技巧。事实上,这个技巧中的知识方便的适用于任何形状的按钮,但我们只作一个圆形的按钮。当你制作一个圆形的按钮时,需要做两件事。第一件事是重载一个适当的绘画方法以画出一个圆形。第二件事是设置一些事件使得只有当你点击圆形按钮的范围中的时侯按钮才会作出响应(不是包含圆形按钮的矩形的范围中)。下面是一个实现了圆形按钮的例程: import java.

jax的专栏 2191

ubuntu18.04安装xenomai3.1(百分百成功版)

ubuntu18.04安装xenomai3.1(百分百成功版),保姆级教学!!!

h542812556sdu的博客 2064

单细胞分析避坑指南:你的Harmony批次矫正真的做对了吗?

本文深入探讨了单细胞转录组分析中Harmony批次矫正的正确使用方法,解析了其工作原理及常见误区。通过详细参数调优指南和实战案例,帮助研究者避免过度矫正,确保生物学信号的有效保留。特别关注Harmony在单细胞数据分析中的关键参数设置和结果验证方法。

weixin_31099291的博客 299

制作圆形Swing按钮(中文版) (转)

制作圆形Swing按钮(中文版) (转)[@more@] ...

cuiji1279的博客 182

论文超详细精读|万字:2s-AGCN

1. 提出了一种新的双流自适应图卷积网络(2S-AGCN)用于基于骨架的动作识别。**模型中的图的拓扑既可以统一地学习,也可以通过BP算法以端到端的方式单独学习。** 2. 提出了一种同时对**一阶和二阶信息(骨架的长度与方向)建模的双流框架**,在识别精度上有了显著的提高。 它将骨架数据的图结构参数化,并嵌入到网络中,与模型共同学习和更新。 3. 文章主要创新点有两个,一个是双流网络,另外一个是自适应。

IanYue的博客 6384

创建圆形的SWING BUTTON (转)

创建圆形的SWING BUTTON (转)[@more@]CREATING ROUND swing BUTTONS This tip is about round Swing buttons. Actually, the...

csd3176的博客 185

【原创】Ubuntu20.04 安装 Isaac Gym 仿真器

Ubuntu20.04 安装 Isaac Gym

qq_54900679的博客 3093

FPGA图像处理入门 ZYNQ7020 图像中值膨胀腐蚀滤波

图像处理的三种滤波 中值滤波应用较为广泛 综合膨胀和腐蚀可以实现边缘检测。

Coast_的博客 715

swing之Button属性设置

import java.awt.*; import javax.swing.*; public class RoundButton extends JButton { public RoundButton(Stri...

congduishao9956的博客 688

Unity Android平台下插件/SDK开发通用流程

本文主要面向对Android开发不甚了解的Unity开发者,介绍了基于最新的Android Studio的标准Android开发环境与项目结构的配置流程,在此基础上,开发者可以快速的进行SDK的接入与插件的开发。

ZhangDi2017的博客 1万+

基于Java的圆形按钮实例代码,含注释

本文设计和实现了一种基于Java的圆形按钮,通过Java的图形界面库和事件处理功能,实现了一个具有圆形形状和交互功能的按钮,以提供更加美观和灵活的用户体验。本文设计并实现了一种基于Java的圆形按钮,利用Java的图形界面库和事件处理功能,实现了一个具有圆形形状和交互功能的按钮,以提供更加美观和灵活的用户体验。2.1 界面设计:设计按钮的外观和位置,确定圆形按钮的样式和大小。2.3 按钮样式:通过Java的图形界面库,设置按钮的颜色、边框样式和文本内容等,以提高按钮的可视性和用户交互性。

weixin_39827856的博客 636

c#上位机最强教程之环境安装

本文介绍了.NET 6开发环境的安装配置指南。主要内容包括:1)安装.NET 6 SDK和Runtime的方法及验证步骤;2)Visual Studio 2022的安装流程;3)创建第一个.NET 6控制台项目;4)项目结构解析和配置文件说明。文章还提供了实践练习和常见问题解答,帮助初学者快速搭建开发环境并运行简单程序。适用于零基础开发者,学习时间约10分钟。

飞上云层 316

23种设计模式-代理(Proxy)设计模式

软考中级-软件设计师 代理设计模式

片言可决讼,半纸能缚龙 1784

CREATING ROUND SWING BUTTONS 2 (转)

CREATING ROUND SWING BUTTONS 2 (转)[@more@]CREATING ROUND swing BUTTONSThis tip is about round Swing buttons. Actua...

csd3176的博客 103

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

java椭圆按钮_JButton时,椭圆形不会改变颜色-Java Swing

我在JFrame中有3个JPanel.两个用于JButton,另一个用于绘图画布.我为每个JButton生成一个随机的颜色,在JPanel的中间绘制一个椭圆,然后在JButton的每次单击中更改椭圆的颜色.问题是颜色没有改变.CirclePanel.java:public class CirclePanel extends JPanel{private static final long seri...

weixin_39938875的博客 293

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

一个SWING的怪问题

我最近在做学校里一个关于操作系统的实验,主要实现进程调度的算法,但要求是可视化,即动态地表现出进程的变化情况.这就牵涉到了界面的动态变化,比如JLabel上的文字要不断变化,由于我对SWING不熟悉所以碰到了一个大问题,如下.比如我有一个Button被监听,一旦按下便动态地不断改变JLable上的文字.button.addActionListener(new ActionListener(){pu

1050

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

swing-FlatLaf 自定义测试

FlatLaf 自定义测试

Record Little 2303

绘制背景为图片的圆形按钮

首先我们自定义一个边框绘制矩形边框import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Insets; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.b...

u012744265的博客 707

java中如何点击一个button后向jpanel切换_java – 在单击按钮时向JPanel添加形状

你有错误的想法.在绘图面板中,您应该有一个List< Circle>.在绘图面板的paintComponent方法中,您应该遍历列表以绘制每个圆class Circle {int x, int y, int width, int height;public Circle (int x, int y, int width, int height) {... set em}public v...

weixin_39580042的博客 268

圆形按钮

代码: xml version="1.0" encoding="utf-8"?> shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> solid android:color="#FFFFFFFF">solid> stroke

renjianhhong的博客 437

UIButton设置为圆形按钮并增加边框

设置按钮的长和宽尺寸一致(即为正方形),然后将圆角半径设为边长的一半,即形成一个圆形 UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; btn.frame = CGRectMake(100, 100, 100, 100); // 设置按钮四个圆角的半径,默认为0,这里取边长一半便是...

weixin_33936401的博客 301

java 鼠标画多个圆形_点击鼠标不同的建(左、中、右)画一个不同颜色的圆

[java]代码库import java.awt.Color;import java.awt.Graphics;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JFrame;public class T1 extends JFrame{int x;int y;Color ...

weixin_30000407的博客 326
上一篇: EJB、DCOM、CORBA 三种技术分析
下一篇: Delphi命令行参数
programlover
博客等级 码龄26年 1粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值