gpt4 book ai didi

java - JFrame 背景图像顶部出现奇怪的白色条纹

转载 作者:行者123 更新时间:2023-12-02 13:11:09 25 4
gpt4 key购买 nike

我的背景图像顶部出现了一个奇怪的白色条纹(见下文)。代码非常简单。如何去掉白色条纹?

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);
}
}

最佳答案

当您确实想使用setPreferredSize()时,请不要使用override getPreferredSize()。在这种情况下,指定的Dimension可能完全“assets/background.png”的大小匹配。这允许显示另一个面板的某些部分,也许是backgroundPanel

在下面的示例中,

  • JPanel 的默认布局是 FlowLayout,它具有“默认 5 个单位的水平和垂直间隙”。淡淡的 Color.blue 使间隙更加突出;调整封闭框架的大小以查看行为。

  • 由于 JFrame 的默认布局是 BorderLayout,因此您可能根本不需要 borderLayoutPanel

    <
  • 由于两个 GridLayout 面板没有内容,因此它们保持不可见。向每个内容添加内容或重写每个内容中的 getPreferredSize() 以查看效果。

  • event dispatch thread 上构造和操作 Swing GUI 对象.

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);
}
}

关于java - JFrame 背景图像顶部出现奇怪的白色条纹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43951430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com