1

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

trackButtons[i].setBorderPainted(false);

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

主.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

用户框架.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 正在做一些特定于操作系统的时髦的事情吗?这是一个屏幕截图

4

1 回答 1

0

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

Swing 很敏感——在 PC 上这一切都很好(虽然按钮大小有点不对)。

我确实让它工作了——有点。尝试使用 JLabel 而不是 JButton(唯一的其他更改是注释掉对 setMargin 的调用并将 Horizo​​ntalTextAlignment 设置为 CENTER。

我仍然说,一般来说,当对摇摆进行故障排除时,首先要做的总是确保它在正确的线程中,但它很少有帮助——只是有一次它确实有帮助,它会解决最奇怪的问题。 .

于 2012-10-21T02:14:46.703 回答