我想编辑我的文本居中对齐方式。我已经知道你可以这样做: intro.setHorizontalAlignment(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 Action和 Text Component Features .
关于java - 像 Microsoft 一样居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19507361/