2014-02-07 77 views
2

我對Java很陌生並正在處理一個項目。作爲製作主菜單的測試,我一直在嘗試使用JFrame和Powerranger的圖片。我的問題是我已經嘗試了很多次,花了很多時間試圖讓我的遊戲和我的幫助按鈕到我的窗口左側。我已經嘗試製作4個網格,並放入一個空標籤,但它不會工作。 這是我的代碼:在JFrame中創建背景圖像時將JButton移動到左側

package be.kdg.AngryTanksKlad; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.io.IOException; 
import java.net.URL; 
import javax.imageio.ImageIO; 
import javax.swing.*; 

public class Main { 
    public static void main(String[] args) throws IOException { 
     final Image image = ImageIO.read(new URL("http://www.modernmom.com/sites/default/files/images/Power%2BRangers%2BPowerRangersMMPR.jpg")); 
     final JFrame frame = new JFrame(); 
     frame.add(new ImagePanel(image)); 
     frame.setSize(800, 600); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
    } 




@SuppressWarnings("serial") 
class ImagePanel extends JPanel { 
    private Image image; 
    private boolean tile; 
    private JPanel menu = new JPanel(new FlowLayout(FlowLayout.LEFT)); 

    ImagePanel(Image image) { 
    this.image = image; 
    this.tile = false; 
    final JButton play = new JButton("PLAY"); 
    final JButton help = new JButton("HELP"); 
    final JLabel empty = new JLabel(); 
    } 

    menu.setLayout(new GridLayout(4,4)); 

    menu.add(empty); 
    menu.add(play); 
    menu.add(help); 
    add(menu,BorderLayout.CENTER); 
    menu.setOpaque(false); 

    setVisible(true); 
}; 

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    if (tile) { 
     int iw = image.getWidth(this); 
     int ih = image.getHeight(this); 
     if (iw > 0 && ih > 0) { 
      for (int x = 0; x < getWidth(); x += iw) { 
       for (int y = 0; y < getHeight(); y += ih) { 
        g.drawImage(image, x, y, iw, ih, this); 
       } 
      } 
     } 
    } else { 
     g.drawImage(image, 0, 0, getWidth(), getHeight(), this); 
    } 
    } 
} 

enter image description here

+0

1)源代碼中的單個空白行空白*總是*足夠。 '{'之後或'}'之前的空行通常也是多餘的。 2)提供圖形用戶界面的ASCII藝術(或帶有簡單繪圖的圖像),因爲它應該以最小的尺寸出現並且(如果可調整大小)以額外的寬度/高度出現。 –

+0

..代碼不能幹淨地編譯。 :( –

+0

對不起,我還不是一個java嚮導;) –

回答

3

這樣的事情?

enter image description here

參見代碼中的註釋。

import java.awt.*; 
import java.awt.image.*; 
import java.awt.event.ActionEvent; 
import java.io.IOException; 
import java.net.URL; 
import javax.imageio.ImageIO; 
import javax.swing.*; 
import javax.swing.border.*; 

public class SillyNAmeForAMainClassGivenIHave100CalledMain { 

public static void main(String[] args) throws IOException { 
    final Image image = new BufferedImage(400,200,BufferedImage.TYPE_INT_RGB); 
    final JFrame frame = new JFrame(); 
    frame.add(new ImagePanel(image)); 
    //frame.setSize(800, 600); 
    frame.pack(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
} 
} 

@SuppressWarnings("serial") 
class ImagePanel extends JPanel { 
private Image image; 
private boolean tile; 
// add 5px spacing between buttons 
private JPanel menu = new JPanel(new GridLayout(4,4,5,5)); 

ImagePanel(Image image) { 
    super(new BorderLayout()); // default is FlowLayout! 
    this.image = image; 
    this.tile = false; 
    final JButton play = new JButton("PLAY"); 
    final JButton help = new JButton("HELP"); 

    menu.add(play); 
    menu.add(help); 

    JPanel buttonConstrain = new JPanel(new FlowLayout()); 
    // pad the top of it.. 
    buttonConstrain.setBorder(new EmptyBorder(20,5,5,5)); 
    buttonConstrain.setOpaque(false); 
    buttonConstrain.add(menu); 
    // it won't help to use a BorderLayout contraint when the default is FlowLayout! 
    // LINE_START = 'left' for left-to-right locales, right for the other 
    add(buttonConstrain,BorderLayout.LINE_START); 
    menu.setOpaque(false); 

    setVisible(true); 
} 

@Override 
public Dimension getPreferredSize() { 
    Dimension d = null; 
    if (image != null) { 
     d = new Dimension(image.getWidth(this), image.getHeight(this)); 
    } else { 
     d = new Dimension(600,400); 
    } 
    return d; 
} 

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    if (tile) { 
     int iw = image.getWidth(this); 
     int ih = image.getHeight(this); 
     if (iw > 0 && ih > 0) { 
      for (int x = 0; x < getWidth(); x += iw) { 
       for (int y = 0; y < getHeight(); y += ih) { 
        g.drawImage(image, x, y, iw, ih, this); 
       } 
      } 
     } 
    } else { 
     g.drawImage(image, 0, 0, getWidth(), getHeight(), this); 
    } 
    } 
} 
+0

這個小孩努力爲你熱插一個圖像,你甚至不用它? WTH:D –

+0

@peeskillet我在非常慢的連接中使用了第4-5次,然後在它上面發生了圖像讀取錯誤。那是當我想'擰這個..'新的BufferedImage(400,200,BufferedImage.TYPE_INT_RGB);'';) –

+0

非常感謝你的答案。這對我幫助很大! :DI也將emptyBorder的值調整爲20到200,以使按鈕更多地位於中間左側;)我希望我可以發佈它的圖片,但我需要更多的影響點:D –

2

「我的問題是,我試過很多次,花了這麼多時間試圖讓我的打法和我的幫助按鈕左側

  1. 裹在另一個JPanel
  2. 菜單的我的窗口「,設置爲L主要ImagePanelBorderLayout
  3. 添加新JPanel到的ImagePanel
  4. setOpaque(false)BorderLayout.WESTJPanel

    JPanel panel = new JPanel(); 
    panel.setOpaque(false); 
    panel.add(menu); 
    setLayout(new BorderLayout()); 
    add(panel, BorderLayout.WEST); 
    

做工精細

enter image description here

import java.awt.*; 
import java.io.IOException; 
import java.net.URL; 
import javax.imageio.ImageIO; 
import javax.swing.*; 

public class Main { 

    public static void main(String[] args) throws IOException { 
     final Image image = ImageIO.read(new URL("http://www.modernmom.com/sites/default/files/images/Power%2BRangers%2BPowerRangersMMPR.jpg")); 
     final JFrame frame = new JFrame(); 
     frame.add(new ImagePanel(image)); 
     frame.setSize(800, 600); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 
} 

@SuppressWarnings("serial") 
class ImagePanel extends JPanel { 

    private Image image; 
    private boolean tile; 
    private JPanel menu = new JPanel(new FlowLayout(FlowLayout.LEFT)); 

    ImagePanel(Image image) { 
     this.image = image; 
     this.tile = false; 
     final JButton play = new JButton("PLAY"); 
     final JButton help = new JButton("HELP"); 
     final JLabel empty = new JLabel(); 


     menu.setLayout(
       new GridLayout(4, 4)); 

     menu.add(empty); 

     menu.add(play); 

     menu.add(help); 

     JPanel panel = new JPanel(); 
     panel.setOpaque(false); 
     panel.add(menu); 
     setLayout(new BorderLayout()); 
     add(panel, BorderLayout.WEST); 

     menu.setOpaque(false); 

     setVisible(true); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     if (tile) { 
      int iw = image.getWidth(this); 
      int ih = image.getHeight(this); 
      if (iw > 0 && ih > 0) { 
       for (int x = 0; x < getWidth(); x += iw) { 
        for (int y = 0; y < getHeight(); y += ih) { 
         g.drawImage(image, x, y, iw, ih, this); 
        } 
       } 
      } 
     } else { 
      g.drawImage(image, 0, 0, getWidth(), getHeight(), this); 
     } 
    } 
}