在JButton中显示结果的错误

时间:2015-01-12 10:58:15

标签: java

正如下面这些代码所示,我将在North上创建一个JButton,当我点击它时会改变它的版本,并且有一些问题,单击后这个词不会改变!而且,在我运行问题(测试)之后显示的框架,方形和北方文本栏(JButton)消失了!我应该拉大窗户,然后酒吧和广场会再出来!

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class GrowAndShrinkSquareGUI {
    JFrame frame;
    JButton clickButton;
    GrowAndShrinkListener listener;

    public class SquareDrawPanel extends JPanel {
        int squareSize = 100;
        int width = frame.getWidth();
        int height = frame.getHeight();

        public SquareDrawPanel() {
            super();
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLUE);
            g.fillRect(getWidth() / 2 - squareSize / 2,
                    getHeight() / 2 - squareSize / 2,
                    squareSize, squareSize);
        }
    }

    public class GrowAndShrinkListener implements ActionListener {
        GrowAndShrinkListener(JButton button) {
            button = new JButton("Click me to grow the Square");
            frame.add(button, BorderLayout.NORTH);
            button.addActionListener(this);
        }

        public void actionPerformed(ActionEvent e) {
            Object source = e.getSource();
            if (source instanceof JButton) {
                clickButton = (JButton) source;
                clickButton.setText("Click to shrink square");
            }
        }
    }

    public static void main(String[] args) {
        GrowAndShrinkSquareGUI test = new GrowAndShrinkSquareGUI();
        test.go();
    }

    private void createListenerButton() {
        listener = new GrowAndShrinkListener(clickButton);
    }

    public void go() {
        frame = new JFrame();
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        SquareDrawPanel squareDraw = new SquareDrawPanel();
        createListenerButton();
        Container cpane = frame.getContentPane();
        cpane.add(squareDraw, BorderLayout.CENTER);
        // 5) set JButton to show text
        clickButton = new JButton("Click me to grow the square"); 
        cpane.add(clickButton, BorderLayout.NORTH);
    }
}

1 个答案:

答案 0 :(得分:1)

您的问题是您在分配侦听器后创建了按钮。将createListenerButton()来电移至go()内的最后一行。

public void go() {
    // ...
    clickButton = new JButton("Click me to grow the square");
    cpane.add(clickButton, BorderLayout.NORTH);
    createListenerButton();
   // ...
}

看起来您还要添加按钮两次。你这太复杂了。

public class GrowAndShrinkListener // ...
    frame.add(button, BorderLayout.NORTH);
    // ....
}

public void go() // ...
    cpane.add(clickButton, BorderLayout.NORTH);
    // ....
}

尝试以下方法:

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

public class GrowAndShrinkSquareGUI {
    private JFrame frame;
    private JButton clickButton;
    private GrowAndShrinkListener listener;

    public class SquareDrawPanel extends JPanel {
        int squareSize = 100;
        int width = frame.getWidth();
        int height = frame.getHeight();

        public SquareDrawPanel() {
            super();
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLUE);
            g.fillRect(getWidth() / 2 - squareSize / 2, getHeight() / 2
                    - squareSize / 2, squareSize, squareSize);
        }
    }

    public class GrowAndShrinkListener implements ActionListener {
        private JButton button;

        public GrowAndShrinkListener(JButton button) {
            this.button = button;
            this.button.addActionListener(this);
        }

        public void actionPerformed(ActionEvent e) {
            Object source = e.getSource();
            if (source instanceof JButton && ((JButton) source) == button) {
                button.setText("Click to shrink square");
            }
        }
    }

    private void createListenerButton(JButton button) {
        listener = new GrowAndShrinkListener(button);
    }

    public void go() {
        frame = new JFrame();
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        SquareDrawPanel squareDraw = new SquareDrawPanel();
        Container cpane = frame.getContentPane();
        cpane.add(squareDraw, BorderLayout.CENTER);
        // 5) set JButton to show text
        clickButton = new JButton("Click me to grow the square");
        cpane.add(clickButton, BorderLayout.NORTH);
        createListenerButton(clickButton);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                GrowAndShrinkSquareGUI test = new GrowAndShrinkSquareGUI();
                test.go();
            }
        });
    }
}