java - 像 Microsoft 一样居中文本

标签 java swing jframe jlabel centering

我想编辑我的文本居中对齐方式。我已经知道你可以这样做: intro.setHorizo​​ntalAlignment(JLabel.CENTER); 使其居中。但我想这样做,以便在您执行该图标时就像在 Microsoft Word 中一样。到目前为止,这是我的程序:

import javax.swing.*;

import javax.swing.JLabel;

public class FrameStuff

{

 public static void main(String[] args)

 {

 // Creating and setting up a regular frame
 JFrame frame = new JFrame();

 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 // Setting up frame characteristics
 frame.setSize(800, 600);
 frame.setTitle("FrameStuff");

 // Making it visible

frame.setVisible(true);

 //Making and assigning a new string

String textintro = new String("<html>This is my program <br/>I am having loads of fun <br/>Thanks for helping me!</html>");

// Made a new label (just happened to call it intro

 JLabel intro = new JLabel(textintro );

 // Set some sort of alignment within the frame

 intro.setHorizontalAlignment(JLabel.CENTER);

 //Add the dang label to the frame. 

frame.add(intro); 

}

}

我是第一次使用这个和编程新手。

最佳答案

例如,使用 StyledEditorKit 中的 AlignmentAction

JButton button = new JButton(new
    StyledEditorKit.AlignmentAction("Center", StyleConstants.ALIGN_CENTER));

您可以在这个相关的example 中试用.另见 How to use ActionText Component Features .

关于java - 像 Microsoft 一样居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19507361/

相关文章:

java - 如何使用 jdbc 中的 ResultSet.getBinaryStream() 从所有列中获取值?

java - 安卓 OpenGL ES : How do you select a 2D object?

java - 如何在更改另一个组合框项目选择后更改组合框的数据?

java - 如何在数组中正确添加JTextField组件?

java - 如何模拟 JButton 数组中特定 JButton 的移动

java - Android应用程序不断崩溃。帮助新手?

java - 如何在 JComboBox 中放置图像并设置其大小

java - 在 Eclipse 中导入现有的 Java 项目,但 JFrame 的设计 View 不存在?

java - 如何在方法期间而不是之后进行按钮更改重绘?

java - JFrame 重绘();错误