java - JPanels,使用监听器改变大小

标签 java oop jpanel

我正在尝试做一个 JPanel,每次单击按钮时都可以更改其大小。我的方法是创建两个不同尺寸的面板。一旦点击,其中一个就会变得可见,而另一个则不可见。到目前为止,我已经设法使第一个隐形,但我被困在那里。我的方法好用吗?我错过了什么?这里是代码...

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;


public class GrowAndShrinkSquareGUI {
    JFrame frame;
    SquareDrawPanel greenPanel;
    SquareDrawPanel greenPanel2;

    public class SquareDrawPanel extends JPanel {

        int locationX;
        int locationY;
        int width;
        int height;

        SquareDrawPanel(int x, int y, int w, int h) {

            locationX = x;
            locationY = y;
            width = w;
            height = h;

        }

        public void paintComponent(Graphics g) {

            g.setColor(Color.green);
            g.fillRect(locationX, locationY, width, height);

        }

    }

    public class growAndShrinkListener implements ActionListener {

        JButton button;

        public growAndShrinkListener() {

            JButton button = new JButton("Click me to grow the Square");
            frame.add(button, BorderLayout.NORTH);
            button.addActionListener(this);}


        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.print("clicked");        
            greenPanel.setVisible(false);
            greenPanel2.setVisible(true);

        }}

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

        test.go();

    }

    public void go() {

        frame = new JFrame();
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        drawPanel(greenPanel);
        drawPanel(greenPanel2);

        growAndShrinkListener button = new growAndShrinkListener();
        //addButton(CreateJButton());

    }

    private JPanel createRectPanel(int x, int y) {

        greenPanel = new SquareDrawPanel(x, y, 100, 100);

        return greenPanel;
    }

    private JPanel createRectPanel2(int x, int y) {

        greenPanel2 = new SquareDrawPanel(x, y, 200, 200);

        return greenPanel2;
    }

    private void drawPanel(JPanel panel) {
        panel = createRectPanel(setLocationX(), setLocationY());
        frame.add(panel, BorderLayout.CENTER); // DoesNot run properly
    }

    private int setLocationX() {

        int centeredX = frame.getWidth() / 2 - 50;
        return centeredX;
    }

    private int setLocationY() {

        int centeredY = frame.getHeight() / 2 - 75;

        return centeredY;
    }


}

最佳答案

public class CustomFrame extends JFrame implements ActionListener
{

    //Creating an object of this class shows a frame which toggles 
    //between these two sizes on a button click
    private static final Dimension FIRST_SIZE = new Dimension(200, 200);
    private static final Dimension SECOND_SIZE = new Dimension(400, 400);

    public CustomFrame()
    {
        //Add a button to the frame and register an action listener
        //to the current object
        JButton button = new JButton("Change size");
        button.addActionListener(this);

        getContentPane().add(button);

        //Make the frame visible
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent ae)
    {
        //This gets executed when the button is clicked
        //
        //We're setting the size to a new value;
        //first, we fetch the current size and 
        //check if it's equal to the first size
        setSize(getSize().equals(FIRST_SIZE)
                //If so, set the frame to the second size
                ? SECOND_SIZE
                //In all other cases, make the frame's 
                //size the value of FIRST_SIZE
                : FIRST_SIZE);
    }
}

关于java - JPanels,使用监听器改变大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27943784/

相关文章:

java - 如何更改 JFrame 内的 JPanel?

java - 将 InterstitialAd 与 InterstitialAdListener 结合使用。获取错误消息

java - 特定类的异常处理/映射

PHP - 如何组合三个或更多相互依赖的类?

perl - 从其他类方法重定向打印流

java - JPanel.getWidth() 不准确。为什么?

java - 如何读取图像,在其上绘制内容并保存结果?

java - 自动运行外部应用程序,使用 Cucumber 来测试主应用程序

java - 在 Java 中显示 unicode 字符

c++ - 在 GUI 设计中避免 dynamic_cast