我有一个 JTable
,我想制作一个 tic tac toe 主题的 table 。我已经设法摆脱了三个边界,但并不真正知道如何......
这是我的代码:
public class Table extends JPanel {
public static void main( String[] args ) {
JFrame jFrame = new JFrame();
JTable table = new JTable(3, 3);
table.setBounds(90, 20, 200, 200);
for(int i = 0; i < 3; i++) {
table.setRowHeight(i, 67);
}
table.setBackground(Color.black);
table.setGridColor(Color.white);
table.setForeground(Color.white);
// content pane so I can set the background
jFrame.getContentPane().add(table);
jFrame.getContentPane().setBackground(Color.black);
jFrame.setSize(400, 400);
jFrame.setLayout(null);
jFrame.setVisible(true);
}
}
我还有一些东西可以像设置值一样初始化表,但我认为这并不重要,但如果您愿意,我可以添加它。那么我怎样才能让表格的右边框也消失呢?
当我编译此代码时,顶部、左侧和底部边框不可见
最佳答案
您可以使用 setBorder
方法和 LineBorder
来填充它:
table.setGridColor(Color.white);
table.setBorder(BorderFactory.createLineBorder(table.getGridColor()));
顺便说一句,setLayout(null)
和 setBounds
是一种不好的做法,将来会给你带来困难(而且它对用户不友好) ,因为您无法调整框架大小并且无法在不同的屏幕尺寸下工作)。尝试use LayoutManagers并让他们为您完成工作。
此外,请查看 initial threads 。所有 Swing 应用程序都应该在自己的线程上运行,而不是在主线程中运行。
关于java - JTable只显示表格的右边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57999654/