单击后,Java按钮消失

时间:2017-04-23 21:08:42

标签: java jbutton

我正在尝试在另一个按钮中使用JOptionPane添加一个按钮,但是在我点击原始按钮后,它一直在消失。

我尝试手动添加JPanel,而不是通过迭代handPanel.buttons使用'handPanelNPC.getHandPanel()',但它不会工作。我检查了ArrayList大小,它已经正确插入。

LayoutTesting.java

public class LayoutTesting extends JFrame{
    HandPanel handPanelNPC = new HandPanel();

    public LayoutTesting(HandPanel handPanel, int type){
        Container pane = getContentPane();

        if (type==1){
            handPanelNPC = handPanel;
        }
        pane.setLayout(new BorderLayout());
        pane.add(handPanelNPC.getHandPanel(), BorderLayout.NORTH);
        validate();
        repaint();
    }

    public LayoutTesting(){
        Container pane = getContentPane();

        pane.setLayout(new BorderLayout());
        pane.add(handPanelNPC.getHandPanel(), BorderLayout.NORTH);
    }
}

HandPanel.java

public class HandPanel implements ActionListener{   
    JFrame frame = null;
    JPanel panel = new JPanel();
    ArrayList<JButton> buttons = new ArrayList<JButton>();

    public HandPanel(){
        addNewButton();
        panel.setLayout(new FlowLayout());
        panel.add(buttons.get(0));
    }

    public JComponent getHandPanel(){
        panel = new JPanel();
        for(int i=0; i<buttons.size(); i++){
            JButton button = buttons.get(i);
            panel.add(button);
        }

        return panel;
    }

    public void addNewButton(){
        JButton button = new JButton();
        button.setPreferredSize(new Dimension(40,58));
        button.addActionListener(this);

        buttons.add(button);
    }

    public void actionPerformed(ActionEvent e) {
      String[] options = {"Summon", "Set", "Add Card"}; 

      int messageType = JOptionPane.QUESTION_MESSAGE;
      int code = JOptionPane.showOptionDialog(
        frame, 
        "What would you like to do?", 
        "Card Name", 
        0, messageType, null, 
        options, options[1]);

      if (code==2){
        addNewButton();
        LayoutTesting frame = new LayoutTesting(this, 1);
      } 
    }
}

Main.java

public class Main extends JFrame{
    public static void main(String[] args){
         LayoutTesting frame = new LayoutTesting();

         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setSize(400, 300);
         frame.setVisible(true);
     }
}

1 个答案:

答案 0 :(得分:0)

您的代码中发生了太多奇怪的事情,无法给出简单的答案。

通过查看HandPanel.java中的这一行来尝试调试自己的代码:

LayoutTesting frame = new LayoutTesting(this, 1);

它到底是做什么的?现在删除该行,该按钮不会消失。现在尝试找出该行正在做什么,以及按钮消失的原因。

另外&#39; panel.add(buttons.get(0));&#39;什么都不做,因为当你打电话时,数组中从不存在按钮(之后在另一种方法中添加按钮)。

这是一个粗略的工作演示,可以让你在第一帧添加任意数量的新卡,每张卡都有一个按钮,可以让你召唤一张新卡。

public class LayoutTesting extends JFrame{
    Container pane;

    //Add card when button is pressed:
    public void addCard(){
        Container card = new JPanel();
        card.setBackground(Color.red);
        JButton newButton = addNewButton();
        newButton.setBackground(Color.red);
        card.add(newButton);
        pane.add(card);
        revalidate();
        repaint();
    }

    //Setup window and add button:
    public LayoutTesting(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setVisible(true);
        setLayout(new FlowLayout());
        pane = new JPanel();
        pane.setLayout(new GridBagLayout());
        JButton firstButton = addNewButton();
        firstButton.setBackground(Color.green);
        add(pane);
        add(firstButton);   
    }

    //Create button and action listener:
    public JButton addNewButton(){
        JButton button = new JButton();
        button.setPreferredSize(new Dimension(40, 58));
        button.addActionListener(new java.awt.event.ActionListener(){
        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt){
            buttonAction(evt);
        }
        });
        return button;
    }

    //Action fer each button:
    public void buttonAction(ActionEvent e) {
        String[] options = {"Summon", "Set", "Add Card"};

        int messageType = JOptionPane.QUESTION_MESSAGE;
        int code = JOptionPane.showOptionDialog(
        this, 
        "What would you like to do?", 
        "Card Name", 
        0, messageType, null, 
        options, options[1]);

        if (code==2){
        addCard();
        }
    }
}