Java 整数返回方法使组件在JPanel中不可见

Java 整数返回方法使组件在JPanel中不可见,java,swing,Java,Swing,我有一个很奇怪的问题。我正在为一个类似绘画的程序编写一个接口,并创建了一个扩展JLabel的类。我添加了基本组件和所需变量,如下所示: import javax.swing.*; import java.awt.*; import java.awt.event.*; import shapes.*; public class denemePanel extends JPanel { int radius; int height; int width; Stri

我有一个很奇怪的问题。我正在为一个类似绘画的程序编写一个接口,并创建了一个扩展JLabel的类。我添加了基本组件和所需变量,如下所示:

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

public class denemePanel extends JPanel {

    int radius;
    int height;
    int width;
    String color;
    JButton drawButRect;
    JButton drawButCirc;
    JButton drawButRand;
    JButton paintBut;
    JLabel heightL;
    JLabel widthL;
    JLabel radiusL;
    JLabel colorL;
    JTextField colorF;
    JTextField heightF;
    JTextField widthF;
    JTextField radiusF;
    ShapeContainer shapes;
    boolean drawRect;
    boolean drawCirc;
    boolean drawRand;
    boolean isColor;
    ButtonListener buttonListener;

    public denemePanel( ShapeContainer shapes) {

        height = -1;
        radius = -1;
        width = -1;
        color = "";
        drawRect = false;
        drawCirc = false;
        drawRand = false;
        isColor = false;
        buttonListener = new ButtonListener();
        setLayout( new GridLayout( 0,7, 5, 5));
        setPreferredSize( new Dimension( 700, 150));
        shapes = this.shapes;
        drawButRect = new JButton( "Draw!");
        drawButCirc = new JButton( "Draw!");
        drawButRand = new JButton( "Draw any!");
        paintBut = new JButton( "Paint!");
        drawButRect.addActionListener( buttonListener);
        drawButCirc.addActionListener( buttonListener);
        drawButRand.addActionListener( buttonListener);
        paintBut.addActionListener( buttonListener);
        heightL = new JLabel( "Height:");
        widthL = new JLabel( "Width:");
        radiusL = new JLabel( "Radius:");
        colorL = new JLabel( "Color:");
        heightF = new JTextField( "0");
        colorF = new JTextField( "yellow/red");
        widthF = new JTextField( "0");
        radiusF = new JTextField( "0");
        add( heightL);
        add( heightF);
        add( radiusL);
        add( radiusF);
        add( new JLabel( ""));
        add( colorL);
        add( colorF);
        add( widthL);
        add( widthF);
        add( new JLabel( ""));
        add( new JLabel( ""));
        add( new JLabel( ""));
        add( new JLabel( ""));
        add( new JLabel( ""));
        add( new JLabel( ""));
        add( drawButRect);
        add( new JLabel( ""));
        add( drawButCirc);
        add( drawButRand);
        add( new JLabel( ""));
        add( paintBut);
        setVisible( true);
    }

    //Methods
    public int getRadius() {
        return radius;
    }
    public int getHeight() {
        return height;
    }
    public int getWidth() {
        return width;
    }
    public String getColor() {
        return color;
    }
    //Methods end

    //-------------------------------------ACTION LISTENER-----------------------------------------------
    public class ButtonListener implements ActionListener {

        public void actionPerformed( ActionEvent e) {

            if( e.getSource() == drawButRect) {
                height = Integer.parseInt( heightF.getText());
                width = Integer.parseInt( widthF.getText());
                if( height > 0 && width > 0) {
                drawRect = true;
                drawCirc = false;
                drawRand = false;
                isColor = false;
                }
                else {
                    JOptionPane.showMessageDialog(null, "Not a valid input!");
                    drawRect = false;
                    drawCirc = false;
                    drawRand = false;
                    isColor = false;
                }
            }

            else if( e.getSource() == drawButCirc) {
                radius = Integer.parseInt( radiusF.getText());
                if( radius > 0) {
                    drawRect = false;
                    drawCirc = true;
                    drawRand = false;
                    isColor = false;
                }
                else {
                    JOptionPane.showMessageDialog(null, "Not a valid input!");
                    drawRect = false;
                    drawCirc = false;
                    drawRand = false;
                    isColor = false;  
                }
            }

            else if( e.getSource() == drawButRand) {
                drawRect = false;
                drawCirc = false;
                drawRand = true;
                isColor = false;
            }

            else if( e.getSource() == paintBut) {
                color = colorF.getText().toLowerCase();
                if( color.equals( "red") || color.equals( "yellow")) {
                    drawRect = false;
                    drawCirc = false;
                    drawRand = false;
                    isColor = true;
                }
                else {
                    JOptionPane.showMessageDialog(null, "Not valid color -just red or yellow!");
                    drawRect = false;
                    drawCirc = false;
                    drawRand = false;
                    isColor = false;
                }
            }
        }
    }
    //-----------------------------------------------------------------------------------------------------------
}
在添加“方法”部分之前,我的代码是这样工作的:

在添加方法(只是方法)后,它变成了这样(没有可见的组件,但就是这样,它们实际上在工作!因此,如果我按下一个按钮,我知道它在那里,它的工作原理与它应该的一样):

通过做一些注释和取消注释,我发现String方法与此无关(取消注释并不能解决问题,只进行未注释也不会导致问题)。但是整数返回方法会单独或一起导致此问题

//Methods
public int getRadius() {
    return radius;
}
public int getHeight() {
    return height;
}
public int getWidth() {
    return width;
}
public String getColor() {
    return color;
}
//Methods end
为了解决这个问题,我将setVisible()添加到面板中,但没有任何更改。提前谢谢大家

更改
getHeight()
getWidth()
方法名称,因为它们已经存在于
JPanel
中,所以您正在重写它们。

您不是在“添加”这些方法,而是在“超越”它们-行为已经存在于超类(JPanel)中。乍一看,您的实现似乎会返回负数,这会导致奇怪的行为;超类有它自己的实现,它可能执行了一些合理的默认行为


如果您阅读了JPanel的文档,您可能会发现更多关于该行为的信息。

寻求调试帮助的问题(“为什么该代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现该行为所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建一个最小的、完整的、可验证的示例。我想要的行为是第一个图像,第二个不是。方法(导致此问题的原因)与此问题无关,我不知道如何缩短它-每一点都可能导致此问题。但你是对的,我也分别添加了方法部分。@Deligoz检查我的答案。原因是显而易见的。您正在重写
getHeight()
getWidth()
方法。我感到震惊(我的无知)。谢谢,哇。我已经改变了宽度和高度来测试这个。当我将它们设为10时,只出现了一小部分(“高度:”和“宽度:”字符串,从左起),但将它们设为1000则使整个结构可见。非常感谢您的支持!