2017-05-13 41 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