我需要幫助,因爲我的JPanel組件不顯示在另一個組件中。我曾經使用過這種佈局,所以我有一些經驗,但在這種情況下仍然存在問題。請不要回答使用其他佈局管理器。我的大部分項目都基於GridBagLayout。GridBagLayout不顯示JPanel組件
所以這是我的父容器:
public class MainArea extends WorkArea
{
private static final long serialVersionUID = 1L;
private GridBagConstraints gbc;
private LogoPanel logo;
public MainArea()
{
gbc = new GridBagConstraints();
logo = new LogoPanel();
JTextField tf = new JTextField(20);
gbc.gridx = 0;
gbc.gridy = 0;
add(logo,gbc);
gbc.gridx = 0;
gbc.gridy = 1;
add(tf,gbc);
}
}
佈局管理器在工作區類集,它是做肯定的權利。 JTextField我只添加了測試,它顯示正確不像標誌。這裏是標識代碼:
public class LogoPanel extends JPanel
{
private BufferedImage image;
public LogoPanel()
{
File imageFile = new File("images/Logo.jpg");
try
{
image = ImageIO.read(imageFile);
}
catch (IOException e)
{
System.err.println("Blad odczytu logo");
e.printStackTrace();
}
}
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
Screen screen = Screen.getInstance();
g2d.drawImage(image, screen.getScreenWidth()/2-200, screen.getScreenHeight()/2-100, this);
}
}
請幫助我。我不知道這裏可能有什麼問題。我也嘗試重畫方法和添加後,但沒有結果驗證。
你在哪裏調用'super.paintComponent(g)'?你確定它能夠很好地加載圖像,如果沒有,請看看這個主題,如何[添加圖像到你的Java項目](http://stackoverflow.com/a/9866659/1057230) –