2014-02-09 25 views
0

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

下面是它的外观图片。

Here's a picture of what it looks like.

这里是什么,我希望它看起来像

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

这是我到目前为止,你运行这个类的图片。

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

这里是我的幻灯片类,你可能也需要这些。

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

} 
} 

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

+0

为了编译它,你可以删除新闻组件,而且还可能得到在正确的位置的幻灯片。 – RuneRebellion

+0

为了集中一些东西,把它作为'GridBagLayout'中没有约束的唯一组件,如[这里]所示[http://stackoverflow.com/questions/7180198/centering-a-jlabel-on-a-jpanel/ 7181197#7181197)。为了更快地获得更好的帮助,请发布[MCTaRE](http://stackoverflow.com/help/mcve)(最小完整测试和可读示例)。 –

回答

0

我画的背景,再涂上将图片的背景

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

看一看这张图(来自Border layout Tutorial

enter image description here

当你只添加面板的布局管理器的中部和南部(PAGE_END)地区有没有其他元件停止中央面板尽可能伸出。

注教程

如果被放大的窗口,中心区域的这个有关刑罚得到尽可能多的可用空间。其他区域只根据需要扩展以填充所有可用空间。容器通常只使用BorderLayout对象的一个​​或两个区域 - 只是中心或中心和底部。

您可能需要在其他面添加空白面板或在中心面板中使用空白边框。在这里看到关于how to use borders

+0

在这种情况下,我不得不使西方和东方的透明?此外,我的背景图像被绘制到新闻,如果我将新闻设置为page_start,幻灯片组件根本不显示。 – RuneRebellion