java中html中按钮居中,java – 如何在NetBeans中控制JButton HTML文本的居中?

原因是你限制了按钮的大小,默认情况下按钮有一个余量,你可以删除边距如下:

readerStopButton_.setMargin(new Insets(0,-30, 0,-30));

package swing;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.Insets;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

public class HTMLTextTest {

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

private static void createAndShowGUI() {

final JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new FlowLayout());

JPanel operationButtons_ = new JPanel();

JButton readerStopButton_ = new JButton();

readerStopButton_.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light"));

readerStopButton_.setFont(new java.awt.Font("Geneva", 0, 12)); // NOI18N

readerStopButton_.setText("

READER
STOP
\n");

readerStopButton_.setToolTipText("Stop button is currently inactive. ");

readerStopButton_.setMargin(new Insets(0,-30, 0,-30));

readerStopButton_.setPreferredSize(new Dimension(66, 40));

operationButtons_.add(readerStopButton_);

readerStopButton_ = new JButton();

readerStopButton_.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.light"));

readerStopButton_.setFont(new java.awt.Font("Geneva", 0, 12)); // NOI18N

readerStopButton_.setText("

READER
STOP
\n");

readerStopButton_.setToolTipText("Stop button is currently inactive. ");

System.out.println(readerStopButton_.getPreferredSize());

readerStopButton_.setPreferredSize(new Dimension(66, 40));

operationButtons_.add(readerStopButton_);

operationButtons_.add(new JButton("yCoder.com"));

frame.add(operationButtons_);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值