带文字的Button

效果图
[img]http://dl.iteye.com/upload/picture/pic/85569/77ccaf8e-d3d5-39dd-b7d5-0c2a24c2f25b.jpg[/img]

导师的项目里要用到的!
界面的最上边有工具栏,需要添加下边带文字提示的按钮!
通过继承,可以订做自己喜欢的按钮!


package util;

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

public class PicturizedButton extends JButton
{
private static final long serialVersionUID = -7782576754032594674L;
public PicturizedButton(String filename, String text) {
try {
PicturizedButton.this.setBorderPainted(false);
setHorizontalTextPosition(SwingConstants.CENTER);
setVerticalTextPosition(SwingConstants.BOTTOM);
setText(text);
if (filename != null) {
ImageIcon icon = new ImageIcon(this.getClass().getResource(
filename));
if (icon != null)
setIcon(icon);
}
} catch (Exception e) {
//System.out.println("[Error Load Image: " + filename + "]");
e.printStackTrace();
}
this.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
PicturizedButton.this.setBorderPainted(true);
PicturizedButton.this.setCursor(Cursor
.getPredefinedCursor(Cursor.HAND_CURSOR));
}

public void mouseExited(MouseEvent e) {
PicturizedButton.this.setBorderPainted(false);
PicturizedButton.this.setCursor(Cursor
.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

}
});
}

public static void main(String[] args){
JFrame frame = new JFrame("TestButton");
PicturizedButton icturizedButton = new PicturizedButton("/image/gis32.png","多拉a梦");
frame.add(icturizedButton);

frame.setVisible(true);
frame.setSize(200,200);
}
}
### 修改按钮样式的文字颜色 在 CSS 中,可以通过 `color` 属性来设置按钮 (`<button>`) 的文字颜色。以下是具体的实现方法: #### 使用 `color` 属性 通过定义 `color` 属性可以轻松更改按钮上的文字颜色。例如: ```css button { color: red; /* 将按钮的文字颜色更改为红色 */ } ``` 如果需要针对特定类名的按钮进行样式调整,则可以在选择器中指定该类名[^1]。例如: ```html <button class="custom-button">自定义按钮</button> ``` 对应的 CSS 可以这样写: ```css .custom-button { color: blue; /* 更改 .custom-button 类下的按钮文字颜色为蓝色 */ } ``` #### Android 和 iOS 开发中的文字颜色设置 对于移动端开发,在 Android 平台下可通过 XML 或 Java/Kotlin 来设置按钮的文字颜色;而在 iOS 下则可利用 Objective-C 或 Swift 实现相同效果。 - **Android**: 在布局文件 (XML) 中可以直接使用 `android:textColor` 属性设定文字颜色[^2]。例如: ```xml <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击我" android:textColor="#FF0000" /> <!-- 文字颜色设为红色 --> ``` - **iOS**: 利用 UIButton 的 titleLabel 属性能够控制其外观特性,包括但不限于字体大小、颜色等参数。示例代码如下: ```swift let button = UIButton(type: .system) button.setTitle("登录", for: .normal) button.setTitleColor(.blue, for: .normal) // 设置正常状态下的文字颜色为蓝色 ``` #### 行高等其他细节处理 当遇到按钮内部文字垂直方向未完全居中的情况时,除了基本的高度(`height`)外还需要额外关注行高(`line-height`)这一属性[^4]。合理配置两者关系有助于改善视觉体验并达到预期设计目标。 最终综合以上要点的一个完整实例可能看起来像这样: ```css .my-custom-btn { height: 100rpx; line-height: 100rpx; /* 确保文字水平和垂直都处于中心位置 */ color: green; /* 定义绿色作为文字显示色彩 */ } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值