【发布时间】:2014-01-13 02:01:27
【问题描述】:
我在我的框架中显示了一些 Graphics2D 图形,然后我在整个框架中添加了一个 JPanel,以便我可以在面板中的任意位置单击鼠标侦听器。但是,图形消失了,我假设被框架挡住了。我尝试将面板设置为可见错误,但这没有做任何事情。如何让我的 mouselistener 监听整个窗口,并且仍然显示我的图形?
编辑: 这是一些代码:编辑:(还有更多)
//adding the panel and mouselistener
JPanel allPanel = new JPanel();
allPanel.addMouseListener(this);
frame.add(allPanel);
//...
//drawing some of the graphics
public void draw() {
frame.add(new CustomPaintComponent());
// Display the frame
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
JPanel allPanel = new JPanel();
allPanel.addMouseListener(this);
frame.add(allPanel);
}
static class CustomPaintComponent extends Component {
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.fillRoundRect(10, 10, 50, 50, 10,10);
//...
【问题讨论】:
-
你能告诉我们更多关于你的问题吗?也许你可以给一些代码。
-
了解还有什么会有所帮助?我认为我的代码不会有太大帮助,但我可以添加一点。
-
就目前而言,我不知道您的问题是什么。你说你的frame中有图形显示,但是它是怎么显示的呢?
-
我编辑添加代码以显示图形的显示方式
-
图形在框架的什么位置显示?在
paint方法中?在paintComponent方法中?
标签: java swing jpanel graphics2d mouselistener