gpt4 book ai didi

java - 如何将组件限制在某个位置

转载 作者:行者123 更新时间:2023-12-01 13:28:56 30 4
gpt4 key购买 nike

我正在使用 swing 创建幻灯片并尝试将其放置在 JFrame 的中心。幻灯片效果很好,但它占据了整个中心部分。我如何将其限制在横幅下的一个小地方?

这是它的外观图片。

Here's a picture of what it looks like.

这是我想要的图片

Here's a picture of what I want it to look like

这就是我到目前为止所拥有的,你运行这个类(class)。

package com.RBV2.engine;

import javax.swing.*;

import com.RBV2.Settings;
import com.RBV2.panels.News;
import com.RBV2.panels.SlideShow;
import com.RBV2.panels.SlideShow.MovingPanel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import java.io.*;
/*
* Author Jon & David
*
*/
@SuppressWarnings("serial")
public class Client extends JFrame implements ActionListener {
private JPanel bottomPanel, news;
private JButton launch;
private JLabel loading, banner;
private MovingPanel slideshow;

protected boolean updated = false;

private void createLayout() {
createPanel();
addPanel();
setVisible(true);
}

private void createPanel() {
bottomPanel = new JPanel(new BorderLayout());
news = new News();
slideshow = new SlideShow.MovingPanel();
launch = new JButton(new URL("http://www.runerebellion.com/clientImages/launch.png"));
loading = new JLabel(new URL("http://www.runerebellion.com/clientImages/loader.gif"));
banner = new JLabel(new URL("http://www.runerebellion.com/clientImages/201457.gif"));
launch.addActionListener(this);
}

private void addPanel() {
setTitle("RuneRebellionV2 Launcher");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
//Bottom Panel
add(bottomPanel, BorderLayout.SOUTH);
bottomPanel.add(new JLabel(" Launcher, release " + Settings.version), BorderLayout.WEST);
bottomPanel.setBackground(Color.BLACK);
bottomPanel.add(launch, BorderLayout.EAST);
launch.setPreferredSize(new Dimension(120, 40));
//News Feed
add(news, BorderLayout.CENTER);
news.add(banner, BorderLayout.CENTER);
banner.setPreferredSize(new Dimension(500, 70));
//slideshow
slideshow.setPreferredSize(new Dimension(610, 331));
add(slideshow, BorderLayout.CENTER);
//Sets size
setSize(Settings.width, Settings.height);
}

public Client() throws IOException {
createLayout();
}

public static void main(String args[]) throws IOException {
final Client l = new Client();
l.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
l.setVisible(true);
}
});

}

这些特定的代码行调用幻灯片(我注释掉了具体位置)

 private void addPanel() {
setTitle("RuneRebellionV2 Launcher");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
//Bottom Panel
add(bottomPanel, BorderLayout.SOUTH);
bottomPanel.add(new JLabel(" Launcher, release " + Settings.version), BorderLayout.WEST);
bottomPanel.setBackground(Color.BLACK);
bottomPanel.add(launch, BorderLayout.EAST);
launch.setPreferredSize(new Dimension(120, 40));
//News Feed
add(news, BorderLayout.CENTER);
news.add(banner, BorderLayout.CENTER);
banner.setPreferredSize(new Dimension(500, 70));
//slideshow here
slideshow.setPreferredSize(new Dimension(610, 331));
add(slideshow, BorderLayout.CENTER);
//Sets size
setSize(Settings.width, Settings.height);
}

这是我的幻灯片类(class),您可能也需要这个。

package com.RBV2.panels;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class SlideShow extends JFrame {
public static class MovingPanel extends JPanel {
public int i = 0;

public MovingPanel() {
Timer timer = new Timer(3000, new TimerListener());
timer.start();
}

protected void paintComponent(Graphics x) {
super.paintComponent(x);
int y = i % 25;

Image showImg = new ImageIcon("bin/slide/" + y + ".png").getImage();
x.drawImage(showImg, 0, 0, getWidth(), getHeight(), 0, 0, 610, 331, null);
}

class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
i++;
repaint();
if (i >= 5)
i = 1;
}
}

}
}

所以我的问题是如何将幻灯片限制在横幅下方正中心的框中。

最佳答案

看看这张图片(来自 Border layout Tutorial )

enter image description here

由于您仅将面板添加到布局管理器的中心和南部 (PAGE_END) 区域,因此没有其他组件可以阻止中心面板尽可能地拉伸(stretch)。

注意教程中的相关句子

If the window is enlarged, the center area gets as much of the available space as possible. The other areas expand only as much as necessary to fill all available space. Often a container uses only one or two of the areas of the BorderLayout object — just the center, or the center and the bottom.

您需要在另一侧添加空白面板,或者使用中心面板内的空白边框。请参阅此处 how to use borders .

关于java - 如何将组件限制在某个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21663590/

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