gpt4 book ai didi

java - Jpanel 内缺少 Jbutton 行

转载 作者:行者123 更新时间:2023-12-01 18:03:26 25 4
gpt4 key购买 nike

谁能告诉我为什么 JFrame 缺少一行按钮?我正在尝试制作一个存储卡游戏,您单击按钮,它就会被图像替换,但有些按钮没有显示?我想这和我的布局有关。应该有一个 4x4 的可点击 jbutton。

enter image description here

package memoryCard;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class gameBoard {

public void newGame() throws IOException

{
//MAIN FRAMES
JFrame mainFrame = new JFrame("Wagner's Memory");
mainFrame.setSize (800, 650);
mainFrame.setLocationRelativeTo(null);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);

//Buttons
JButton a1 = new JButton("");
JButton a2 = new JButton("");
JButton a3 = new JButton("");
JButton a4 = new JButton("");
JButton b1 = new JButton("");
JButton b2 = new JButton("");
JButton b3 = new JButton("");
JButton b4 = new JButton("");
JButton c1 = new JButton("");
JButton c2 = new JButton("");
JButton c3 = new JButton("");
JButton c4 = new JButton("");
JButton d1 = new JButton("");
JButton d2 = new JButton("");
JButton d3 = new JButton("");
JButton d4 = new JButton("");

//images

//penguin.jpg (a1 & d3)



//jax.jpg (a2 & c1)

String pathJ = "jax.jpg";
File fileJ = new File(pathJ);
BufferedImage jax = ImageIO.read(fileJ);
ImageIcon imgJ = new ImageIcon(jax);

//blink.jpg(a3 & d4)
String pathB = "blink.jpg";
File fileB = new File(pathB);
BufferedImage blink = ImageIO.read(fileB);
ImageIcon imgB = new ImageIcon(blink);


//mona.jpg(a4 & c4)
String pathM = "mona.jpg";
File fileM = new File(pathM);
BufferedImage mona = ImageIO.read(fileM);
ImageIcon imgM = new ImageIcon(mona);

//beer.jpg(b1 & c2)
String pathBeer = "beer.jpg";
File fileBeer = new File(pathBeer);
BufferedImage beer = ImageIO.read(fileBeer);
ImageIcon imgBeer = new ImageIcon(beer);

//jesus.jpg(b2 & c3)
String pathJes = "jesus.jpg";
File fileJes = new File(pathJes);
BufferedImage jesus = ImageIO.read(fileJes);
ImageIcon imgJes = new ImageIcon(jesus);


//bball.png(b4 & d2)
String pathBB = "bball.png";
File fileBB = new File(pathBB);
BufferedImage bball = ImageIO.read(fileBB);
ImageIcon imgBB = new ImageIcon(bball);


//cullen.jpg(b3 & d1)
String pathC = "cullen.jpg";
File fileC = new File(pathC);
BufferedImage cullen = ImageIO.read(fileC);
ImageIcon imgC = new ImageIcon(cullen);


//jpanels

JPanel panel1 = new JPanel(new BorderLayout());
panel1.add(a1, BorderLayout.CENTER);
JPanel panel2 = new JPanel(new BorderLayout());
panel2.add(a2, BorderLayout.CENTER);
JPanel panel3 = new JPanel(new BorderLayout());
panel3.add(a3, BorderLayout.CENTER);
JPanel panel4 = new JPanel(new BorderLayout());
panel4.add(a4, BorderLayout.CENTER);
JPanel panel5 = new JPanel(new BorderLayout());
panel5.add(b1, BorderLayout.CENTER);
JPanel panel6 = new JPanel(new BorderLayout());
panel6.add(b2, BorderLayout.CENTER);
JPanel panel7 = new JPanel(new BorderLayout());
panel7.add(b3, BorderLayout.CENTER);
JPanel panel8 = new JPanel(new BorderLayout());
panel8.add(b4, BorderLayout.CENTER);
JPanel panel9 = new JPanel(new BorderLayout());
panel9.add(c1, BorderLayout.CENTER);
JPanel panel10 = new JPanel(new BorderLayout());
panel10.add(c2, BorderLayout.CENTER);
JPanel panel11 = new JPanel(new BorderLayout());
panel11.add(c3, BorderLayout.CENTER);
JPanel panel12 = new JPanel(new BorderLayout());
panel12.add(c4, BorderLayout.CENTER);
JPanel panel13 = new JPanel(new BorderLayout());
panel13.add(b1, BorderLayout.CENTER);
JPanel panel14 = new JPanel(new BorderLayout());
panel14.add(b2, BorderLayout.CENTER);
JPanel panel15 = new JPanel(new BorderLayout());
panel15.add(b3, BorderLayout.CENTER);
JPanel panel16 = new JPanel(new BorderLayout());
panel16.add(b4, BorderLayout.CENTER);

//CONTAINER & ADDS
Container content = mainFrame.getContentPane();
content.setLayout(new GridLayout(4,4));
content.add(panel1);
content.add(panel2);
content.add(panel3);
content.add(panel4);
content.add(panel5);
content.add(panel6);
content.add(panel7);
content.add(panel8);
content.add(panel9);
content.add(panel10);
content.add(panel11);
content.add(panel12);
content.add(panel13);
content.add(panel14);
content.add(panel15);
content.add(panel16);

content.setVisible(true);


//ARRAY
JButton buttonArray1 [] = {a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4, d1, d2, d3, d4};


for (int i = 0; i < buttonArray1.length; i++) {
buttonArray1[i].addActionListener(new ActionListener() { //add an action listener to the current button
@Override
public void actionPerformed(ActionEvent e)
{
int d = 0;
d++;

if (e.getSource() == a1)
{

try {
String pathP = "penguin.jpg";
File fileP = new File(pathP);
BufferedImage peng;

peng = ImageIO.read(fileP);

ImageIcon imgP = new ImageIcon(peng);

JLabel imgLabel = new JLabel(imgP);
panel2.add(imgLabel);
content.add(panel1);
panel1.revalidate();
panel1.repaint();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}

}


});
}
}
public class wtf extends JPanel{
@Override
protected void paintComponent(Graphics g) {

super.paintComponent(g);

}
}
}

最佳答案

一个组件只能有一个父组件。

您尝试将“b”按钮添加到两个不同的面板,因此这些按钮从第二个面板中删除。

最后一个面板应显示“d”按钮。

最好使用循环来创建和显示按钮。那么游戏可以是任意大小的网格。

关于java - Jpanel 内缺少 Jbutton 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38858297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com