鉴于我在下面的代码,无论我在其中有任何其他错误,我怎样才能在一个面板上获得两个图像?我必须让这个程序每个面板显示 4 个图像 2 个,第一个面板并排放置,第二个面板能够拖动和调整大小?我真的不知道该怎么做。我可以用 4 个面板获得所有 4 张图像,但这不是任务所要求的。谢谢!!
import java.awt.*;
import javax.swing.*;
public class TwoPanels
{
//-----------------------------------------------------------------
// Creates and displays the primary application frame.
//-----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Label Demo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
ImageIcon icon1 = new ImageIcon ("coin.jpg");
ImageIcon icon2 = new ImageIcon ("coke.jpg");
ImageIcon icon3 = new ImageIcon ("shuttle.jpg");
ImageIcon icon4 = new ImageIcon ("sun.jpg");
JLabel label1, label2;
label1 = new JLabel ( icon1, SwingConstants.CENTER);
label1.setHorizontalTextPosition (SwingConstants.LEFT);
label1.setVerticalTextPosition (SwingConstants.BOTTOM);
label1 = new JLabel ( icon2, SwingConstants.CENTER);
label2 = new JLabel (icon3, SwingConstants.CENTER);
label2.setHorizontalTextPosition (SwingConstants.CENTER);
label2.setVerticalTextPosition (SwingConstants.BOTTOM);
label2 = new JLabel (icon4, SwingConstants.CENTER);
JPanel panel = new JPanel();
panel.setBackground (Color.cyan);
panel.setPreferredSize (new Dimension (960, 640));
panel.add (label1);
panel.add (label2);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}