我的问题是,如何使用 Action 监听器清除图形并通过再次运行 OtherPanel 来创建一组新图形?
public class MainFrame extends JFrame
{
private OtherPanel panel;
public MainFrame()
{
panel = new OtherPanel();
}
class OtherPanel extends JPanel
{
private OtherPanel()
{
...
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
....
}
private class ReloadListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
}
最佳答案
class OtherPanel extends JPanel
{
private boolean isReset;
private OtherPanel()
{
...
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(!isReset){
//your painting code here
}
}
public void setReset(boolean reset){
isReset = reset;
}
private class ReloadListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
setReset(true);
repaint();
}
}
}
一切都取决于“重置”面板的外观。我只留下了 super.paintComponent()
作为默认外观,您可能想要更改它。当您想在面板上绘制某些内容时,请不要忘记将 setReset(false)
添加到代码中的某个位置。
关于java - 使用 ActionListener 清除 JPanel 中的图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9764156/