单击按钮后向JPanel添加按钮

时间:2014-02-24 01:49:52

标签: java swing user-interface jpanel

好的,所以我正在尝试制作一个Flash卡程序,我希望它能够在单击添加设置(或新的卡片组)时添加一个按钮来表示底部的设置。框架。

我现在所拥有的只会在调整帧大小后显示按钮。我真的不知道我正在对面板做什么,因为我还没弄清楚如何正确布置所有部件。

我需要程序在设置完成后在底部面板中显示设置图标。

import javax.imageio.ImageIO;
import javax.swing.*;

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

public class GraphicsUI extends JPanel {

private DeckList setsList;
CardActions action = new CardActions();

JButton addSetButton, addCardButton;

private JLabel label;
private JPanel bottomPanel;

public GraphicsUI(){
    this.setPreferredSize(new Dimension(1000,825));
    this.setBackground(Color.LIGHT_GRAY);




    this.label = new JLabel();
    label.setOpaque(true);
    label.setBackground(Color.white);
    label.setPreferredSize(new Dimension(600, 400));



    ImageIcon img = new ImageIcon("setIcon.png");       
    //make that set image at bottom



this.addSetButton = new JButton("Add Set");
//      this.addSetButton.setBackground(Color.white);
//      this.addSetButton.setPreferredSize(new Dimension(240, 180));
        this.add(addSetButton, BorderLayout.WEST);
        this.addSetButton.addActionListener(new ButtonListener());


    this.addCardButton = new JButton("Add Card");
//  this.addCardButton

    this.add(label);

    JLabel blah = new JLabel();
    blah.setPreferredSize(new Dimension(1000,30));
    this.add(blah);



    this.bottomPanel = new JPanel();

    this.bottomPanel.setPreferredSize(new Dimension(1000, 400));
    this.add(bottomPanel);





}


public class ButtonListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        action.setCommand(CardActions.Command.ADDSET);

        action.setList(getSetInfo());


    }

}

private CardList getSetInfo(){
    CardList cl = new CardList();
    String setName = JOptionPane.showInputDialog("Enter the name of your set.");

    if(setName.isEmpty()){
        JOptionPane.showMessageDialog(this, "Cannot have an empty set.");
    }

        cl.setSetName(setName);
        ImageIcon img = new ImageIcon("setIcon.png");
        bottomPanel.add(new JButton(img));
            return cl;

}

}

2 个答案:

答案 0 :(得分:1)

添加完按钮后,尝试调用容器上的revalidate来更新容器层次结构。

您可能还会发现使用setPreferredSize可能没有足够的空间让新按钮出现

答案 1 :(得分:0)

你应该创建一个新的jpanel,它只包含可以将你带到用户创建的特定集的按钮。更新jpanel只需调用repaint()

相关问题