我有三个课程:一个是主要课程:
public class Thera {
public static void main(String[] args) {
TheraGUI start = new TheraGUI();
}
}
一个是GUI
public class TheraGUI extends JFrame {
public TheraGUI(){
Board board = new Board();
this.setLayout(new GridBagLayout());
this.setTitle("Thera");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.add(board);
this.pack();
}
}
最后是我的董事会:
package thera;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Board extends JPanel {
JLayeredPane layeredpane;
JPanel board;
JLabel piece;
int xAdj;
int yAdj;
private static final String imageFolderPath = "src/resources/images/";
Dimension dimension = new Dimension(500, 500);
public Board(){
//create the layered pane
layeredpane = new JLayeredPane();
layeredpane.setPreferredSize(dimension);
this.add(layeredpane);
//create the Board
board = new JPanel();
board.setLayout(new GridLayout(7,9));
board.setPreferredSize(dimension);
layeredpane.add(board, JLayeredPane.DEFAULT_LAYER);
for(int c = 0; c < 7; c++){
for(int r = 0; r < 9; r++){
JPanel square = new JPanel();
square.setLayout(new BorderLayout());
square.setBackground(new Color(255, 204, 051));
board.add(square);
}
}
}
}
我的问题:我的主板不会出现! 附加信息:主要布局是gridbaglayout,在单元格中我添加了一个JPanel,在JPANel中我添加了一个分层窗格。
答案 0 :(得分:0)
您在设置可见性后添加了一个组件(从而更改了层次结构)。对于事情的运作方式,Swing有点迂腐。
public class TheraGUI extends JFrame {
public TheraGUI(){
Board board = new Board();
this.setLayout(new GridBagLayout());
this.setTitle("Thera");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// note the order
this.add(board);
this.pack();
this.setVisible(true);
}
}
答案 1 :(得分:0)
分层窗格使用空布局,这意味着您负责设置添加到分层面板的组件的大小/位置:
board.setPreferredSize(dimension);
board.setSize(dimension); // added
请注意,您不应该使用硬编码首选大小。您应该创建一个表示方块的类,然后覆盖该类的getPreferredSize()
方法。