在 GridLayout 上方添加 JLabel
Adding a JLabel above a GridLayout
我很难包含 GridLayout,因此在其上方添加 JLabel 不会丢失任何东西。我尝试了一些不同的东西,但似乎仍然无法正确对齐。
这是一款井字游戏:
package assg01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToe extends JFrame {
private static final String TITLE = "Tic Tac Toe";
private static final int WIDTH = 700;
private static final int HEIGHT = 808;
private Container content;
private JLabel result;
private JButton[] cells;
private JButton exitButton;
private JButton initButton;
private CellButtonHandler[] cellHandlers;
private ExitButtonHandler exitHandler;
private InitButtonHandler initHandler;
private boolean noughts;
private boolean gameOver;
private Container panel;
private ImageIcon x,empty,o;
public TicTacToe() {
// Necessary initialization code
setTitle(TITLE);
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Get content pane
panel = getContentPane();
//content.setBackground(Color.blue.darker())
// Set layout
panel.setLayout(new GridLayout(3, 3, 5, 5));
// Create cells and handlers
cells = new JButton[9];
cellHandlers = new CellButtonHandler[9];
for (int i = 0; i < 9; i++) {
cells[i] = new JButton();
cellHandlers[i] = new CellButtonHandler();
cells[i].addActionListener(cellHandlers[i]);
}
// Create result label
//result.setForeground(Color.white);
// Add elements to the grid content pane
for (int i = 0; i < 9; i++) {
panel.add((cells[i]));
}
init();
}
public void init() {
// Initialize
JPanel topPanel = new JPanel();
add(topPanel,BorderLayout.NORTH);
JLabel myLabel = new JLabel ("Label") ;
topPanel.add(myLabel);
x=new ImageIcon("images/x.png");
empty=new ImageIcon("images/empty.png");
o=new ImageIcon("images/o.png");
noughts = true;
gameOver = false;
// Initialize text in buttons
for (int i = 0; i < 9; i++) {
char ch = (char) (i + 1);
cells[i].setIcon(empty);
cells[i].setName(""+ch);
}
// Initialize result label
setVisible(true);
}
I'm having a hard time with containing a GridLayout
so that adding a JLabel
above it won't throw off anything.
这里的技巧是组合布局:
- 创建一个
JPanel
和 BorderLayout
- 将
GridLayout
添加到 BorderLayout
的 CENTER
。
- 将
JLabel
添加到 BorderLayout
的 PAGE_START
。
另一种选择是使用 TitledBorder
(而不是 JLabel
)作为 GridLayout
。
我很难包含 GridLayout,因此在其上方添加 JLabel 不会丢失任何东西。我尝试了一些不同的东西,但似乎仍然无法正确对齐。
这是一款井字游戏:
package assg01;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToe extends JFrame {
private static final String TITLE = "Tic Tac Toe";
private static final int WIDTH = 700;
private static final int HEIGHT = 808;
private Container content;
private JLabel result;
private JButton[] cells;
private JButton exitButton;
private JButton initButton;
private CellButtonHandler[] cellHandlers;
private ExitButtonHandler exitHandler;
private InitButtonHandler initHandler;
private boolean noughts;
private boolean gameOver;
private Container panel;
private ImageIcon x,empty,o;
public TicTacToe() {
// Necessary initialization code
setTitle(TITLE);
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Get content pane
panel = getContentPane();
//content.setBackground(Color.blue.darker())
// Set layout
panel.setLayout(new GridLayout(3, 3, 5, 5));
// Create cells and handlers
cells = new JButton[9];
cellHandlers = new CellButtonHandler[9];
for (int i = 0; i < 9; i++) {
cells[i] = new JButton();
cellHandlers[i] = new CellButtonHandler();
cells[i].addActionListener(cellHandlers[i]);
}
// Create result label
//result.setForeground(Color.white);
// Add elements to the grid content pane
for (int i = 0; i < 9; i++) {
panel.add((cells[i]));
}
init();
}
public void init() {
// Initialize
JPanel topPanel = new JPanel();
add(topPanel,BorderLayout.NORTH);
JLabel myLabel = new JLabel ("Label") ;
topPanel.add(myLabel);
x=new ImageIcon("images/x.png");
empty=new ImageIcon("images/empty.png");
o=new ImageIcon("images/o.png");
noughts = true;
gameOver = false;
// Initialize text in buttons
for (int i = 0; i < 9; i++) {
char ch = (char) (i + 1);
cells[i].setIcon(empty);
cells[i].setName(""+ch);
}
// Initialize result label
setVisible(true);
}
I'm having a hard time with containing a
GridLayout
so that adding aJLabel
above it won't throw off anything.
这里的技巧是组合布局:
- 创建一个
JPanel
和BorderLayout
- 将
GridLayout
添加到BorderLayout
的CENTER
。 - 将
JLabel
添加到BorderLayout
的PAGE_START
。
另一种选择是使用 TitledBorder
(而不是 JLabel
)作为 GridLayout
。