我想製作一種策略遊戲,並且我無法獲得JPanel來填充整個JFrame。我已經看過論壇,其他沒有關於此的帖子對我有所幫助。這裏是我的代碼:JPanel沒有正確調整
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class FCG extends JPanel{
static JFrame frame = new JFrame("Fleet Commander Greg");
static JPanel panel = new JPanel();
static JLabel ship = new JLabel();
public static void main(String[] args) {
ship.setIcon(new ImageIcon("/Users/Alexander/workspace/Fleet Commander Greg/src/res/sprite_1.png"));
ship.setBounds(32,32,0,0);
panel.add(ship);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1800,900);
frame.setVisible(true);
}
}
任何幫助將不勝感激,謝謝。 - 超級良好的編碼器,將某一天統治宇宙
編輯: 好了,我懂了工作,(有點),但仍然有框架的頂部和圖像的頂部之間的一些景觀。這是我的主要方法:
stars.setIcon(new ImageIcon("/Users/Alexander/workspace/Destroy the World/res/stars_1.png"));
stars.setBounds(0,0,400,500);
panel.add(stars);
frame.setLayout(new BorderLayout());
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 500);
frame.setVisible(true);
我不知道它爲什麼這樣做,它應該從我所見過的很好。 謝謝你 - 不是很好打碼機如果你想在JPanel中充滿整個JFrame的人永遠接管宇宙
我不明白你的問題。您將包含圖像的標籤添加到框架。標籤確實佔據了所有的空間。但是,圖像將以其實際大小繪製。即圖像不會自動縮放以填充幀的大小。 – camickr