Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java keylistener和JMenuBar问题_Java_Swing_Bluej_Jmenubar - Fatal编程技术网

Java keylistener和JMenuBar问题

Java keylistener和JMenuBar问题,java,swing,bluej,jmenubar,Java,Swing,Bluej,Jmenubar,菜单栏无法绘制,keylistener也无法工作。我应该将菜单栏添加到面板还是内容窗格?我做错了什么?怎么办?帮忙?谢谢大家! 请先复制并运行代码。 课堂绘图演示 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DrawingDemo extends JFrame implements ActionListener, KeyListener{ DrawingPanel demo = n

菜单栏无法绘制,keylistener也无法工作。我应该将菜单栏添加到面板还是内容窗格?我做错了什么?怎么办?帮忙?谢谢大家!

请先复制并运行代码。

课堂绘图演示

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();
}
}
类绘图面板

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

}
import javax.swing.*;
导入java.awt.geom.*;
导入java.awt.*;
导入java.awt.Graphics;
公共类DrawingPanel扩展了JPanel{
long nextSecond=System.currentTimeMillis()+1000;
int framesinastsecond=0;
int framesInCurrentSecond=0;
int[]LP=新int[19][3];
双pos_camx、pos_camy、pos_camz、rot_camx、rot_camy、xpoint、ypoint、zpoint;
双rot_radx,rot_rady,nclip,xscr,yscr,kx,ky;
int pxscr、pyscr、nxscr、nyscr、e;
公共空间涂料(图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();
超级油漆(g);
图形2d g1=(图形2d)g;
g1.setRenderingHint(renderingHits.KEY\u ANTIALIASING,renderingHits.VALUE\u ANTIALIAS\u ON);
g、 设置颜色(颜色为黑色);
xpoint=(双精度)(LP[0][0])/2;
ypoint=20+(双)(LP[0][1])/2;
zpoint=(双)(LP[0][2])/2;
pxscr=(int)(
((xpoint-pos_-camx)*数学cos(rot_-radx)+(ypoint+pos_-camy)*数学sin(rot_-radx))*nclip
/((ypoint-pos_-camy)*数学cos(rot_-radx)+(pos_-camx-xpoint)*数学sin(rot_-radx)+0.000000012)*100/kx
+getWidth()/2);
pyscr=(int)(
getHeight()/2-
((ypoint+pos_-camy)*数学sin(rot_-rady)+(zpoint-pos_-camz)*数学cos(rot_-rady))*nclip
/((ypoint-pos_-camy)*数学cos(rot_-radx)+(pos_-camx+xpoint)*数学sin(rot_-radx)+0.00000000 12)*100/ky)
);
for(int i=1;i下一秒){
下一秒+=1000;
framesInLastSecond=framesInCurrentSecond;
framesInCurrentSecond=0;
}
framesInCurrentSecond++;
g、 抽绳(帧最后一秒+“fps”,20,20);
}
}

只有在发生变化时才需要重新绘制,即使发生了变化,也可能不是每秒一百万次(或者是cpu可以处理的任何事情)。因此,我建议将
while(true)
更改为等待一段时间的循环,或者在keylistener中重新绘制(取决于您希望应用程序执行的操作)

您实现了
keylistener
接口,但没有将其添加到注册的侦听器中,因此需要一个
this.addKeyListener(this)

JMenuBar
不会显示,因为您在添加菜单栏之前使框架可见。通常,您希望在初始化其组件后使窗口可见。
因此,将
设置为可见(true)添加菜单栏后,Swing是一个事件驱动的环境,以任何方式阻止事件分派线程都将阻止它开始处理任何事件(如鼠标或键盘事件)

KeyListener
是一种低级API,由于许多原因,通常不鼓励使用,焦点问题最为突出。为了使组件能够响应
KeyListener
,它必须是可聚焦的且具有焦点。您的问题是,这两个条件实际上都没有得到满足(也没有向密钥侦听器注册任何内容)

虽然
JFrame
是可聚焦的,但它包含一个
JRootPane
,其中包括一个内容窗格,其中包含您的
DrawingPanel
。其中任何一个都可能在任何时候从帧中窃取焦点,从而使您的
KeyListener
无用

首选方法是使用

通常,不鼓励您重写
paint
。绘制是一种非常复杂的方法,只有在绝对确定正确的情况下,才应该覆盖它。执行自定义绘制的首选方法是将
paintComponent
方法覆盖为外线。最重要的原因是,此方法是双缓冲的,使绘制更平滑,而且通常不会干扰组件上的其他组件

另外,在绘制方法中,您将摄影机位置设置为
0
,这基本上会使您与非功能性关键点侦听器的良好工作失效;)

公共类DrawingDemo扩展了JFrame{//实现了ActionListener、KeyListener{
DrawingPanel演示=新建DrawingPanel();
公共绘图演示(){
getContentPane().add(演示);
setVisible(真);
设置大小(1024720);
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();
        }

    }

}