当我在Java中隐藏JButton边框时,为什么我的文本会消失?

时间:2012-10-21 00:46:06

标签: java macos text border jbutton

我正在制作一个圆形轨道的GUI,让两个角色可以移动。我使用按钮数组构建它,但是一旦我设置:

trackButtons[i].setBorderPainted(false);

无论我将其设置为什么尺寸,文字都会消失。注释掉该行允许文本再次显示,但显然边框显示,按钮再次显示为按钮。有没有办法隐藏按钮的边框但是允许显示文字?

main.java:

import javax.swing.JFrame;
public class main
{
    public static void main(String[] args)
    {
        UserFrame myFrame = new UserFrame();
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setSize(600,460);
        myFrame.setVisible(true);
        myFrame.setResizable(false);
    }//End of main
}//End of class main

UserFrame.java:

import javax.swing.*;
import java.awt.BorderLayout;           //Needed for Border Layout
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Color;                  //Needed for BLING!
import java.awt.Font;
import java.awt.Dimension;              //Needed for Button Dimensions

public class UserFrame extends JFrame
{
    //Variables
    private JLabel messageBox;

    //Layouts
    private JPanel northPanel;
    private JPanel eastPanel;
    private JPanel southPanel;
    private JPanel westPanel;
    private JPanel controlPanel;    //Contains all buttons and what not.

    // Board Pieces Grid
    private JButton[] trackButtons; //Ring of buttons forming track.

    //Game Functions
    private GameFunc gameFunctions;

    //Constructor
    public UserFrame()
    {
        //Construct GUI
        super("Tortoise and Hare Race");
        setLayout(new BorderLayout());

        //Variables
        Color myGreen = new Color(70, 190, 102);
        Font myFont = new Font("Arial", Font.PLAIN, 8);

        //Set size of Button Array
        trackButtons = new JButton[100];
        northPanel = new JPanel();
        eastPanel = new JPanel();
        southPanel = new JPanel();
        westPanel = new JPanel();

        //Set Layout
        northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.LINE_AXIS));
        eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.PAGE_AXIS));
        southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.LINE_AXIS));
        westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.PAGE_AXIS));

        for(int i=0; i<50; i++)
        {
            trackButtons[i] = new JButton();
            trackButtons[i].setMinimumSize(new Dimension(20, 20));
            trackButtons[i].setPreferredSize(new Dimension(20, 20));
            trackButtons[i].setMaximumSize(new Dimension(20, 20));
            trackButtons[i].setSize(20,20);
            trackButtons[i].setMargin(new Insets(0,0,0,0));
            trackButtons[i].setBackground(myGreen);
            trackButtons[i].setOpaque(true);
            trackButtons[i].setBorderPainted(false);
            trackButtons[i].setFont(myFont);
            //DEBUG
            trackButtons[i].setText(""+i);
            if(i<30)
                northPanel.add(trackButtons[i]);
            else if(i<50)
                eastPanel.add(trackButtons[i]);
        }

        //Flip count for south and west panel to maintain order!
        for(int i=99; i>=50; i--)
        {
            trackButtons[i] = new JButton();
            trackButtons[i].setMinimumSize(new Dimension(20, 20));
            trackButtons[i].setPreferredSize(new Dimension(20, 20));
            trackButtons[i].setMaximumSize(new Dimension(20, 20));
            trackButtons[i].setMargin(new Insets(0,0,0,0));
            trackButtons[i].setBackground(myGreen);
            trackButtons[i].setOpaque(true);
            trackButtons[i].setBorderPainted(false);
            trackButtons[i].setFont(myFont);
            //DEBUG
            trackButtons[i].setText(""+i);
            if(i>=80)
                westPanel.add(trackButtons[i]);
            else if(i>=50)
                southPanel.add(trackButtons[i]);
        }


        //Add Panels to respective locations
        add(BorderLayout.NORTH, northPanel);
        add(BorderLayout.EAST, eastPanel);
        add(BorderLayout.SOUTH, southPanel);
        add(BorderLayout.WEST, westPanel);
    }//End of constructor method
}//End of userFrame class

提前致谢!

编辑:我在Mac上,可能是Java正在做一些特定于操作系统的时髦吗?这是一个screenshot

1 个答案:

答案 0 :(得分:0)

我在Mac上玩了一段时间,看到了你看到的完全相同的问题。

Swing很敏感 - 在PC上这一切都很好(虽然按钮尺寸有点不对)。

我确实得到了它的工作 - 有点儿。尝试使用JLabel而不是JButton(唯一的其他变化是注释掉对setMargin的调用并将horizo​​ntalTextAlignment设置为CENTER。

我仍然说,一般情况下,在排除故障时首先要做的是确保它在正确的线程中,但它很少有帮助 - 它只是有一次它确实有帮助,它将修复最奇怪的问题...