2010-12-11 99 views
1

我想寫一個由U.F.O組成的小遊戲。它可以在各個方向飛行(基於按鈕),並在碰到屏幕底部時爆炸。我有4個班級,其中2個是我不允許編輯的。它們是:爲什麼我的菜單欄不會彈出顯示窗口?

顯示窗口

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

public class DisplayWindow extends JFrame{ 

    private Container c; 

    public DisplayWindow(){ 
    super("Display"); 
    c = this.getContentPane(); 
    } 

    public void addPanel(JPanel p){ 
    c.add(p); 
    } 

    public void showFrame(){ 
    this.pack(); 
    this.setVisible(true); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 

而對於UFO的驅動程序。

import javax.swing.*; 

public class SaucerDriver{ 

    public static void main(String[] args){ 
    DisplayWindow d = new DisplayWindow(); 
    JMenuBar menuBar = new JMenuBar(); 
    d.setJMenuBar(menuBar); 
    SaucerPanel p = new SaucerPanel(menuBar); 
    d.addPanel(p); 
    d.showFrame(); 
    } 
} 

我想添加一個菜單欄,但它不會顯示出來。我究竟做錯了什麼?

這裏是我的繪畫所有的建築類和諸如此類的東西

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

public class SaucerPanel extends JPanel implements ActionListener{ 
    JButton quitButton; 
    JButton upButton; 
    JButton leftButton; 
    JButton rightButton; 
    JButton downButton; 
    int xLoc; 
    int yLoc; 
    boolean explode; 

    public SaucerPanel(JMenuBar menuBar){ 
    xLoc = 20; 
    yLoc = 200; 

    explode = false; 

    this.setPreferredSize(new Dimension(850,500)); 
    this.setBackground(Color.white); 
    quitButton = new JButton("Quit"); 
    this.add(quitButton); 
    quitButton.addActionListener(this); 

    downButton = new JButton("Down"); 
    this.add(downButton); 
    downButton.addActionListener(this); 

    upButton = new JButton("Up"); 
    this.add(upButton); 
    upButton.addActionListener(this); 

    leftButton = new JButton("Left"); 
    this.add(leftButton); 
    leftButton.addActionListener(this); 

    rightButton = new JButton("Right"); 
    this.add(rightButton); 
    rightButton.addActionListener(this); 
    } 

    public void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    g.setColor(Color.green); 
    g.fillRect(0,440,850,60); 

    g.setColor(Color.gray); 
    g.fillRect(80,420,210,100); 
     g.fillRect(100,410,170,100); 
     g.fillRect(120,400,130,100); 
     g.fillOval(130,350,110,150); 
     int[] aPoints = {170,200,185}; 
     int[] bPoints = {370,370,320}; 
     g.fillPolygon(aPoints,bPoints, 3); 
     g.setColor(Color.black); 
     g.fillRect(135,410,20,100); 
     g.fillRect(175,410,20,100); 
     g.fillRect(215,410,20,100); 

     g.setColor(Color.blue); 
     g.fillRect(460,140,120,360); 

     g.setColor(Color.red); 
     g.fillRect(700,400,100,150); 

     g.setColor(Color.yellow); 
     int[] xPoints = {700,800,750}; 
     int[] yPoints = {400,400,350}; 
     g.fillPolygon(xPoints, yPoints, 3); 

     g.setColor(Color.black); 
     g.fillRect(730,450,40,50); 

     g.setColor(Color.darkGray); 
    if(explode == false){ 
     g.fillOval(xLoc,yLoc,80,40); 
     g.drawString("hovering...",xLoc,yLoc - 10); 
    }else{ 
     for(int i = 0; i < 2000; i++){ 
     int x = (int)(Math.random()*850); 
     int y = (int)(Math.random()*850); 
     g.setColor(Color.orange); 
     g.fillOval(x,y,5,5); 
     int a = (int)(Math.random()*850); 
     int b = (int)(Math.random()*850); 
     g.setColor(Color.red); 
     g.fillOval(a,b,5,5); 
     int c = (int)(Math.random()*850); 
     int d = (int)(Math.random()*850); 
     g.setColor(Color.yellow); 
     g.fillOval(c,d,5,5); 
     } 
    } 
    } 

     public void actionPerformed(ActionEvent e){ 
     if(e.getSource() == quitButton){ 
      System.exit(0); 
     } 
     else if(e.getSource() == downButton){ 
      yLoc += 5; 
      if(yLoc == 400){ 
      explode = true; 
      } 
     } 
     else if(e.getSource() == upButton){ 
      yLoc -= 5; 
     } 
     else if(e.getSource() == leftButton){ 
      xLoc -= 5; 
     } 
     else if(e.getSource() == rightButton){ 
      xLoc += 5; 
     } 

     repaint(); 
     } 
    } 

這裏是我的菜單欄類

import javax.swing.*; 


public class menuBar extends JFrame{ 

    public menuBar(){ 

    JFrame frame = new JFrame("Menu"); 
    frame.setVisible(true); 
    frame.setSize(850,200); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

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

    JMenu file = new JMenu("File"); 
    menuBar.add(file); 
    JMenuItem open = new JMenuItem("Open"); 
    file.add(open); 
    JMenuItem neww = new JMenuItem("New"); 
    file.add(neww); 

    JMenu edit = new JMenu("Edit"); 
    menuBar.add(edit); 
    JMenuItem color = new JMenuItem("Change Color"); 
    edit.add(color); 

    JMenu help = new JMenu("Help"); 
    menuBar.add(help); 
    JMenuItem about = new JMenuItem("About"); 
    help.add(about); 

    frame.setVisible(true); 
    } 
} 
+0

爲什麼你有兩個JFrame的課嗎?當然你只是想將你的JMenuBar添加到DisplayWindow類中? – Codemwnci 2010-12-12 00:31:37

+0

是的,這就是我想要做的,但我只能編輯Menu類和SaucerPanel類。我會怎麼做? – user539243 2010-12-12 00:37:05

回答

2

您需要進行一些小的改動。

  • 你的MenuBar類不應該擴展JFrame,它應該擴展JMenuBar。

  • 您的SaucerDriver類需要添加您的MenuBar類,而不是創建一個新的JMenuBar,因爲它是空的,並且不會在您的代碼中的任何位置填充。

你的代碼看起來應該是這樣

import javax.swing.*; 


public class menuBar extends JMenuBar{ 

    public menuBar(){ 

    JMenu file = new JMenu("File"); 
    this.add(file); 
    JMenuItem open = new JMenuItem("Open"); 
    file.add(open); 
    JMenuItem neww = new JMenuItem("New"); 
    file.add(neww); 

    JMenu edit = new JMenu("Edit"); 
    this.add(edit); 
    JMenuItem color = new JMenuItem("Change Color"); 
    edit.add(color); 

    JMenu help = new JMenu("Help"); 
    this.add(help); 
    JMenuItem about = new JMenuItem("About"); 
    help.add(about); 
    } 
} 

和SaucerDriver這樣

import javax.swing.*; 

public class SaucerDriver{ 

    public static void main(String[] args){ 
    DisplayWindow d = new DisplayWindow(); 
    d.setJMenuBar(new menuBar()); 
    SaucerPanel p = new SaucerPanel(menuBar); 
    d.addPanel(p); 
    d.showFrame(); 
    } 
} 

編輯:因爲你不能改變SauceDriver和A的JMenuBar對象傳遞到SaucerPanel對象,那麼你可以從SaucerPanel類操作MenuBar。因此,執行下列操作...

public SaucerPanel(JMenuBar menuBar){ 
    setMenuBarItems(menuBar); 
    //...rest of the JPANEL code here 
} 

而且這種方法添加到您的SaucerPanel類

public void setMenuBarItems(JMenuBar menuBar) { 
    JMenu file = new JMenu("File"); 
    menuBar.add(file); 
    JMenuItem open = new JMenuItem("Open"); 
    file.add(open); 
    JMenuItem neww = new JMenuItem("New"); 
    file.add(neww); 

    JMenu edit = new JMenu("Edit"); 
    menuBar.add(edit); 
    JMenuItem color = new JMenuItem("Change Color"); 
    edit.add(color); 

    JMenu help = new JMenu("Help"); 
    menuBar.add(help); 
    JMenuItem about = new JMenuItem("About"); 
    help.add(about); 
} 
+0

我不能編輯SaucerDriver或DisplayWindow類。他們被給了我。 – user539243 2010-12-12 00:42:57

+0

然後你有兩個選擇。首先,您可以創建您自己的Main類,代替SaucerDriver,並添加JMenuBar ....或者,從SaucerPanel類中,您可以獲得JPanel的Palette(這將是JFrame),並添加菜單欄。 – Codemwnci 2010-12-12 00:47:05

+0

我會做第二個,因爲我必須使用他的主類,但我不確定.getParent是什麼.. – user539243 2010-12-12 00:48:30

相關問題