简体   繁体   中英

Why does my text disappear when I hide JButton borders in Java?

I'm making a GUI for a circular track for two characters to move around in. I've constructed it using a button array, but as soon as I set:

trackButtons[i].setBorderPainted(false);

The text disappears no matter what size I set it to. Commenting out that line allows the text to show again but obviously the border shows up and the buttons appear like buttons again. Is there any way to hide the border to the buttons but allow for text to appear?

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

Thanks in advance!

Edit: I'm on a Mac, could it be that Java is doing something funky that is OS specific? Here's a screenshot

I played with this for a while on the mac and saw the exact same problem that you saw.

Swing is touchy--on the PC this all works fine (Although the button sizes are a little wrong).

I did get it to work--kindof. Try using JLabel instead of JButton (The only other change is to comment out the calls to setMargin and set the horizontalTextAlignment to CENTER.

I still say that in general when troubleshooting swing the very first thing to do is always to ensure it's in the right thread, but it rarely helps--it's just that the one time it does help, it'll fix the strangest problems...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM