2016-03-12 119 views
1

我認爲所有的東西都寫在代碼中,我只需要知道如何分類不同的圖層。如何在ImageIcon前移動JLabel?

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.GraphicsDevice; 
import java.awt.GraphicsEnvironment; 
import java.awt.event.ActionEvent; 

import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.ImageIcon; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.KeyStroke; 

class FullSceenToggleAction extends AbstractAction { 

    private JFrame frame; 
    private GraphicsDevice fullscreenDevice; 

    public FullSceenToggleAction(JFrame frame) { 
     this(frame, GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()); 
    } 

    public FullSceenToggleAction(JFrame frame, GraphicsDevice fullscreenDevice) { 
     this.frame = frame; 
     this.fullscreenDevice = fullscreenDevice; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     frame.dispose(); 
     if (frame.isUndecorated()) { 
      fullscreenDevice.setFullScreenWindow(null); 
      frame.setUndecorated(false); 
     } else { 
      frame.setUndecorated(true); 
      fullscreenDevice.setFullScreenWindow(frame); 
     } 
     frame.setVisible(true); 
     frame.repaint(); 
    } 

} 

public class Main { 

    public static final void addKeyBinding(JComponent c, String key, final Action action) { 
     c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), key); 
     c.getActionMap().put(key, action); 
     c.setFocusable(true); 
    } 

    public static void main(String[] args) { 
     final JFrame frame = new JFrame("Fullscreen Toggle Test"); 
     Container contentPane = frame.getContentPane(); 
     contentPane.add(new JLabel("Hey"), BorderLayout.CENTER); 
     frame.add(new JLabel(new ImageIcon("C:/Users/SamBr/Pictures/DesktopBackgrounds/image.png"))); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(960, 600); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setTitle("Virtual World"); 
     addKeyBinding(frame.getRootPane(), "F11", new FullSceenToggleAction(frame)); 
    } 

} 

當我運行的代碼,我看到的形象,但在它的前面或在屏幕上的任何地方,我不還是明白爲什麼沒有顯示我怎樣才能使它在正面看到沒有文字「嗨」圖片。

+0

在設計部分,右鍵單擊組件,應該有一個選項來獲取組件前後 –

+0

我已經告訴過您這個問題,但您從未向我回復過,好像您忽略了它。如果您不瞭解評論,請不要忽略它,請要求澄清。 –

+0

@Sam如果你使用的是佈局,你不能真正把你的組件放在你想要的確切位置(並且大多數佈局不允許部分重疊)。組件的位置將由佈局決定。如果您有興趣讓2張圖片重疊並將它們移動到任何想要的位置,您可以考慮自定義繪畫。你可以看看這裏:[圖像重疊](http:// stackoverflow。COM /問題/ 35711891 /如何到創建-A-背景和前景圖像 - 這-重疊/ 35711894#35711894)。 – user3437460

回答

3

我告訴過之前的原因 - 你正在向contentPane添加兩個組件,而第二個組件正在取代第一個組件。

注意的這兩行代碼添加一個組件到相同容器,Jframe的的contentPane:

contentPane.add(new JLabel("Hey"), BorderLayout.CENTER); 
    frame.add(new JLabel(
      new ImageIcon("C:/Users/SamBr/Pictures/DesktopBackgrounds/image.png"))); 

由於按照the JFrame API

爲方便起見,該加載,這個類的remove,setLayout方法被重寫,這樣它們就可以將調用委託給ContentPane的相應方法。

此外,contentPane中默認使用BorderLayout的,如果你不使用第二個參數常量添加組件,該組件在默認情況下加入到BorderLayout.CENTER位置。

如果你想同時顯示,那麼無論

  1. 從容器中默認使用不同佈局的borderlayout
  2. 保持BorderLayout的,但增加你的兩個JLabel的兩個不同的BorderLayout的位置,如BorderLayout的。 CENTER,BorderLayout.PAGE_START,...
  3. 使用的JLayeredPane持有層
  4. 繪製圖像中的繪圖JPanel的paintComponent的方法裏面,並添加「嘿」的JLabel來此JPanel
  5. ....其他解決方案。
+1

(1+)表示持久性。 – camickr

1

誰確切地知道你在做什麼。

但這裏是在根據您的需求不同位置的標籤上顯示文本的一些例子:

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

public class LabelImageText extends JPanel 
{ 
    public LabelImageText() 
    { 
     JLabel label1 = new JLabel(new ColorIcon(Color.ORANGE, 100, 100)); 
     label1.setText("Easy Way"); 
     label1.setHorizontalTextPosition(JLabel.CENTER); 
     label1.setVerticalTextPosition(JLabel.CENTER); 
     add(label1); 

     // 

     JLabel label2 = new JLabel(new ColorIcon(Color.YELLOW, 200, 150)); 
     label2.setLayout(new BoxLayout(label2, BoxLayout.Y_AXIS)); 
     add(label2); 

     JLabel text = new JLabel("More Control"); 
     text.setAlignmentX(JLabel.CENTER_ALIGNMENT); 
     label2.add(Box.createVerticalGlue()); 
     label2.add(text); 
     label2.add(Box.createVerticalStrut(10)); 

     // 

     JLabel label3 = new JLabel(new ColorIcon(Color.GREEN, 200, 150)); 
     label3.setLayout(new GridBagLayout()); 
     add(label3); 

     JLabel text3 = new JLabel(); 
     text3.setText("<html><center>Text<br>over<br>Image<center></html>"); 
     text3.setLocation(20, 20); 
     text3.setSize(text3.getPreferredSize()); 
     label3.add(text3); 

     // 

     JLabel label4 = new JLabel(new ColorIcon(Color.CYAN, 200, 150)); 
     add(label4); 

     JTextPane textPane = new JTextPane(); 
     textPane.setText("Add some text that will wrap at your preferred width"); 
     textPane.setEditable(false); 
     textPane.setOpaque(false); 
     SimpleAttributeSet center = new SimpleAttributeSet(); 
     StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); 
     StyledDocument doc = textPane.getStyledDocument(); 
     doc.setParagraphAttributes(0, doc.getLength(), center, false); 
     textPane.setBounds(20, 20, 75, 100); 
     label4.add(textPane); 
    } 

    public static class ColorIcon implements Icon 
    { 
     private Color color; 
     private int width; 
     private int height; 

     public ColorIcon(Color color, int width, int height) 
     { 
      this.color = color; 
      this.width = width; 
      this.height = height; 
     } 

     public int getIconWidth() 
     { 
      return width; 
     } 

     public int getIconHeight() 
     { 
      return height; 
     } 

     public void paintIcon(Component c, Graphics g, int x, int y) 
     { 
      g.setColor(color); 
      g.fillRect(x, y, width, height); 
     } 
    } 

    private static void createAndShowUI() 
    { 
     JFrame frame = new JFrame("LabelImageText"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new LabelImageText()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowUI(); 
      } 
     }); 
    } 
} 

最簡單的辦法只是使用既圖標和文本的單個標籤。其他人使用其他組件並利用佈局管理器。