我正在尝试编写一个益智游戏,其中的内容显示在两个不同的面板中:
-SplitTable:在此面板中,一些标签将显示一些相关数据。 SplitPanel大小的一半和垂直,在屏幕的左侧。
-SplitPanel:这里的拼图将显示在一个面板上,该面板将位于现有图像的顶部。
-SplitMainView:这里所有东西都放在一起
-SplitCustomPanel:自定义面板的代码
显然一切正常,但如果仔细观察并在面板上添加一些边框,就会发现存在某种重叠。
我该怎么做才能解决这个问题并让它们占据正确的空间?
public SplitTable(SplitModel model){
this.model = model;
setLayout(new GridLayout(1,6));
java.net.URL urls = ClassLoader.getSystemResource("Imagenes/vertical.jpg");
Toolkit kit = Toolkit.getDefaultToolkit();
Image back = kit.createImage(urls);
p = new SplitCustomPanel(back, 346, 900, 0, 0);
p.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2, true));
la1 = new JLabel(" ");
la2 = new JLabel(" ");
la3 = new JLabel(" ");
la4 = new JLabel(" ");
la5 = new JLabel(" ");
la6 = new JLabel(" ");
p.add(la1);
p.add(la2);
p.add(la3);
p.add(la4);
p.add(la5);
p.add(la6);
add(p);}
public SplitPanel(SplitModel model){
this.model = model;
setLayout(new GridLayout(1,6));
java.net.URL urls = ClassLoader.getSystemResource("Imagenes/tap1.jpg");
Toolkit kit = Toolkit.getDefaultToolkit();
Image back = kit.createImage(urls);
p = new SplitCustomPanel(back, 1200, 900, 300, 0);
p.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2, true));
la1 = new JLabel(" ");
la2 = new JLabel(" ");
la3 = new JLabel(" ");
la4 = new JLabel(" ");
la5 = new JLabel(" ");
la6 = new JLabel(" ");
p.add(la1);
p.add(la2);
p.add(la3);
p.add(la4);
p.add(la5);
p.add(la6);
add(p);}
public SplitMainView(SplitModel model){
super("Splitter");
java.net.URL urls = ClassLoader.getSystemResource("Imagenes/ese.jpg");
Toolkit kit = Toolkit.getDefaultToolkit();
Image imgs = kit.createImage(urls);
this.setIconImage(imgs);
this.model = model;
JPanel p = new JPanel();
p.setLayout(new GridBagLayout());
GridBagConstraints ct = new GridBagConstraints();
GridBagConstraints cp = new GridBagConstraints();
ct.fill = GridBagConstraints.BOTH;
cp.fill = GridBagConstraints.BOTH;
ct.weightx = 1.0;
ct.weighty = 1.0;
cp.weightx = 1.0;
cp.weighty = 1.0;
ct.gridheight = 8;
ct.gridwidth = 2;
ct.gridx = 0;
ct.gridy = 0;
cp.gridheight = 8;
cp.gridwidth = 8;
cp.gridx = 0;
cp.gridy = 2;
menu = new SplitMenuBar();
panel = new SplitPanel(model);
table = new SplitTable(model);
this.setJMenuBar(menu);
p.add(table, ct);
p.add(panel, cp);
this.add(p);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
公共类SplitCustomPanel扩展了JPanel {
private Image back;
private int w;
private int h;
private int x;
private int y;
public SplitCustomPanel(Image backe, int w, int h, int x, int y) {
this.back = backe;
this.w = w;
this.h = h;
this.x = x;
this.y = y;
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(back, x, y, w, h, this);
}