java - 单击 JButton 时动态添加组件到 JPanel

标签 java swing dynamic custom-component

这是我的 block 类:

public class Block extends JComponent {
    public int width, height;
    public Color colour;

    public Block( int width, int height, Color colour) {
        super();
        setSize(new Dimension(width, height));
        setPreferredSize(new Dimension(width, height));
        setBackground(colour);
        this.width = width;
        this.height = height;
        this.colour = colour;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(colour);
        g.fillRect(0, 0, width, height);
    }
}

还有我的球类:

public class Ball extends JComponent {
    public int width, height;
    public Color colour;

    public Ball(int width, int height, Color colour) {
        super();
        setSize(new Dimension(width, height));
        setPreferredSize(new Dimension(width, height));
        setBackground(colour);
        this.width = width;
        this.height = height;
        this.colour = colour;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(colour);
        g.fillOval(0, 0, width, height);
    }
}

这是我的主类(class):

public class Main extends JPanel {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setContentPane(new Main());
        frame.setSize(new Dimension(1000, 1000));
        frame.setPreferredSize(new Dimension(1000, 1000));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public Main() {
        JComboBox<String> shapes = new JComboBox(new String[]{"Square", "Oval"});

        JSpinner width = new JSpinner();
        SpinnerNumberModel widthModel = new SpinnerNumberModel();
        widthModel.setMinimum(1);
        widthModel.setMaximum(200);
        width.setModel(widthModel);

        JSpinner height = new JSpinner();
        SpinnerNumberModel heightModel = new SpinnerNumberModel();
        heightModel.setMinimum(1);
        heightModel.setMaximum(200);
        height.setModel(heightModel);

        JButton submit = new JButton("Add Shape");
        submit.addActionListener((ActionEvent e) -> {
            if (shapes.getSelectedItem().equals("Square")) {
                add(new Block((Integer)widthModel.getValue(), (Integer)heightModel.getValue(), Color.BLUE));
            } else {
                add(new Ball((Integer)widthModel.getValue(), (Integer)heightModel.getValue(), Color.BLUE));                
            }
        });

        add(shapes);
        add(width);
        add(height);
        add(submit);


    }
}

当您单击“提交”按钮时,它应该添加一个正方形或椭圆形(取决于您的选择),并具有您在微调器中指定的高度和宽度。然而,它什么也没有。我打印出了宽度和高度,它们是有效的,所以没有问题。另外,我尝试自行添加自定义组件,并且成功了。

最佳答案

添加组件后调用repaint()方法。

关于java - 单击 JButton 时动态添加组件到 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49752337/

相关文章:

java - 将图像添加到按钮

C++ 模板化类的动态数组(即 vector )

java - 安卓/Java : how to add array to array

java - 如何从一个表中抓取另一表中具有相同类的数据

java - Spring Cloud Kubernetes 应用程序未检测到它何时在 pod 中运行

c# - 当访问存储在动态中的对象的值类型属性时,是否会导致对这些属性的值进行装箱?

javascript - Jquery - 选择特定 div 内的跨度内容

java - 强制 spring boot jackson 解串器使用 BigDecimal

java - 使用 miglayout 的垂直按钮栏布局

java - MigLayout JTextArea 在与 linewrap=true 一起使用时不会缩小