gpt4 book ai didi

Java - 如何正确添加多个带有背景图像的面板?

转载 作者:行者123 更新时间:2023-11-30 11:24:52 25 4
gpt4 key购买 nike

我正在尝试编写一个益智游戏,其中的内容显示在两个不同的面板中:

-SplitTable:在这个面板中,几个标签将显示一些相关数据。 SplitPanel 大小的一半,垂直,在屏幕左侧。

-SplitPanel:这里将在现有图像之上的面板中显示拼图。

-SplitMainView:这里所有东西都放在一起

-SplitCustomPanel:自定义面板的代码

显然一切正常,但如果您仔细观察并向面板添加一些边框,您会发现存在某种重叠。

我应该怎么做才能解决这个问题并让它们占用正确的空间?

public SplitTable(SplitModel model){
this.model = model;
setLayout(new GridLayout(1,6));

java.net.URL urls = ClassLoader.getSystemResource("Imagenes/vertical.jpg");
Toolkit kit = Toolkit.getDefaultToolkit();
Image back = kit.createImage(urls);
p = new SplitCustomPanel(back, 346, 900, 0, 0);
p.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2, true));
la1 = new JLabel(" ");
la2 = new JLabel(" ");
la3 = new JLabel(" ");
la4 = new JLabel(" ");
la5 = new JLabel(" ");
la6 = new JLabel(" ");
p.add(la1);
p.add(la2);
p.add(la3);
p.add(la4);
p.add(la5);
p.add(la6);
add(p);}


public SplitPanel(SplitModel model){
this.model = model;

setLayout(new GridLayout(1,6));

java.net.URL urls = ClassLoader.getSystemResource("Imagenes/tap1.jpg");
Toolkit kit = Toolkit.getDefaultToolkit();
Image back = kit.createImage(urls);
p = new SplitCustomPanel(back, 1200, 900, 300, 0);
p.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2, true));
la1 = new JLabel(" ");
la2 = new JLabel(" ");
la3 = new JLabel(" ");
la4 = new JLabel(" ");
la5 = new JLabel(" ");
la6 = new JLabel(" ");
p.add(la1);
p.add(la2);
p.add(la3);
p.add(la4);
p.add(la5);
p.add(la6);
add(p);}

public SplitMainView(SplitModel model){
super("Splitter");

java.net.URL urls = ClassLoader.getSystemResource("Imagenes/ese.jpg");
Toolkit kit = Toolkit.getDefaultToolkit();
Image imgs = kit.createImage(urls);
this.setIconImage(imgs);

this.model = model;
JPanel p = new JPanel();

p.setLayout(new GridBagLayout());

GridBagConstraints ct = new GridBagConstraints();
GridBagConstraints cp = new GridBagConstraints();

ct.fill = GridBagConstraints.BOTH;
cp.fill = GridBagConstraints.BOTH;

ct.weightx = 1.0;
ct.weighty = 1.0;
cp.weightx = 1.0;
cp.weighty = 1.0;

ct.gridheight = 8;
ct.gridwidth = 2;
ct.gridx = 0;
ct.gridy = 0;
cp.gridheight = 8;
cp.gridwidth = 8;
cp.gridx = 0;
cp.gridy = 2;

menu = new SplitMenuBar();
panel = new SplitPanel(model);
table = new SplitTable(model);

this.setJMenuBar(menu);
p.add(table, ct);
p.add(panel, cp);
this.add(p);


setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

公共(public)类 SplitCustomPanel 扩展 JPanel {

private Image back;
private int w;
private int h;
private int x;
private int y;

public SplitCustomPanel(Image backe, int w, int h, int x, int y) {
this.back = backe;
this.w = w;
this.h = h;
this.x = x;
this.y = y;
}


@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(back, x, y, w, h, this);
}

它是这样的: http://i.stack.imgur.com/An0hg.jpg

最佳答案

如图here ,使用 getSubimage() 沿像素边界切片原始图像。在example ,调整框架大小以查看无边界组件如何对齐。仅在需要时使用 drawImage() 合并片段。如果是这样,请调用 super.paintComponent() 以避免渲染瑕疵。

关于Java - 如何正确添加多个带有背景图像的面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20543054/

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