Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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 为什么菜单栏显示两次,而绘制的线条只显示在页面的四分之一?_Java_Swing - Fatal编程技术网

Java 为什么菜单栏显示两次,而绘制的线条只显示在页面的四分之一?

Java 为什么菜单栏显示两次,而绘制的线条只显示在页面的四分之一?,java,swing,Java,Swing,我正在学习Java。我有一个问题,当然是基本问题 菜单栏显示两次,绘制的线条仅显示在页面的四分之一处 你认为专家组和JMenuBar之间有关系吗 您认为问题与repaint()方法有关吗 代码如下: 班级小组: public class Panel extends JPanel { private String colorName = "All"; private Color color = Color.RED; private String sha

我正在学习Java。我有一个问题,当然是基本问题

菜单栏显示两次,绘制的线条仅显示在页面的四分之一处

你认为专家组和JMenuBar之间有关系吗

您认为问题与repaint()方法有关吗

代码如下:

班级小组:

public class Panel extends JPanel {

    private String colorName = "All";
    private Color color = Color.RED;
    private String shape = "Circle";
    private int size1 = 30;
    private int x = 0, y = 0;
    private boolean allow = false;
    

    @Override
    public void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);

        if (allow == true) {
            if (colorName == "Red")
                g.setColor(Color.RED);
            else if (colorName == "Blue")
                g.setColor(Color.BLUE);
            else if (colorName == "All") {
                int r = (int) ((Math.random()) * 256);
                int v = (int) ((Math.random()) * 256);
                int b = (int) ((Math.random()) * 256);

                color = new Color(r, v, b);
                g.setColor(color);
            }
            if (shape == "Circle")
                g.fillOval(x, y, size1, size1);
            if (shape == "Square")
                g.fillRect(x, y, size1, size1);
            
        }
    repaint();
        
        
    }

    public String getColorName() {
        return colorName;
    }

    public void setColorName(String colorName) {
        this.colorName = colorName;

    }

    public boolean isAllow() {
        return allow;
    }

    public void setAllow(boolean allow) {
        this.allow = allow;
    }

    public String getShape() {
        return shape;
    }

    public void setForme(String forme) {
        this.shape = shape;

    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getSize1() {
        return size1;
    }

    public void setSize1(int size1) {
        this.size1 = size1;
    }

    

}

类框架:

public class Frame extends JFrame{
   
    JMenuBar mb = new JMenuBar();
    JMenu file = new JMenu("File");
    JMenu edit = new JMenu("Edit");
    
    Panel p = new Panel();
    public Frame() {
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setSize(400,400);
        this.setTitle("Test");
        this.addMouseListener(new MouseAdapter(){


            

            @Override
            public void mousePressed(MouseEvent e) {
                int a = e.getX() - (p.getSize1()/2);
                int b = e.getY() - (p.getSize1());
                
                p.setX(a);
                p.setY(b);
                p.setAllow(true);
                
                
            }

        
            
        });
        this.addMouseMotionListener(new MouseMotionListener() {
            
            @Override
            public void mouseMoved(MouseEvent e) {
                
            }
            
            @Override
            public void mouseDragged(MouseEvent e) {
                
                int a = e.getX() - (p.getSize1()/2);
                int b = e.getY() - (p.getSize1());
                
                p.setX(a);
                p.setY(b);
                p.setAllow(true);
                
            }
        });
        mb.add(file);
        mb.add(edit);
        this.setJMenuBar(mb);
        this.setLayout(new BorderLayout());
        this.add(p, BorderLayout.CENTER);       
                
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new Frame();
    }
}

你能帮帮我吗?

你的代码中有一个非常严重的排版错误:

@Override
public void paintComponent(Graphics g) {
    // TODO Auto-generated method stub
    super.paintComponents(g);
应该是:

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
请注意超级调用中的差异,因为
paintComponents
!=<代码>油漆组件


另外,不要使用
==
比较字符串=。请改用
equals(…)
equalsIgnoreCase(…)
方法。了解
==
检查两个对象引用是否相同,这不是您感兴趣的。另一方面,这些方法检查两个字符串是否具有相同顺序的相同字符,这就是这里的问题所在

因此,不是:

if (colorName == "Red") {
    // ....
}
而是:

if (colorName.equalsIgnoreCase("Red")) {
    // ....
}

另外,从不在绘制方法中调用
repaint()
。另外,不要随机化绘制或更改paintComponent中的对象状态,因为您无法控制何时或是否调用该方法。在监听器(如鼠标监听器)中随机化,如果需要与鼠标监听器提供的动画分开的动画,请使用摆动计时器


同时 这:


重写从组件类继承的JPanel的方法,该方法是放置JPanel的关键,与
getY()
相同。尽快重命名这些方法

您的代码中有一个非常严重的排版错误:

@Override
public void paintComponent(Graphics g) {
    // TODO Auto-generated method stub
    super.paintComponents(g);
应该是:

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
请注意超级调用中的差异,因为
paintComponents
!=<代码>油漆组件


另外,不要使用
==
比较字符串=。请改用
equals(…)
equalsIgnoreCase(…)
方法。了解
==
检查两个对象引用是否相同,这不是您感兴趣的。另一方面,这些方法检查两个字符串是否具有相同顺序的相同字符,这就是这里的问题所在

因此,不是:

if (colorName == "Red") {
    // ....
}
而是:

if (colorName.equalsIgnoreCase("Red")) {
    // ....
}

另外,从不在绘制方法中调用
repaint()
。另外,不要随机化绘制或更改paintComponent中的对象状态,因为您无法控制何时或是否调用该方法。在监听器(如鼠标监听器)中随机化,如果需要与鼠标监听器提供的动画分开的动画,请使用摆动计时器


同时 这:


重写从组件类继承的JPanel的方法,该方法是放置JPanel的关键,与
getY()
相同。尽快重命名这些方法是的,你是对的,但这并没有解决问题。@RIDI7788:另外,不要使用
==
比较字符串=。请改用
equals(…)
equalsIgnoreCase(…)
方法。了解
==
检查两个对象引用是否相同,这不是您感兴趣的。另一方面,方法检查两个字符串是否具有相同顺序的相同字符,这就是这里的问题所在。好的,我尝试过,但问题仍然存在,您认为我应该更改Frame类中的某些内容吗?@RIDI7788:您在代码中遇到了很多问题。注意编辑以回答好的,但行仍然显示在页面的四分之一处。是的,你是对的,但这并没有解决问题。@RIDI7788:另外,不要使用
==
比较字符串=。请改用
equals(…)
equalsIgnoreCase(…)
方法。了解
==
检查两个对象引用是否相同,这不是您感兴趣的。另一方面,方法检查两个字符串是否具有相同顺序的相同字符,这就是这里的问题所在。好的,我尝试过,但问题仍然存在,您认为我应该更改Frame类中的某些内容吗?@RIDI7788:您在代码中遇到了很多问题。注意编辑答案好的,但是这些行仍然显示在页面的四分之一处。不要将你的类称为“面板”和“框架”。已经有AWT组件具有这些类名,因此很容易混淆。类名应该更具描述性。好的,谢谢!不要把你的类称为“面板”和“框架”。已经有AWT组件具有这些类名,因此很容易混淆。类名应该更具描述性。好的,谢谢!