【发布时间】:2015-02-08 09:22:33
【问题描述】:
它说构造函数 JPanel(ImageIcon) 是未定义的。
这是我的部分代码
public Method_1(Test aa) {
this.aa = aa;
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 730, 540);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//JPanel panel = new JPanel();
BufferedImage myPicture = ImageIO.read(new File("method_1.png"));
JPanel picLabel = new JPanel(new ImageIcon(myPicture));
add(picLabel);
picLabel.setBounds(12, 34, 369, 175);
【问题讨论】:
-
避免使用
null布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正 -
使用
JPanel#add和JLabel,将标签添加到面板。见How to Use Labels -
错误消息准确地说明了问题所在:JPanel 没有将单个 ImageIcon 作为参数的构造函数。
-
setBounds(100, 100, 730, 540);pack()更好(在遵循@MadProgrammer 的明智建议并添加组件之后)。 -
@AndrewThompson 这一行
JPanel picLabel = new JPanel(new ImageIcon(myPicture));