java jbutton文字居中_Java Swing - 如何更改JButton的标签对齐方式

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Component;

import java.awt.Container;

import java.awt.Graphics;

import javax.swing.Icon;

import javax.swing.JButton;

import javax.swing.JFrame;

public class Main {

public static void main(String args[]) {

JFrame frame = new JFrame();

Container contentPane = frame.getContentPane();

JButton b;

Icon icon = new PieIcon(Color.red);

b = new JButton("Default", icon);

contentPane.add(b, BorderLayout.NORTH);

b = new JButton("Text Left", icon);

b.setHorizontalTextPosition(JButton.LEFT);

contentPane.add(b, BorderLayout.SOUTH);

b = new JButton("Text Up", icon);

b.setHorizontalTextPosition(JButton.CENTER);

b.setVerticalTextPosition(JButton.TOP);

contentPane.add(b, BorderLayout.EAST);

b = new JButton("Text Down", icon);

b.setHorizontalTextPosition(JButton.CENTER);

b.setVerticalTextPosition(JButton.BOTTOM);

contentPane.add(b, BorderLayout.WEST);

b = new JButton("Align Bottom Left", icon);

b.setHorizontalAlignment(JButton.LEFT);

b.setVerticalAlignment(JButton.BOTTOM);

contentPane.add(b, BorderLayout.CENTER);

frame.setSize(300, 200);

frame.show();

}

}

class PieIcon implements Icon {

Color color;

public PieIcon(Color c) {

color = c;

}

public int getIconWidth() {

return 20;

}

public int getIconHeight() {

return 20;

}

public void paintIcon(Component c, Graphics g, int x, int y) {

g.setColor(color);

g.fillArc(x, y, getIconWidth(), getIconHeight(), 45, 270);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值