我似乎无法正确处理。该代码没有任何语法错误,但没有显示我想要显示的内容。这是我写的代码。预先感谢您的帮助。我想在游戏面板(子面板)中显示图像 jbuttons.. 那里应该有 9 个 jbuttons 但它没有显示。框架只是灰色的。我不知道如何让子面板显示。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class midterm extends JFrame
{
JPanel mainpanel;
JButton image1=new JButton();
JButton image2=new JButton();
JButton image3=new JButton();
JButton image4=new JButton();
JButton image5=new JButton();
JButton image6=new JButton();
JButton image7=new JButton();
JButton image8=new JButton();
JButton image9=new JButton();
JLabel text=new JLabel("Please select a photo above.");
public midterm()
{
super("Girls' Generation - I GOT A BOY 4th Korean Album");
setSize(700,700);
setLocation(200,30);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
panel();
}
public static void main(String[] args)
{
midterm frame=new midterm();
}
void panel()
{
mainpanel=new JPanel();
mainpanel.setLayout(new BorderLayout());
JPanel gamepanel=new JPanel();
gamepanel.setBorder(BorderFactory.createTitledBorder("Photo Puzzle"));
gamepanel.setLayout(new GridLayout(3,3));
ImageIcon sica = new ImageIcon("jung.jpg");
image1.setBorder(BorderFactory.createLineBorder(Color.blue));
image1.setIcon(sica);
image1.setSize(200,200);
image1.setVisible(true);
image1.setToolTipText("Jessica Jung Sooyeon");
gamepanel.add((image1));
ImageIcon yuri = new ImageIcon("kwon.jpg");
image2.setIcon(yuri);
image2.setSize(200,200);
image2.setVisible(true);
image2.setToolTipText("Kwon Yuri");
gamepanel.add(image2);
ImageIcon tiff = new ImageIcon("hwang.jpg");
image3.setIcon(tiff);
image3.setSize(200,200);
image3.setVisible(true);
image3.setToolTipText("Tiffany Hwang Miyoung");
gamepanel.add(image3);
ImageIcon sunny = new ImageIcon("lee.jpg");
image4.setIcon(sunny);
image4.setSize(200,200);
image4.setVisible(true);
image4.setToolTipText("Sunny Lee Sunkyu");
gamepanel.add(image4);
ImageIcon soo = new ImageIcon("choi.jpg");
image5.setIcon(soo);
image5.setSize(200,200);
image5.setVisible(true);
image5.setToolTipText("Choi Sooyoung");
gamepanel.add(image5);
ImageIcon hyo = new ImageIcon("kimh.jpg");
image6.setIcon(hyo);
image6.setSize(200,200);
image6.setVisible(true);
image6.setToolTipText("Kim Hyoyeon");
gamepanel.add(image6);
ImageIcon yoona = new ImageIcon("im.jpg");
image7.setIcon(yoona);
image7.setSize(200,200);
image7.setVisible(true);
image7.setToolTipText("Im Yoona");
gamepanel.add(image7);
ImageIcon taeng = new ImageIcon("kimt.jpg");
image8.setIcon(taeng);
image8.setSize(200,200);
image8.setVisible(true);
image8.setToolTipText("Kim Taeyeon");
gamepanel.add(image8);
ImageIcon hyunnie = new ImageIcon("seo.jpg");
image9.setIcon(hyunnie);
image9.setSize(200,200);
image9.setVisible(true);
image9.setToolTipText("Seohyun Seo Juhyun");
gamepanel.add(image9);
JPanel controls = new JPanel();
controls.setLayout(new BorderLayout());
controls.setBorder(BorderFactory.createTitledBorder("Control"));
JButton newGame = new JButton("New Game");
controls.add(newGame, BorderLayout.NORTH);
JButton exitGame = new JButton("Exit");
controls.add(exitGame, BorderLayout.SOUTH);
mainpanel.add(gamepanel, BorderLayout.CENTER);
mainpanel.add(controls, BorderLayout.EAST);
process jj = new process();
image1.addActionListener(jj);
image2.addActionListener(jj);
image3.addActionListener(jj);
image4.addActionListener(jj);
image5.addActionListener(jj);
image6.addActionListener(jj);
image7.addActionListener(jj);
image8.addActionListener(jj);
image9.addActionListener(jj);
}
class process implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if(e.getSource().equals(image1))
{
image2.setVisible(false);
image3.setVisible(false);
image4.setVisible(false);
image5.setVisible(false);
image6.setVisible(false);
image7.setVisible(false);
image8.setVisible(false);
image9.setVisible(false);
text.setVisible(false);
}
else if(e.getSource().equals(image2))
{
image1.setVisible(false);
image3.setVisible(false);
image4.setVisible(false);
image5.setVisible(false);
image6.setVisible(false);
image7.setVisible(false);
image8.setVisible(false);
image9.setVisible(false);
text.setVisible(false);
}
else if(e.getSource().equals(image3))
{
image2.setVisible(false);
image1.setVisible(false);
image4.setVisible(false);
image5.setVisible(false);
image6.setVisible(false);
image7.setVisible(false);
image8.setVisible(false);
image9.setVisible(false);
text.setVisible(false);
}
else if(e.getSource().equals(image4))
{
image2.setVisible(false);
image3.setVisible(false);
image1.setVisible(false);
image5.setVisible(false);
image6.setVisible(false);
image7.setVisible(false);
image8.setVisible(false);
image9.setVisible(false);
text.setVisible(false);
}
else if(e.getSource().equals(image5))
{
image2.setVisible(false);
image3.setVisible(false);
image4.setVisible(false);
image1.setVisible(false);
image6.setVisible(false);
image7.setVisible(false);
image8.setVisible(false);
image9.setVisible(false);
text.setVisible(false);
}
else if(e.getSource().equals(image6))
{
image2.setVisible(false);
image3.setVisible(false);
image4.setVisible(false);
image5.setVisible(false);
image1.setVisible(false);
image7.setVisible(false);
image8.setVisible(false);
image9.setVisible(false);
text.setVisible(false);
}
else if(e.getSource().equals(image7))
{
image2.setVisible(false);
image3.setVisible(false);
image4.setVisible(false);
image5.setVisible(false);
image6.setVisible(false);
image1.setVisible(false);
image8.setVisible(false);
image9.setVisible(false);
text.setVisible(false);
}
else if(e.getSource().equals(image8))
{
image2.setVisible(false);
image3.setVisible(false);
image4.setVisible(false);
image5.setVisible(false);
image6.setVisible(false);
image7.setVisible(false);
image1.setVisible(false);
image9.setVisible(false);
text.setVisible(false);
}
else if(e.getSource().equals(image9))
{
image2.setVisible(false);
image3.setVisible(false);
image4.setVisible(false);
image5.setVisible(false);
image6.setVisible(false);
image7.setVisible(false);
image8.setVisible(false);
image1.setVisible(false);
text.setVisible(false);
}
}
}
}