2010-12-11 2 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의 클래스가 수행 ...

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

을하고 SaucerPanel 클래스에이 방법을 추가? 확실히 JMenuBar를 DisplayWindow 클래스에 추가하기 만하면됩니까? – Codemwnci

+0

네, 저것을하고 싶습니다만, Menu 클래스와 SaucerPanel 클래스 만 편집 할 수 있습니다. 내가 어떻게 그럴 수 있니? – user539243

답변

2

입니다.

  • MenuBar 클래스는 JFrame을 확장해서는 안되며 JMenuBar를 확장해야합니다.

  • SaucerDriver 클래스는 MenuBar 클래스를 추가해야하며 새 JMenuBar를 만들지 않아야합니다. 새 JMenuBar는 비어 있으며 코드에서 아무 곳이나 채워지지 않습니다.

귀하의 코드는 다음과

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(); 
    } 
} 

편집 등이

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과 같아야합니다 : 당신이 SauceDriver과의 JMenuBar 객체가 SaucerPanel 객체로 전달됩니다 변경할 수 없기 때문에, SaucerPanel 클래스에서 MenuBar를 조작 할 수 있습니다. 따라서,

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

+0

그러면 두 가지 옵션이 있습니다. 첫째, SaucerDriver 대신 Main 클래스를 만들고 JMenuBar ....를 추가하거나 SaucerPanel 클래스에서 JPanel (JFrame이 됨)의 getParent를 만들고 거기에 메뉴 모음을 추가 할 수 있습니다 . – Codemwnci

+0

주 수업을 사용해야하므로 두 번째 수업을 진행할 예정입니다.하지만 .getParent가 무엇인지 잘 모르겠습니다. – user539243

관련 문제