【发布时间】:2016-03-26 11:09:31
【问题描述】:
我正在开发一个swing GUI 项目,我有一个JButton 可以将文本转换为JTextPane 粗体。我使用Action 来执行此操作。
这是Action的代码
public static Action boldAction = new StyledEditorKit.BoldAction();
这是JButton的代码
JButton bold = new JButton("B");
bold.setFont(new Font("Arial", Font.BOLD, 15));
bold.setBounds(393, 15, 100, 100);
bold.setBackground(Color.WHITE);
bold.setAction(boldAction);
frame.add(bold);
如果不包含Action,按钮上的文本是粗体“B”,这就是我想要的。出现的问题是,当我添加操作时,它会将按钮上的文本更改为“font-bold”。
为什么会发生这种情况,我该如何解决?
【问题讨论】:
-
bold.setBounds(393, 15, 100, 100);1) Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或combinations of them 以及white space 的布局填充和边框。 2) 为了尽快获得更好的帮助,请发帖minimal reproducible example 或Short, Self Contained, Correct Example。 -
动作是独立的工作单元,其中包括按钮用来设置其文本的动作的名称