Java JMenuItem仅从键盘输入触发,而不是单击
我使用JFrame用Java编写了一个简单的绘图程序(这是我第一次使用JFrame)。用户单击并拖动以绘制形状,但这并不重要。我有一个Java JMenuItem仅从键盘输入触发,而不是单击,java,swing,jframe,menubar,jmenubar,Java,Swing,Jframe,Menubar,Jmenubar,我使用JFrame用Java编写了一个简单的绘图程序(这是我第一次使用JFrame)。用户单击并拖动以绘制形状,但这并不重要。我有一个JMenuBar,有一系列选项,比如形状类型、新建和退出。当用户单击new按钮时,应该会清除屏幕。当用户点击Ctrl+N时,这一切正常。但是,当单击按钮时,它根本不起作用 我将调试System.out.println放在newItem的actionEvent内部,当单击该项目时,它会很好地打印出来,但实际上不会擦除屏幕。知道这是什么原因吗 我删掉了大部分节目,留下
JMenuBar
,有一系列选项,比如形状类型、新建和退出。当用户单击new
按钮时,应该会清除屏幕。当用户点击Ctrl+N时,这一切正常。但是,当单击按钮时,它根本不起作用
我将调试System.out.println
放在newItem的actionEvent内部,当单击该项目时,它会很好地打印出来,但实际上不会擦除屏幕。知道这是什么原因吗
我删掉了大部分节目,留下了看问题所必需的内容。您仍然可以按住并拖动以绘制形状(该形状在释放鼠标按钮之前不会显示),然后按Ctrl+N键以清除屏幕,但单击“新建”无法完成此操作
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
public class E3G04 extends JFrame implements WindowListener, ActionListener, MouseListener, MouseMotionListener
{
//Variables are declared as volatile to ensure that they're always called from system RAM
static volatile String type = "rectangle";
static volatile Boolean fill = true;
static Color lineColor = Color.BLACK;
static Color fillColor = Color.RED;
static int size = 1;
CanvasEX cx = new CanvasEX();
static boolean running = true;
JMenuBar mb = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem newItem = new JMenuItem("New");
JMenuItem quitItem = new JMenuItem ("Quit");
protected E3G04()
{
mb.add(fileMenu);
fileMenu.add(newItem);
fileMenu.add(quitItem);
newItem.setMnemonic('N');
quitItem.setMnemonic('Q');
newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
quitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
newItem.addActionListener(this);
quitItem.addActionListener(this);
cx.setSize(800,600);
cx.addMouseListener(this);
cx.addMouseMotionListener(this);
setJMenuBar(mb);
setBounds(100,100,800,600);
setLayout(new BorderLayout());
add("Center",cx);
addWindowListener(this);
setResizable(true);
setVisible(true);
}
public static void main(String [] args)
{
new E3G04();
}
public void stop()
{
newItem.removeActionListener(this);
quitItem.removeActionListener(this);
dispose();
System.exit(0);
}
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if (o == newItem)
{
cx.erase = true;
cx.repaint();
}
if (o == quitItem)
{
running = false;
stop();
}
}
public void mousePressed(MouseEvent m)
{
cx.start = m.getPoint();
cx.end = cx.start;
cx.cur = cx.start;
}
public void mouseDragged(MouseEvent m)
{
cx.cur = m.getPoint();
}
public void mouseReleased(MouseEvent m)
{
cx.end = cx.cur;
cx.repaint();
}
public void itemStateChanged(ItemEvent e)
{
Object o = e.getSource();
}
public void windowClosing(WindowEvent e)
{
running = false;
stop();
}
public void mouseClicked(MouseEvent m){}
public void mouseExited(MouseEvent m){}
public void mouseEntered(MouseEvent m){}
public void mouseMoved(MouseEvent m){}
public void windowClosed(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
class CanvasEX extends Canvas
{
Point start = new Point(100,100);
Point cur = new Point(100,100);
Point end = new Point(100,100);
Image offscreen;
boolean erase = false;
public void update(Graphics g)
{
//This is adds the new stuff to the screen or erases the screen if erase is true
Graphics buffer;
if (offscreen == null)
{
offscreen = createImage(getWidth(), getHeight());
}
buffer = offscreen.getGraphics();
if (erase)
{
buffer.setColor(getBackground());
buffer.fillRect(0,0,800, 600);
buffer.dispose();
erase = false;
}
paint(buffer);
g.drawImage(offscreen, 0, 0, this);
}
public void paint(Graphics g)
{
Graphics buffer = g;
if (erase)
{
g.dispose();
erase = false;
}
g.setColor(E3G04.lineColor);
if (end.x > start.x && end.y > start.y)
g.fillRect(start.x,start.y, Math.abs(end.x-start.x),Math.abs(end.y-start.y));
if (end.x > start.x && end.y < start.y)
g.fillRect(start.x,end.y, Math.abs(end.x-start.x),Math.abs(end.y-start.y));
if (end.x < start.x && end.y > start.y)
g.fillRect(end.x, start.y, Math.abs(end.x-start.x),Math.abs(end.y-start.y));
if (end.x < start.x && end.y < start.y)
g.fillRect(end.x, end.y, Math.abs(end.x-start.x),Math.abs(end.y-start.y));
g.setColor(E3G04.fillColor);
if (end.x > start.x && end.y > start.y)
g.fillRect(start.x + E3G04.size,start.y + E3G04.size, Math.abs(end.x-start.x) - 2 * E3G04.size,Math.abs(end.y-start.y) - 2 * E3G04.size);
if (end.x > start.x && end.y < start.y)
g.fillRect(start.x + E3G04.size,end.y + E3G04.size, Math.abs(end.x-start.x) - 2 * E3G04.size,Math.abs(end.y-start.y) - 2 * E3G04.size);
if (end.x < start.x && end.y > start.y)
g.fillRect(end.x + E3G04.size, start.y + E3G04.size, Math.abs(end.x-start.x) - 2 * E3G04.size,Math.abs(end.y-start.y) - 2 * E3G04.size);
if (end.x < start.x && end.y < start.y)
g.fillRect(end.x + E3G04.size, end.y + E3G04.size, Math.abs(end.x-start.x) - 2 * E3G04.size,Math.abs(end.y-start.y) - 2 * E3G04.size);
}
}
import java.awt.*;
导入java.awt.event.*;
导入java.lang.*;
导入javax.swing.*;
公共类E3G04扩展了JFrame,实现了WindowListener、ActionListener、MouseListener、MouseMotionListener
{
//变量声明为volatile,以确保始终从系统RAM调用它们
静态易失性字符串type=“矩形”;
静态可变布尔填充=真;
静态颜色lineColor=Color.BLACK;
静态颜色fillColor=Color.RED;
静态整数大小=1;
CanvasEX cx=新CanvasEX();
静态布尔运行=真;
JMenuBar mb=新的JMenuBar();
JMenu fileMenu=新JMenu(“文件”);
JMenuItem newItem=新JMenuItem(“新”);
JMenuItem quitItem=新的JMenuItem(“退出”);
受保护的E3G04()
{
添加(文件菜单);
fileMenu.add(newItem);
fileMenu.add(退出项);
newItem.set助记符('N');
quitItem.set助记符('Q');
setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_掩码));
setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,ActionEvent.CTRL_MASK));
newItem.addActionListener(此);
quitItem.addActionListener(此);
cx.设置大小(800600);
cx.addMouseListener(本);
cx.addMouseMotionListener(此);
setJMenuBar(mb);
立根(100800600);
setLayout(新的BorderLayout());
添加(“中心”,cx);
addWindowListener(此);
可设置大小(真);
setVisible(真);
}
公共静态void main(字符串[]args)
{
新的E3G04();
}
公共停车场()
{
newItem.removeActionListener(此);
quitItem.removeActionListener(此);
处置();
系统出口(0);
}
已执行的公共无效操作(操作事件e)
{
对象o=e.getSource();
if(o==newItem)
{
cx.erase=true;
cx.repaint();
}
如果(o==quitItem)
{
运行=错误;
停止();
}
}
公共空间鼠标按下(MouseEvent m)
{
cx.start=m.getPoint();
cx.end=cx.start;
cx.cur=cx.start;
}
公共无效鼠标标记(MouseEvent m)
{
cx.cur=m.getPoint();
}
公共无效MouseEvent(MouseEvent m)
{
cx.end=cx.cur;
cx.repaint();
}
公共无效itemStateChanged(ItemEvent e)
{
对象o=e.getSource();
}
公共无效窗口关闭(WindowEvent e)
{
运行=错误;
停止();
}
公共无效mouseClicked(MouseEvent m){}
public void mouseExited(MouseEvent m){}
公共无效mouseenterned(MouseEvent m){}
public void mouseMoved(MouseEvent m){}
公共无效windowClosed(WindowEvent e){}
公共无效窗口已打开(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
公共无效WindowIconnified(WindowEvent e){}
公共无效WindowDeconified(WindowEvent e){}
}
类CanvasEX扩展了Canvas
{
点开始=新点(100100);
点cur=新点(100100);
点结束=新点(100100);
屏幕外图像;
布尔擦除=假;
公共空间更新(图g)
{
//这是将新内容添加到屏幕上,或在“擦除”为真时擦除屏幕
图形缓冲区;
如果(屏幕外==null)
{
offscreen=createImage(getWidth(),getHeight());
}
buffer=offscreen.getGraphics();
如果(删除)
{
setColor(getBackground());
buffer.fillRect(0,08000600);
buffer.dispose();
擦除=假;
}
油漆(缓冲液);
g、 drawImage(屏幕外,0,0,此);
}
公共空间涂料(图g)
{
图形缓冲区=g;
如果(删除)
{
g、 处置();
擦除=假;
}
g、 setColor(E3G04.lineColor);
if(end.x>start.x&&end.y>start.y)
g、 fillRect(start.x,start.y,Math.abs(end.x-start.x),Math.abs(end.y-start.y));
if(end.x>start.x&&end.ystart.y)
g、 fillRect(end.x,start.y,Math.abs(end.x-start.x),Math.abs(end.y-start.y));
if(end.xstart.x&&end.y>start.y)
g、 fillRect(start.x+E3G04.size,start.y+E3G04.size,Math.abs(end.x-start.x)-2*E3G04.size,Math.abs(end.y-start.y)-2*E3G04.size);
if(end.x>start.x&&end.yclass CanvasEX extends Canvas
class CanvasEX extends JPanel
public void paint(Graphics g)
{
..
public void paintComponent(Graphics g)
{
super.paintComponent(g);
..