简体   繁体   English

单击后,Java按钮消失

[英]Java Button disappears after clicking

I'm trying to add a button using an JOptionPane in another button, but after I clicked the original button just keeps disappearing. 我正在尝试在另一个按钮中使用JOptionPane添加一个按钮,但是在我点击原始按钮后,它就会一直消失。

I've tried adding the JPanel manually instead of using 'handPanelNPC.getHandPanel()' by iterating through the handPanel.buttons but it stil wont work. 我已经尝试手动添加JPanel,而不是通过迭代handPanel.buttons使用'handPanelNPC.getHandPanel()',但它不会工作。 I've checked the ArrayList size and it is already inserted properly. 我检查了ArrayList大小,它已经正确插入。

LayoutTesting.java 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 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 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);
     }
}

You have too many odd things happening in your code to give a simple answer. 您的代码中发生了太多奇怪的事情,无法给出简单的答案。

Try debugging your own code by looking at this line in HandPanel.java: 尝试通过查看HandPanel.java中的这一行来调试自己的代码:

LayoutTesting frame = new LayoutTesting(this, 1);

What does it really do? 它到底是做什么的? Now remove that line and the button will not disappear. 现在删除该行,该按钮不会消失。 Now try and work out what that line was doing, and why the button disappeared. 现在尝试找出该行正在做什么,以及为什么按钮消失了。

Also 'panel.add(buttons.get(0));' 'panel.add(buttons.get(0));' does nothing because there is never a button in the array when you make that call (You add the button afterwards in another method). 什么都不做,因为当你进行那个调用时,数组中从不存在按钮(之后在另一个方法中添加按钮)。

Here is a rough working demo that lets you add as many new cards as you want to the first frame, and each card has a button that will let you summon a new card. 这是一个粗略的工作演示,可以让你在第一帧添加任意数量的新卡,每张卡都有一个按钮,可以让你召唤一张新卡。

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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM