2012-12-12 37 views
1

菜單欄不會繪製,並且keylistener不起作用。我應該將菜單欄添加到面板還是內容窗格?我究竟做錯了什麼?該怎麼辦?幫幫我?謝謝!keylistener和JMenuBar問題

請先複製並運行代碼。

CLASS DRAWINGDEMO

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

public class DrawingDemo extends JFrame implements ActionListener, KeyListener{ 
DrawingPanel demo = new DrawingPanel(); 
public DrawingDemo() 
{ 

getContentPane().add(demo); 
setVisible(true); 
setSize(1024,720); 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
setLocationRelativeTo(null); 

JPanel p = new JPanel(); 
JMenuBar mb = new JMenuBar(); 
JMenu file = new JMenu("File"); 
JMenu edit = new JMenu("Edit"); 
JMenu settings = new JMenu("Settings"); 
JMenu help = new JMenu("Help"); 
JMenuItem exit = new JMenuItem(">Exit"); 
JMenuItem imp = new JMenuItem(">Import"); 
JMenuItem exp = new JMenuItem(">Export"); 
JMenuItem sav = new JMenuItem(">Save"); 
JMenuItem ope = new JMenuItem(">Open"); 
file.add(ope); 
file.add(sav); 
file.add(imp); 
file.add(exp); 
file.add(exit); 
mb.add(file); 
mb.add(edit); 
mb.add(settings); 
mb.add(help); 
setJMenuBar(mb); 

while(true){ 
demo.repaint(); 
} 
} 
public void actionPerformed(java.awt.event.ActionEvent e) 
{ 
} 
public void KeyEvent(java.awt.event.ActionEvent e) 
{ 
} 
public void keyReleased(java.awt.event.KeyEvent e) 
{ 
} 
public void keyPressed(java.awt.event.KeyEvent e) 
{ 
switch (e.getKeyCode()){ 
    case KeyEvent.VK_A : 
    { 
    demo.pos_camx -= 0.5; 
    } 
    break; 
    case KeyEvent.VK_D : 
    { 
    demo.pos_camx += 0.5; 
    } 
    break; 
    case KeyEvent.VK_W : 
    { 
    demo.pos_camy += 0.5; 
    } 
    break; 
    case KeyEvent.VK_S : 
    { 
    demo.pos_camy -= 0.5; 
    } 
    break; 
    } 
} 
public void keyTyped(java.awt.event.KeyEvent e) 
{ 
} 
public static void main(String[] args) 
{ 
    new DrawingDemo(); 
} 
} 

CLASS DRAWINGPANEL

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

public class DrawingPanel extends JPanel { 

long nextSecond = System.currentTimeMillis() + 1000; 
int framesInLastSecond = 0; 
int framesInCurrentSecond = 0; 
int[][] LP= new int[19][3]; 
    double pos_camx,pos_camy,pos_camz,rot_camx,rot_camy,xpoint,ypoint,zpoint; 
    double rot_radx,rot_rady,nclip,xscr,yscr,kx,ky; 
    int pxscr,pyscr,nxscr,nyscr,e; 



public void paint(Graphics g) 
{ 
      LP[0][0] = -1; 
     LP[0][1] = -1; 
     LP[0][2] = -1; 

     LP[1][0] = 1; 
     LP[1][1] = -1; 
     LP[1][2] = -1; 

     LP[2][0] = 1; 
     LP[2][1] = 1; 
     LP[2][2] = -1; 

     LP[3][0] = -1; 
     LP[3][1] = 1; 
     LP[3][2] = -1; 

     LP[4][0] = -1; 
     LP[4][1] = -1; 
     LP[4][2] = -1; 

     LP[5][0] = 1; 
     LP[5][1] = 1; 
     LP[5][2] = -1; 

     LP[6][0] = 1; 
     LP[6][1] = 1; 
     LP[6][2] = 1; 

     LP[7][0] = -1; 
     LP[7][1] = 1; 
     LP[7][2] = 1; 

     LP[8][0] = -1; 
     LP[8][1] = -1; 
     LP[8][2] = 1; 

     LP[9][0] = 1; 
     LP[9][1] = -1; 
     LP[9][2] = 1; 

     LP[10][0] = 1; 
     LP[10][1] = 1; 
     LP[10][2] = 1; 

     LP[11][0] = -1; 
     LP[11][1] = -1; 
     LP[11][2] = 1; 

     LP[12][0] = -1; 
     LP[12][1] = -1; 
     LP[12][2] = -1; 

     LP[13][0] = 1; 
     LP[13][1] = -1; 
     LP[13][2] = 1; 

     LP[14][0] = 1; 
     LP[14][1] = -1; 
     LP[14][2] = -1; 

     LP[15][0] = 1; 
     LP[15][1] = 1; 
     LP[15][2] = 1; 

     LP[16][0] = -1; 
     LP[16][1] = 1; 
     LP[16][2] = -1; 

     LP[17][0] = -1; 
     LP[17][1] = 1; 
     LP[17][2] = 1; 

     LP[18][0] = -1; 
     LP[18][1] = -1; 
     LP[18][2] = -1; 

pos_camx = 0; 
pos_camy = 0; 
pos_camz = 0; 

rot_camx = 0; 
rot_camy = 0; 
rot_radx = 3.1415*rot_camx/180; 
rot_rady = 3.1415*rot_camy/180; 

nclip = 0.275; 
kx = 8.52/getWidth(); 
ky = 5.46/getHeight(); 
super.paint(g); 
    Graphics2D g1 = (Graphics2D)g; 
    g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
    g.setColor(Color.black); 
    xpoint = (double)(LP[0][0])/2; 
    ypoint = 20 + (double)(LP[0][1])/2; 
    zpoint = (double)(LP[0][2])/2; 

    pxscr = (int)(
    ((xpoint - pos_camx)*Math.cos(rot_radx) + (ypoint + pos_camy)*Math.sin(rot_radx))*nclip 
    /((ypoint - pos_camy)*Math.cos(rot_radx) + (pos_camx- xpoint)*Math.sin(rot_radx) + 0.0000000012)*100/kx 
        + getWidth()/2); 

    pyscr = (int)(
        getHeight()/2- 
    (((ypoint + pos_camy)*Math.sin(rot_rady) + (zpoint - pos_camz)*Math.cos(rot_rady))*nclip 
    /((ypoint - pos_camy)*Math.cos(rot_radx) + (pos_camx + xpoint)*Math.sin(rot_radx) + 0.0000000012)*100/ky) 
       ); 
    for (int i=1; i<19;i++){ 
      xpoint = (double)(LP[i][0])/2; 
      ypoint = 20 + (double)(LP[i][1])/2; 
      zpoint = (double)(LP[i][2])/2; 

      nxscr = (int)(
     ((xpoint - pos_camx)*Math.cos(rot_radx) + (ypoint + pos_camy)*Math.sin(rot_radx))*nclip 
     /((ypoint - pos_camy)*Math.cos(rot_radx)+(pos_camx + xpoint)*Math.sin(rot_radx)+0.0000000012)*100/kx 
         +getWidth()/2); 

      nyscr = (int)(
         getHeight()/2- 
     (((ypoint + pos_camy)*Math.sin(rot_rady)+(zpoint - pos_camz)*Math.cos(rot_rady))*nclip 
     /((ypoint - pos_camy)*Math.cos(rot_radx)+(pos_camx + xpoint)*Math.sin(rot_radx)+0.0000000012)*100/ky) 
         ); 
      g1.drawLine(pxscr,pyscr,nxscr,nyscr); 

     pxscr = nxscr; 
     pyscr = nyscr; 
     } 
     g.drawString("(" + pxscr + "," + pyscr + ")",20,40); 
     long currentTime = System.currentTimeMillis(); 
    if (currentTime > nextSecond) { 
    nextSecond += 1000; 
    framesInLastSecond = framesInCurrentSecond; 
    framesInCurrentSecond = 0; 
    } 
framesInCurrentSecond++; 
g.drawString(framesInLastSecond + " fps", 20, 20); 
} 

} 
+2

'while(true){demo。重繪(); }'這是你的第一個問題 – MadProgrammer

+0

你能詳細說明嗎? – tiffanyButterfly95

+0

你永遠不會從這個無限循環退出構造函數。 – Mordechai

回答

0

你只需要重繪如果有什麼變化,即使在當時,恐怕不是一百萬次(或任何你的CPU可以處理)。所以我建議將while(true)更改爲一個等待或在keylistener中重繪的循環(取決於您希望應用程序執行的操作)

並且您實現了keylistener接口,但不添加它給註冊的聽衆,所以你需要一個this.addKeyListener(this);

JMenuBar doesnt顯示,因爲你使框架可見之前添加菜單欄。通常你想在初始化它的組件後使窗口可見。 因此在添加菜單欄後輸入setVisible(true);

+0

其實,菜單欄並沒有顯示,因爲他阻止了EDT,你的觀點是有效的。如果他在調用'setVisible'之前添加了菜單,則可能菜單欄會顯示出來;) – MadProgrammer

3

Swing是一個事件驅動環境,以任何方式阻止事件派發線程將阻止它開始處理任何事件(例如鼠標或鍵盤事件)。

KeyListener是一個低級別的API,並且由於多種原因通常不鼓勵,焦點問題是最突出的。爲了使組件能夠響應KeyListener它必須是可以聚焦的並且有重點。你有的問題是,這些條件都沒有實際滿足(也沒有實際註冊關鍵聽衆)。

儘管JFrame是可以關注的,但它包含一個JRootPane,其中包含(其中包含)一個內容窗格,其中包含您的DrawingPanel。任何這些都可能隨時從框架中偷取焦點,導致您的KeyListener無用。

的優選方法是使用key bindings API

一般情況下,是從覆蓋paint氣餒。油漆是一種非常複雜的方法,如果您絕對確信這是正確的做法,您應該只重寫它。執行自定義繪畫的首選方法是重寫paintComponent方法,如列出的here。最重要的原因是,這種方法是雙緩衝的,使您的繪畫更流暢,而且,您通常不會干擾組件上可能存在的其他組件。

而且,在你的paint方法,你的相機位置設置爲0基本上撤消與非功能按鍵偵聽所有你良好的工作;)

public class DrawingDemo extends JFrame { //implements ActionListener, KeyListener { 

    DrawingPanel demo = new DrawingPanel(); 

    public DrawingDemo() { 

     getContentPane().add(demo); 
     setVisible(true); 
     setSize(1024, 720); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLocationRelativeTo(null); 

     JPanel p = new JPanel(); 
     JMenuBar mb = new JMenuBar(); 
     JMenu file = new JMenu("File"); 
     JMenu edit = new JMenu("Edit"); 
     JMenu settings = new JMenu("Settings"); 
     JMenu help = new JMenu("Help"); 
     JMenuItem exit = new JMenuItem(">Exit"); 
     JMenuItem imp = new JMenuItem(">Import"); 
     JMenuItem exp = new JMenuItem(">Export"); 
     JMenuItem sav = new JMenuItem(">Save"); 
     JMenuItem ope = new JMenuItem(">Open"); 
     file.add(ope); 
     file.add(sav); 
     file.add(imp); 
     file.add(exp); 
     file.add(exit); 
     mb.add(file); 
     mb.add(edit); 
     mb.add(settings); 
     mb.add(help); 
     setJMenuBar(mb); 

    } 

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

    public class DrawingPanel extends JPanel { 

     long nextSecond = System.currentTimeMillis() + 1000; 
     int framesInLastSecond = 0; 
     int framesInCurrentSecond = 0; 
     int[][] LP = new int[19][3]; 
     double pos_camx, pos_camy, pos_camz, rot_camx, rot_camy, xpoint, ypoint, zpoint; 
     double rot_radx, rot_rady, nclip, xscr, yscr, kx, ky; 
     int pxscr, pyscr, nxscr, nyscr, e; 

     public DrawingPanel() { 
      InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW); 
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "Move.A"); 
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "Move.D"); 
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "Move.W"); 
      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "Move.S"); 

      ActionMap am = getActionMap(); 
      am.put("Move.A", new MoveXAction(this, 0.5f)); 
      am.put("Move.D", new MoveXAction(this, -0.5f)); 
      am.put("Move.W", new MoveYAction(this, 0.5f)); 
      am.put("Move.S", new MoveYAction(this, -0.5f)); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); //To change body of generated methods, choose Tools | Templates. 

      LP[0][0] = -1; 
      LP[0][1] = -1; 
      LP[0][2] = -1; 

      LP[1][0] = 1; 
      LP[1][1] = -1; 
      LP[1][2] = -1; 

      LP[2][0] = 1; 
      LP[2][1] = 1; 
      LP[2][2] = -1; 

      LP[3][0] = -1; 
      LP[3][1] = 1; 
      LP[3][2] = -1; 

      LP[4][0] = -1; 
      LP[4][1] = -1; 
      LP[4][2] = -1; 

      LP[5][0] = 1; 
      LP[5][1] = 1; 
      LP[5][2] = -1; 

      LP[6][0] = 1; 
      LP[6][1] = 1; 
      LP[6][2] = 1; 

      LP[7][0] = -1; 
      LP[7][1] = 1; 
      LP[7][2] = 1; 

      LP[8][0] = -1; 
      LP[8][1] = -1; 
      LP[8][2] = 1; 

      LP[9][0] = 1; 
      LP[9][1] = -1; 
      LP[9][2] = 1; 

      LP[10][0] = 1; 
      LP[10][1] = 1; 
      LP[10][2] = 1; 

      LP[11][0] = -1; 
      LP[11][1] = -1; 
      LP[11][2] = 1; 

      LP[12][0] = -1; 
      LP[12][1] = -1; 
      LP[12][2] = -1; 

      LP[13][0] = 1; 
      LP[13][1] = -1; 
      LP[13][2] = 1; 

      LP[14][0] = 1; 
      LP[14][1] = -1; 
      LP[14][2] = -1; 

      LP[15][0] = 1; 
      LP[15][1] = 1; 
      LP[15][2] = 1; 

      LP[16][0] = -1; 
      LP[16][1] = 1; 
      LP[16][2] = -1; 

      LP[17][0] = -1; 
      LP[17][1] = 1; 
      LP[17][2] = 1; 

      LP[18][0] = -1; 
      LP[18][1] = -1; 
      LP[18][2] = -1; 

//   pos_camx = 0; 
//   pos_camy = 0; 
//   pos_camz = 0; 

      rot_camx = 0; 
      rot_camy = 0; 
      rot_radx = 3.1415 * rot_camx/180; 
      rot_rady = 3.1415 * rot_camy/180; 

      nclip = 0.275; 
      kx = 8.52/getWidth(); 
      ky = 5.46/getHeight(); 
      Graphics2D g1 = (Graphics2D) g; 
      g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
      g.setColor(Color.black); 
      xpoint = (double) (LP[0][0])/2; 
      ypoint = 20 + (double) (LP[0][1])/2; 
      zpoint = (double) (LP[0][2])/2; 

      pxscr = (int) (((xpoint - pos_camx) * Math.cos(rot_radx) + (ypoint + pos_camy) * Math.sin(rot_radx)) * nclip 
        /((ypoint - pos_camy) * Math.cos(rot_radx) + (pos_camx - xpoint) * Math.sin(rot_radx) + 0.0000000012) * 100/kx 
        + getWidth()/2); 

      pyscr = (int) (getHeight()/2 
        - (((ypoint + pos_camy) * Math.sin(rot_rady) + (zpoint - pos_camz) * Math.cos(rot_rady)) * nclip 
        /((ypoint - pos_camy) * Math.cos(rot_radx) + (pos_camx + xpoint) * Math.sin(rot_radx) + 0.0000000012) * 100/ky)); 
      for (int i = 1; i < 19; i++) { 
       xpoint = (double) (LP[i][0])/2; 
       ypoint = 20 + (double) (LP[i][1])/2; 
       zpoint = (double) (LP[i][2])/2; 

       nxscr = (int) (((xpoint - pos_camx) * Math.cos(rot_radx) + (ypoint + pos_camy) * Math.sin(rot_radx)) * nclip 
         /((ypoint - pos_camy) * Math.cos(rot_radx) + (pos_camx + xpoint) * Math.sin(rot_radx) + 0.0000000012) * 100/kx 
         + getWidth()/2); 

       nyscr = (int) (getHeight()/2 
         - (((ypoint + pos_camy) * Math.sin(rot_rady) + (zpoint - pos_camz) * Math.cos(rot_rady)) * nclip 
         /((ypoint - pos_camy) * Math.cos(rot_radx) + (pos_camx + xpoint) * Math.sin(rot_radx) + 0.0000000012) * 100/ky)); 
       g1.drawLine(pxscr, pyscr, nxscr, nyscr); 

       pxscr = nxscr; 
       pyscr = nyscr; 
      } 
      g.drawString("(" + pxscr + "," + pyscr + ")", 20, 40); 
      long currentTime = System.currentTimeMillis(); 
      if (currentTime > nextSecond) { 
       nextSecond += 1000; 
       framesInLastSecond = framesInCurrentSecond; 
       framesInCurrentSecond = 0; 
      } 
      framesInCurrentSecond++; 
      g.drawString(framesInLastSecond + " fps", 20, 20); 
     } 
    } 

    public class MoveXAction extends AbstractAction { 

     private float direction; 
     private final DrawingPanel pane; 

     private MoveXAction(DrawingPanel pane, float amount) { 

      this.direction = amount; 
      this.pane = pane; 

     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      System.out.println("...x by " + direction); 
      pane.pos_camx += direction; 
      pane.repaint(); 
     } 

    } 

    public class MoveYAction extends AbstractAction { 

     private float direction; 
     private final DrawingPanel pane; 

     private MoveYAction(DrawingPanel pane, float amount) { 

      this.direction = amount; 
      this.pane = pane; 

     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      System.out.println("...y by " + direction); 
      pane.pos_camy += direction; 
      pane.repaint(); 
     } 

    } 

} 

我有大約一個快速的打法和我不得不說,尼斯;)

+1

+1非常好,並在KeyBindings上提供意見,並且在JPanel上查看它的相機旋轉代碼。真是令人敬畏的OP –