2013-12-12 62 views
1

我想創建一個帶有JMenubar和JPanel的JFrame與背景圖像。我迄今爲止創建的代碼創建了兩個窗口,一個JFrame和一個JPanel,JFrame具有JMenuBar,JPanel具有背景,我可以將它們放在一個窗口中嗎?JFrame與JMenuBar和java中的背景Netbeans

package RPGPackage; 

import java.awt.BorderLayout; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 
import javax.swing.*; 
import javax.swing.JMenuBar; 

public class RPG extends JFrame { 

    public RPG() { 
     JFrame frame = new JFrame("RPG"); 
     frame.setVisible(true); 
     frame.setSize(1005,710); 
     frame.setLocationRelativeTo(null); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setResizable(false); 

     JPanel panel = new JPanel(); 
     panel.setLayout(new BorderLayout()); 
     frame.add(panel); 
     setLocation(0,0); 
     setVisible(true); 
     setSize(1005,710); 
     setResizable(false); 

     JMenuBar menubar = new JMenuBar(); 
     frame.setJMenuBar(menubar); 

     JMenu file = new JMenu("File"); 
     file.setMnemonic(KeyEvent.VK_A); 

     menubar.add(file); 

     JMenuItem Load = new JMenuItem("Load",KeyEvent.VK_L); 
     file.add(Load); 

     JMenuItem Save = new JMenuItem("Save",KeyEvent.VK_S); 
     file.add(Save); 

     JMenuItem exit = new JMenuItem(new AbstractAction("Quit"){//Adds Exit button 
     public void actionPerformed(ActionEvent e) {//adds an action to the JMenuItem  "Exit" 
     System.exit(0);}//when action is performed program will exit 
     }); 
     file.add(exit);//adds the JMenuitem to the JMenuBar thats above. 

     Background background = new Background(); 
     add(background); 
    } 

    public static void main(String[] args) { 
      RPG window = new RPG(); 
    } 
} 







package RPGPackage; 

import java.awt.Graphics; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Background extends JPanel { 

    @Override 
    public void paintComponent(Graphics g) { 

     super.paintComponent(g); 

     ImageIcon background = new ImageIcon("Image.png"); 

     background.paintIcon(this, g, 0, 0); 
    } 
} 

回答

0

1)您RPG類擴展JFrame,你不需要創建框架

JFrame frame = new JFrame("RPG");)的另一個實例。使用您的RPG實例。

2)使用BufferedImage代替ImageIcon,並繪製圖像等下:

private BufferedImage img = ImageIO.read(getClass().getResourceAsStream("Image.png")); 

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    Graphics2D g2d = (Graphics2D) g; 
    g2d.drawImage(img, 0, 0, this); 
} 

3)調用setVisible(true);在界面結構的端部或下一個像:

SwingUtilities.invokeLater(new Runnable() { 

    @Override 
    public void run() { 
     setVisible(true); 
    } 
}); 

4)的setResizable(假);這是不好的做法。使用LayoutManager幫助管理組件大小調整。

5)使用pack();方法,而不是setSize(...)

我有固定的代碼,審視它:

import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 

import javax.imageio.ImageIO; 
import javax.swing.AbstractAction; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 

public class RPG extends JFrame { 

    public RPG() { 
     super("RPG"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setResizable(false); 

     JMenuBar menubar = new JMenuBar(); 
     setJMenuBar(menubar); 

     JMenu file = new JMenu("File"); 
     file.setMnemonic(KeyEvent.VK_A); 

     menubar.add(file); 

     JMenuItem Load = new JMenuItem("Load", KeyEvent.VK_L); 
     file.add(Load); 

     JMenuItem Save = new JMenuItem("Save", KeyEvent.VK_S); 
     file.add(Save); 

     JMenuItem exit = new JMenuItem(new AbstractAction("Quit") { 

      public void actionPerformed(ActionEvent e) { 
       System.exit(0); 
      } 
     }); 
     file.add(exit); 

     Background background = new Background(); 
     add(background); 

     setSize(200,200); 
     setLocationRelativeTo(null); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     RPG window = new RPG(); 
    } 

    class Background extends JPanel { 

     private BufferedImage img; 

     public Background() { 
      try { 
       img = ImageIO.read(getClass().getResourceAsStream("Image.png")); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g; 
      g2d.drawImage(img, 0, 0, this); 
     } 

    } 
} 

而且不是畫的圖像,你可以使用JLabel舉辦書畫的是,它可以幫助你從框中獲得一些功能:

class Background extends JPanel { 

    private ImageIcon img; 

    public Background() { 
     URL resource = getClass().getResource("Image.jpg"); 
     img = new ImageIcon(resource); 
     JLabel l = new JLabel(img); 
     add(l); 
    } 
}