2014-02-09 40 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