2017-05-13 44 views
0

我有一個奇怪的白色條紋(見下文)出現在我的背景圖像上。代碼很簡單。如何擺脫白色條紋?JFrame奇怪的白色條紋背景圖像

enter image description here

//Graphics side of the game 
public class GUI extends JFrame { 

    private final int larghezza = 1280; 
    private final int altezza = 720; 
    private final String name = "Sette e Mezzo"; 

    private final ImageIcon backgroundImage; 
    private JLabel bgImageLabel; 
    private JPanel backgroundPanel, borderLayoutPanel, topGridLayout, botGridLayout; 

    public GUI() { 
     backgroundImage = new ImageIcon ("assets/background.png"); 

     bgImageLabel = new JLabel (backgroundImage); 

     //Panels 
     borderLayoutPanel = new JPanel (new BorderLayout()); 
     topGridLayout = new JPanel (new GridLayout (1, 3)); 
     botGridLayout = new JPanel (new GridLayout (1, 3)); 
     backgroundPanel = new JPanel(); 
     backgroundPanel.add (bgImageLabel); 

     //Frame 
     this.setName (name); 
     this.setPreferredSize (new Dimension(larghezza, altezza)); 
     this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 

     //Adding to frame and panels 
     borderLayoutPanel.add (topGridLayout, BorderLayout.NORTH); 
     borderLayoutPanel.add (botGridLayout, BorderLayout.SOUTH); 

     this.add (borderLayoutPanel); 
     this.add (backgroundPanel); 

     this.pack(); 
     this.setLocationRelativeTo (null); 
     this.setVisible (true); 
    } 
} 
+0

1)爲了更好地幫助越早,張貼[MCVE]或[簡要,獨立的,正確的示例](http://www.sscce.org/)。 (例如,爲了完成上面的代碼需要導入和一個主要的方法)2)*「'新的ImageIcon(」assets/background.png「);'」*一個例子獲取圖像的方法是以熱點鏈接到[本問答](http://stackoverflow.com/q/19209650/418556)中看到的圖像。 –

+0

.. 3)順便說一句 - 這不是白色的,它是RGB(255,250,254)。 –

回答

1

不要使用setPreferredSize()當你真正的意思是overridegetPreferredSize()。在這種情況下,指定的Dimension可能不會與相當匹配的大小爲"assets/background.png"。這允許顯示另一個面板的某個部分,可能是backgroundPanel

在下面的例子,

  • JPanel的默認佈局是FlowLayout,它有一個「默認是5個單位的水平和垂直間隔。」 Color.blue觸摸使得缺口突出;調整封閉幀以查看行爲。

  • 由於JFrame的默認佈局爲BorderLayout,因此根本不需要borderLayoutPanel

  • 因爲兩個GridLayout面板沒有內容,所以它們仍然不可見。向每個添加內容或覆蓋每個內容以查看效果。

  • 構建和操作Swing GUI對象只有event dispatch thread上。

image

import java.awt.*; 
import java.net.URL; 
import javax.swing.*; 

public class GUI { 

    private static final String TITLE = "Title"; 
    private static ImageIcon IMAGE_ICON; 

    private void display() { 
     //Panels 
     JPanel topGridLayout = new JPanel(new GridLayout(1, 3)); 
     JPanel botGridLayout = new JPanel(new GridLayout(1, 3)); 
     JPanel backgroundPanel = new JPanel(); 
     backgroundPanel.setBackground(Color.blue); 
     backgroundPanel.add(new JLabel(IMAGE_ICON)); 

     //Frame 
     JFrame f = new JFrame(TITLE); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Add components 
     f.add(topGridLayout, BorderLayout.NORTH); 
     f.add(backgroundPanel); 
     f.add(botGridLayout, BorderLayout.SOUTH); 

     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) throws Exception { 
     IMAGE_ICON = new ImageIcon(new URL("http://i.imgur.com/mowekvC.jpg")); 
     EventQueue.invokeLater(new GUI()::display); 
    } 
} 
+0

我幾乎可以看到一個浮在豆子上面的杯子。 – trashgod