Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.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 试图理解一个已经生成的JApplet代码_Java_Swing_Graphics_Actionlistener_Japplet - Fatal编程技术网

Java 试图理解一个已经生成的JApplet代码

Java 试图理解一个已经生成的JApplet代码,java,swing,graphics,actionlistener,japplet,Java,Swing,Graphics,Actionlistener,Japplet,问题:正如您所看到的,当我们执行此代码并单击圆形按钮时,它会正确计数,但当单击方形按钮时,它会再次正确计数,但再次单击圆形按钮时,先前的圆形计数会消失,并且再次从1开始 问题出在哪里:在做了一点搜索之后,我知道ContentPane对象上调用的getGraphics()方法导致了问题,我尝试更改它,但是程序甚至没有运行,如果它运行,那么所有GUI组件都会有问题 编辑:你做了mKorbel说的吗 import java.awt.*; import java.awt.event.*; import

问题:正如您所看到的,当我们执行此代码并单击圆形按钮时,它会正确计数,但当单击方形按钮时,它会再次正确计数,但再次单击圆形按钮时,先前的圆形计数会消失,并且再次从1开始

问题出在哪里:在做了一点搜索之后,我知道ContentPane对象上调用的getGraphics()方法导致了问题,我尝试更改它,但是程序甚至没有运行,如果它运行,那么所有GUI组件都会有问题

编辑:你做了mKorbel说的吗

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CountShapes1 extends JApplet implements ActionListener
{
    Container cont;
    JPanel p;

    JLabel lblCount;
    JButton btCircle,btSquare;

    boolean blnCircle,blnSquare;
    int count=0;
    String shape="";

    public void init() 
    {
        cont=getContentPane();

        lblCount=new JLabel("Shape Count : 0",JLabel.CENTER);
        lblCount.setFont(new Font("Arial",Font.BOLD,18));
        cont.add(lblCount,BorderLayout.NORTH);

        btCircle=new JButton("Circle");
        btSquare=new JButton("Square");

        btCircle.addActionListener(this);
        btSquare.addActionListener(this);

        p=new JPanel();
        p.add(btCircle);
        p.add(btSquare);

        cont.add(p,BorderLayout.SOUTH);
    }

     public void actionPerformed(ActionEvent ae)
     {
        if(ae.getSource()==btCircle)
        {
            if(blnSquare==true)
            {
                blnSquare=false;
                count=0;
            }

            blnCircle=true;
            shape="CIRCLE";
            count++;

        lblCount.setText(shape+" Count : "+count);
        repaint();
    }

    if(ae.getSource()==btSquare)
    {
        if(blnCircle==true)
        {
                blnCircle=false;
                count=0;
        }
        blnSquare=true;
        shape="SQUARE";
        count++;

        lblCount.setText(shape+" Count : "+count);
        repaint();
    }
}

    public void paint(Graphics g)
   {
       cont.paint(cont.getGraphics());

       int x=10,y=30,w=30,h=30;
       if(shape.equals("CIRCLE") || shape.equals("SQUARE"))
       {
         for(int i=0;i<count;i++)
         {
            if(shape.equals("CIRCLE"))
            {
                g.drawOval(x,y,w,h);
            }
            else
            {
                g.drawRect(x,y,w,h);
            }
             x+=40;
                if(x>=getWidth()-30)
                {
                    x=10;
                    y+=40;
                }
         }  //for -loop finished

      }  // if-finished
   } // paint() finished
 } // class finished

/*
   <applet code="CountShapes1" width=500 height=500>
   </applet>
*/
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类CountShapes1扩展了JApplet实现ActionListener
{
集装箱运输;
JPanel p;
JLabel-lblCount;
JButton btCircle,btSquare;
布尔blnCircle,blnSquare;
整数计数=0;
字符串形状=”;
公共void init()
{
cont=getContentPane();
lblCount=新的JLabel(“形状计数:0”,JLabel.CENTER);
lblCount.setFont(新字体(“Arial”,字体粗体,18));
cont.add(lblCount,BorderLayout.NORTH);
btCircle=新的JButton(“Circle”);
btSquare=新的JButton(“Square”);
btCircle.addActionListener(这个);
btSquare.addActionListener(这个);
p=新的JPanel();
p、 添加(btCircle);
p、 添加(btSquare);
继续添加(p,南部边界布局);
}
已执行的公共无效行动(行动事件ae)
{
如果(ae.getSource()==btCircle)
{
如果(blnSquare==true)
{
blnSquare=false;
计数=0;
}
blnCircle=真;
shape=“圆形”;
计数++;
lblCount.setText(形状+“计数:”+Count);
重新油漆();
}
如果(ae.getSource()==btSquare)
{
如果(blnCircle==真)
{
blnCircle=假;
计数=0;
}
blnSquare=true;
shape=“SQUARE”;
计数++;
lblCount.setText(形状+“计数:”+Count);
重新油漆();
}
}
公共空间涂料(图g)
{
cont.paint(cont.getGraphics());
int x=10,y=30,w=30,h=30;
if(shape.equals(“圆”)| | shape.equals(“正方形”))
{
对于(int i=0;i=getWidth()-30)
{
x=10;
y+=40;
}
}//for-loop已完成
}//如果完成
}//绘制()已完成
}//下课了
/*
*/

我认为以前的开发人员在
cont
上调用
paint
是一个错误。这将作为零件自动完成,如果零件仍在加工,我会将其替换为
super.paint

我怀疑原作者不知道如何一次绘制多个形状

paint
方法一次只能绘制一个形状。除非将形状添加到某种类型的列表中,然后进行循环,否则每次调用
paint
方法时,每次调用
paint
时,它都会删除以前的内容,只绘制命名的形状

您确实应该创建一个自定义组件,扩展类似于
JPanel
的表单,并覆盖其
paintComponent
方法。在这种情况下,您应该绘制形状


当前实现的问题是,形状可能会在控件上绘制,这很可能是不可取的

+1:-)不理解,也没有问题,也许你是对的:-)为了获得更好的帮助,请尽快发布简短、可运行、可编译、精简的代码(重要问题)关于你问题中的描述