我正在为一个圆形轨道制作一个 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 正在做一些特定于操作系统的时髦的事情吗?这是一个屏幕截图