以下是发生的事情:
我想制作一个非常基本的绘画程序来练习使用绘画方法,使用图形,使用工具栏等等。我一直在做很多关于如何做这些事情的阅读,而且我确定我在某个地方错过了一个重要的代码行,因为paintpanel本身正在渲染一个不是这样的JMenu。应该在那里。我有一个使用actionListeners设置的JMenu,但是作为额外渲染的那个没有做任何事情而且无法与之交互。这是一张图片:
正如您在下图中看到的那样,我可以在菜单上绘画,但它仍然没有反应。另外,这次你可以看到程序中前一个窗口的radom按钮由于某种原因被添加。
此按钮来自以下位置:
我对如何解决这个问题感到很遗憾,所以这里是相关类的代码:
这是绘画的面板。 包画家;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;
@SuppressWarnings("serial")
public class PaintPanel extends JPanel implements MouseInputListener
{
//These two values will be used to determine where the mouse is and thus where to paint...stuff...
public int xCoordinate, yCoordinate = -10;
static Color currentColor = Color.black;
public PaintPanel()
{
//These two methods simply attaches the mouse listener methods listed below to the actual panel.
addMouseListener(this);
addMouseMotionListener(this);
}
//This method is an overwritten version of the default paint method. It's job is to render custom graphics
//objects on the screen. The Graphics g argument is the item responsible for doing the actual rendering.
@Override
public void paintComponent(Graphics g)
{
// super.paintComponent(g);
g.setColor(currentColor);
g.fillRect(xCoordinate, yCoordinate, 10, 10);
}
@Override
//This method, added because of the MouseInputListener implementation, is used when a user clicks the left mouse button.
public void mouseClicked(MouseEvent e)
{
xCoordinate = e.getX();
yCoordinate = e.getY();
repaint();
}
@Override
public void mouseEntered(MouseEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
//This method, added because of the MouseInputListener implementation, is used when a user presses and holds the
//left mouse button while moving the mouse.
public void mouseDragged(MouseEvent e)
{
xCoordinate = e.getX();
yCoordinate = e.getY();
repaint();
}
@Override
public void mouseMoved(MouseEvent arg0)
{
// TODO Auto-generated method stub
}
public static void changeColor(Color color)
{
currentColor = color;
}
}
这是在PaintPanel,菜单和后来的其他按钮等中添加的主类。
package painter;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import painter.menu.FileMenu;
import painter.menu.ToolsMenu;
@SuppressWarnings("serial")
//This class will do the job of bringing together all the various sub-classes and rendering everything.
public class MainPainterGUI extends JFrame
{
private JPanel menuPanel;
private JMenuBar menuBar;
//These values are going to be used to set an initial window size.
private final short WINDOW_HEIGHT = 1000;
private final short WINDOW_WIDTH = 1000;
//This constructor will do the actual creation of the window.
public MainPainterGUI()
{
//This does the same thing that setTitle does.
super("Painter");
//setTitle("Painter");
//This method will set what the window is supposed to do when the red x is clicked.
//Technically, the value passed is an integer.
setDefaultCloseOperation(EXIT_ON_CLOSE);
//Obviously this method sets the initial size of the window. It can be changed by the user however.
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
//This method sets the center of the window relative to whatever is passed. In this case, null makes
//the window appear in the center of the desktop regardless of the size.
setLocationRelativeTo(null);
menuPanel = new JPanel();
menuBar = new JMenuBar();
menuBar.add(new FileMenu());
menuBar.add(new ToolsMenu());
menuPanel.add(menuBar);
//The add method simply attaches a component to whatever is calling, like a frame or another panel.
//The BorderLayout is needed to use the draggable toolbar.
add(new PaintPanel(), BorderLayout.CENTER);
add(menuPanel, BorderLayout.NORTH);
//This will allow the window to be seen. Make sure this method is last, as if
//a component is created afterwards it may not be visible.
setVisible(true);
}
}
这两个类应该是唯一与当前问题相关的类,但是如果需要其他类,请告诉我。我只是不想用无用的信息弄乱帖子。
现在,我已经尝试过了:
正如您在PaintPanel类中看到的,我添加了一个super.paintComponent(g);方法调用。然而,虽然这完全解决了我的问题,但它一次只绘制一个黑色方块。我会想象,因为它用绘画调用覆盖了面板,然后每次调用重绘时绘制正方形。
这是添加了super.paintComponent(g)的照片:
我不确定这是否重要,但我使用的是Windows 10。
感谢您提供的任何帮助。
答案 0 :(得分:0)
我认为您需要致电.repaint()
以减少您的用户界面。