Java在JButton中添加一个JLabel

时间:2016-03-14 00:42:08

标签: java swing jbutton jlabel

我想简单地将JLabel添加到JButton中并将其水平居中。我尝试过很多东西,但文字留在左边......这是我的代码:

    JLabel labeltest = new JLabel("Simple test");
    JButton myButton = new JButton();
    myButton.setHorizontalTextPosition(SwingConstants.CENTER);
    myButton.add(labeltest);

output picture

2 个答案:

答案 0 :(得分:3)

创建文本图标,并为图标指定所需的前景/背景颜色。

  

我尝试了这个,但是当我不得不做button.setenabled(false)时出现问题;在此之后,文本变得几乎不可见

您可以将禁用的Icon设置为与Icon相同,并且不会在禁用状态下绘制:

JButton button = new JButton( new ImageIcon(...) );
button.setDisabledIcon( button.getIcon() );
button.setEnabled(false);

当然,这种方法的问题是用户不知道按钮被禁用。所以实际上你需要2个图标:

  1. 一个正常状态
  2. 一个为残疾人状态

答案 1 :(得分:3)

JButton也是java.awt.Container。因此,您可以设置布局管理器。例如。你可以使用GridBagLayout。

public class Main {

  public static void main(String[] args) {
    JFrame frame = new JFrame();

    JToggleButton toggleButton = new JToggleButton();
    JLabel jLabel = new JLabel("3");

    JToggleButton.ToggleButtonModel toggleButtonModel = (JToggleButton.ToggleButtonModel) toggleButton.getModel()
    ToggleForegroundAction toggleForegroundAction = 
            new ToggleForegroundAction(toggleButtonModel, Color.WHITE, Color.RED);
    toggleForegroundAction.setComponent(jLabel);
    toggleButton.setAction(toggleForegroundAction);

    toggleButton.setLayout(new GridBagLayout());
    toggleButton.add(jLabel, new GridBagConstraints());

    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    panel.setLayout(new BorderLayout());
    panel.add(toggleButton, BorderLayout.CENTER);

    Container contentPane = frame.getContentPane();
    contentPane.add(panel);

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.pack();
    frame.setVisible(true);
  }
}

切换标签前景色的动作可能如下所示

  public class ToggleForegroundAction extends AbstractAction {

    private JComponent component;
    private JToggleButton.ToggleButtonModel toggleButtonModel;
    private final Color selectedColor;
    private final Color unselectedColor;

    public ToggleForegroundAction(JToggleButton.ToggleButtonModel toggleButtonModel, Color selectedColor, Color unselectedColor) {
      this.toggleButtonModel = toggleButtonModel;
      this.selectedColor = selectedColor;
      this.unselectedColor = unselectedColor;
    }

    public void setComponent(JComponent component) {
      this.component = component;
      setForeground(component, toggleButtonModel.isSelected());
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      JComponent targetComponent = this.component;
      if (targetComponent == null) {
        targetComponent = (JComponent) e.getSource();
      }
      setForeground(targetComponent, toggleButtonModel.isSelected());
    }

    private void setForeground(JComponent targetComponent, boolean isSelected) {
      Color foreground;
      if (isSelected) {
        foreground = selectedColor;
      } else {
        foreground = unselectedColor;
      }
      targetComponent.setForeground(foreground);
    }
  }

Unselected => Selected