使用JButton绘制边框时,所有按钮都会消失

时间:2017-02-24 10:51:03

标签: java swing jbutton

我使用Swing制作了一个带有GUI的程序。当我使用带有绘制边框的JButton时,它会使所有其他按钮消失。我用一个最小的代码创建了一个程序,按钮工作得很好:

public class ShowError {
  public static void main(String[] args) {
    JFrame _GameWindow = new JFrame("Trivial Pursuit");
    _GameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    _GameWindow.setSize(800, 650);
    _GameWindow.setResizable(false);
    _GameWindow.setVisible(true);

    JPanel _MainMenu = new JPanel();
    _MainMenu.setLayout(new FlowLayout(FlowLayout.CENTER));
    _GameWindow.add(_MainMenu);

    ImageIcon[] _PlayerButtonImage = new ImageIcon[5];
    for (int i = 2; i < _PlayerButtonImage.length; i++) {
        _PlayerButtonImage[i] = new ImageIcon("Images/" + i + "PlayerButton.png");
    }
    ImageIcon _HowToPlayButtonImage = new ImageIcon("Images/HowToPlayButton.png");

    JButton[] _PlayerButton = new JButton[5];
    for (int i = 2; i < _PlayerButton.length; i++) {
        _PlayerButton[i] = new JButton(_PlayerButtonImage[i]);
        _MainMenu.add(_PlayerButton[i]);
        _PlayerButton[i].setOpaque(true);
        _PlayerButton[i].setPreferredSize(new Dimension(271,123));
        _PlayerButton[i].setContentAreaFilled(true);
        _PlayerButton[i].setBorderPainted(true);
        _PlayerButton[i].setFocusPainted(true);
    }

    JButton _HowToPlayButton = new JButton(_HowToPlayButtonImage);
    _MainMenu.add(_HowToPlayButton);
    _HowToPlayButton.setOpaque(true);
    _HowToPlayButton.setContentAreaFilled(true);
    _HowToPlayButton.setBorderPainted(true);
    _HowToPlayButton.setPreferredSize(new Dimension(271,123));
    _HowToPlayButton.setFocusPainted(true);
  }
}

此代码有效,但在我的实际程序中,它无法正常工作。

这是我的实际计划:

    JButton[] _PlayerButton = new JButton[5];
    for (int i = 2; i < _PlayerButton.length; i++) {
        _PlayerButton[i] = new JButton(_PlayerButtonImage[i]);
        _MainMenu.add(_PlayerButton[i]);
        _PlayerButton[i].setOpaque(true);
        _PlayerButton[i].setPreferredSize(new Dimension(271, 123));
        _PlayerButton[i].setContentAreaFilled(true);
        _PlayerButton[i].setBorderPainted(true);
        _PlayerButton[i].setFocusPainted(true);
    }

    JButton _HowToPlayButton = new JButton(_HowToPlayButtonImage);
    _MainMenu.add(_HowToPlayButton);
    _HowToPlayButton.setOpaque(false);
    _HowToPlayButton.setContentAreaFilled(true);
    _HowToPlayButton.setBorderPainted(false);

如果我将边框设置为false,一切都很好。当我将其设置为true时,一切都会消失。

1 个答案:

答案 0 :(得分:3)

这实际上是一个非常简单的修复

JButton[] _PlayerButton = new JButton[4];
    for (int i = 0; i < _PlayerButton.length; i++) {
        final int FinalI = i;
        _PlayerButton[i] = new JButton(_PlayerButtonImage[i]);
        _MainMenu.add(_PlayerButton[i]);
        _PlayerButton[i].setOpaque(false);
        _PlayerButton[i].setPreferredSize(new Dimension(271, 123));
        _PlayerButton[i].setContentAreaFilled(false);
        _PlayerButton[i].setBorder(BorderFactory.createEmptyBorder());

        _PlayerButton[i].addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                _PlayerButton[FinalI].setBorder(BorderFactory.createLoweredBevelBorder());
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                _PlayerButton[FinalI].setBorder(BorderFactory.createEmptyBorder());
            }
        });
    }