1

我正在使用 CDE/Motif LookAndFeel 测试我的 Java 代码。

当 PreferredSize 建立时,我正在查看 JButton 的性能/行为。

注意:我正在查看这篇文章Fit size of an ImageIcon to a JButton,但我不想动态地更改大小(JButton/ImageIcon),只想要 LAF 之间的“相同”外观。

我的代码

首先,我在飞行中创建了一个图像来测试 porpouses。

    BufferedImage bi = new BufferedImage(13, 13, BufferedImage.TYPE_INT_ARGB);

    Graphics gr = bi.createGraphics();
    gr.setColor(Color.RED);
    gr.drawLine(6, 0, 6, 13);
    gr.setColor(Color.GREEN);
    gr.drawLine(0, 6, 13, 6);
    gr.dispose();
    gr.drawImage(bi, 0, 0, null);

按钮!

    Font inFont = new Font("Monospaced", Font.PLAIN, 10);

    //Creating the Button!
    JButton jbt = new JButton(new ImageIcon(bi));
    jbt.setPreferredSize(new Dimension(30, 21)); // I want to use this Dimension!!!
    jbt.setSize(new Dimension(30, 21));
    jbt.setFont(inFont);

现在面板...

    JPanel jpHorz = new JPanel();
    GroupLayout jpLAFLayout = new GroupLayout(jpHorz);
    jpHorz.setLayout(jpLAFLayout);

    //Code to obtain this appareance inserting THE BUTTON 'jbt'
    jpLAFLayout.setHorizontalGroup(
      jpLAFLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jpLAFLayout.createSequentialGroup()
      .addGap(26, 26, 26)
      .addComponent(jbt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
      .addContainerGap(26, Short.MAX_VALUE))
    );
    jpLAFLayout.setVerticalGroup(
      jpLAFLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jpLAFLayout.createSequentialGroup()
      .addGap(6, 6, 6)
      .addComponent(jbt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE)
      .addContainerGap(12, Short.MAX_VALUE))
    );

更多代码...

    JPanel jpVert = new JPanel();
    jpVert.setBackground(new Color(208, 208, 208));
    jpVert.setLayout(new BoxLayout(jpVert, BoxLayout.PAGE_AXIS));
    jpVert.add(jpHorz);
    frame.add(jpVert, BorderLayout.NORTH);
    frame.setPreferredSize(new Dimension(480, 120));

如您所见,使用CDE/Motif LookAndFeel图像不完整

jbt.setMargin(new Insets(0, 0, 0, 0));没有得到效果。

有没有办法用 Motif LAF 改变 BUTTON 的这种不良外观?

这里是图像比较。

在此处输入图像描述

4

0 回答 0