所以我試圖啓動一個圖形程序,我有一個JFrame,它擁有多個JPanel。 JPanel需要結合來創建1個圖像,但是當我運行我的程序時,我會看到圖像周圍的邊框。我無法區分邊框是由保存圖像的JLabel引起的,還是由於JPanel或佈局管理器引起的。如何刪除JLabel上的邊框?
如何刪除邊框?我需要更改佈局管理器嗎?如果是這樣如何?
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class StarryNight {
JFrame backGround;
JPanel rootPanel;
JLabel rootImage;
public StarryNight(){
backGround = new JFrame("Starry Starry Night");
backGround.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
backGround.setResizable(false);
backGround.setSize(1000,667);
backGround.getContentPane().setBackground(Color.BLACK);
backGround.setLayout(new BoxLayout(backGround.getContentPane(),BoxLayout.Y_AXIS));
rootPanel = new JPanel();
rootPanel.setSize(1000, 667);
rootPanel.setBackground(Color.BLUE);;
rootImage = new JLabel();
rootImage.setIcon(new ImageIcon(getClass().getResource("Starry Night.jpg")));
rootPanel.add(rootImage);
JPanel jap = new JPanel();
jap.setSize(1000,100);
jap.setBackground(Color.GREEN);
backGround.add(rootPanel);
backGround.add(jap);
backGround.pack();
backGround.setVisible(true);
}
private static void runGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
StarryNight ssn= new StarryNight();
}
public static void main(String args[]){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
runGUI();
}
});
}
}
AFAIK,默認情況下JLabel沒有邊框。問題在別處。 –
請勿設置尺寸。讓圖像,組件和佈局爲您設置尺寸。這可能是你的問題 - 很難說,因爲我們沒有與我們的圖像。 –