您好,我正在尝试在 JFrame 中设置 JPanel 的大小,但我无法实现此目的。我怎样才能达到我想要的输出?
这是我的代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Sample extends JFrame
{
private JPanel panel;
private JLabel label1;
public Sample()
{
panel = new JPanel();
panel.setBackground(Color.YELLOW);
panel.setBounds(100, 100, 100, 100);
ImageIcon icon1 = new ImageIcon("E:\\image\\ZooDelhi\\1.jpg");
label1 = new JLabel(icon1);
label1.setLocation(0,0);
panel.add(label1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(panel);
this.setSize(500,500);
this.setVisible(true);
setLayout(null);
}
public static void main (String[] args) {
new Sample();
}
}
提前致谢。
最佳答案
我认为有或没有布局并不重要。没有布局不会影响改变 JPanel 的大小。 尝试使用这个:
myPanel.setPreferredSize(new Dimension(500, 500));
希望对您有所帮助;)
关于java - 如何在空布局中设置jpanel的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24904060/