Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将组件限制在特定位置_Java_Swing_Layout_Jframe - Fatal编程技术网

Java 如何将组件限制在特定位置

Java 如何将组件限制在特定位置,java,swing,layout,jframe,Java,Swing,Layout,Jframe,我正在使用swing创建一个幻灯片,并试图将其放置在JFrame的中心。幻灯片效果很好,但它占据了整个中间部分。我如何将它限制在旗帜下的一个小地方 这是一张它看起来像什么的图片 这是一张我想要的照片 这就是我到目前为止所做的,你来主持这个班 package com.RBV2.engine; import javax.swing.*; import com.RBV2.Settings; import com.RBV2.panels.News; import com.RBV2.panels.

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

这是一张它看起来像什么的图片

这是一张我想要的照片

这就是我到目前为止所做的,你来主持这个班

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

    }
这些特定的代码行调用幻灯片(我注释了确切的位置)

这是我的幻灯片课程,你可能也需要这个

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

}
}

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

看看这张照片(从)

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

请注意本教程的这一相关句子

如果窗口被放大,中心区域将获得尽可能多的可用空间。其他区域仅根据需要进行扩展,以填充所有可用空间。通常,容器仅使用BorderLayout对象的一个或两个区域-仅使用中心或中心和底部


您需要在另一侧添加空白面板,或者使用中间面板内的空白边框。请看这里。

我绘制了背景,然后在背景上绘制了图像

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

            Image showImg = new ImageIcon("bin/slide/" + y + ".png").getImage();
            super.paintComponent(x);
            x.drawImage((Settings.background).getImage(), 0, 0, getWidth(), getHeight(), this);
            x.drawImage(showImg, 360, 260, getWidth(), getHeight(), 0, 0, 110, 31, null);
}

为了编译它,您可以删除新闻组件,但仍然可能会将幻灯片放在正确的位置。要居中显示某个内容,请将其作为
GridBagLayout
中唯一没有任何约束的组件,如图所示。为了更快地获得更好的帮助,请发布一个(经过测试且可读性最低的完整示例)。如果是这样,我不需要让东西方透明吗?此外,我的背景图像被绘制为“新闻”,如果我将“新闻”设置为“页面开始”,则幻灯片放映组件根本不会显示。
protected void paintComponent(Graphics x) {
            super.paintComponent(x);
            int y = i % 25;

            Image showImg = new ImageIcon("bin/slide/" + y + ".png").getImage();
            super.paintComponent(x);
            x.drawImage((Settings.background).getImage(), 0, 0, getWidth(), getHeight(), this);
            x.drawImage(showImg, 360, 260, getWidth(), getHeight(), 0, 0, 110, 31, null);
}