整数返回方法使组件在JPanel

时间:2016-03-24 20:20:55

标签: 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;
    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()添加到了Panel,但没有任何改变。提前谢谢大家!

2 个答案:

答案 0 :(得分:7)

更改<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.4.1208</version> </dependency> getHeight()方法名称,因为它们已存在于getWidth()中,因此您将覆盖它们。

答案 1 :(得分:2)

你没有&#34;添加&#34;那些方法,你是&#34; Over-riding&#34;它们 - 超类(JPanel)中已存在的行为。乍一看,看起来您的实现将返回负数,这会导致奇怪的行为;超类有自己的实现,可能正在做一些合理的默认行为。

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