0

ここで JButton を実行するオプションがあるかどうかを知りたかっただけです。

if(RandomNrJeden <= 50)
{
    JOptionPane.showMessageDialog(frameKontrastGame, "Eggs are not supposed to be green.");
    JButton dialogOdp = new JButton();
}
else
{
    JOptionPane.showMessageDialog(frameKontrastGame, "Eggs are not supposed to be red.");
}

そして、次のように入力します: panel.add(dialogOdp); 外側?

コード全体は次のとおりです。

final JButton continueGame = new JButton();
continueGame.setPreferredSize(new Dimension(1000, 30)); 
continueGame.setLocation(95, 45);
continueGame.setText("<html>Continue</html>");
continueGame.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent ev) {
        panel.remove(continueGame);

        SwingUtilities.updateComponentTreeUI(frameKontrastGame);
        if(RandomNrJeden <= 50)
        {
            JOptionPane.showMessageDialog(frameKontrastGame, "Eggs are not supposed to be green.");
            JButton dialogOdp = new JButton();
        }
        else
        {
            JOptionPane.showMessageDialog(frameKontrastGame, "Eggs are not supposed to be red.");
        } 
    }});


    //final JLabel im = new JLabel(new ImageIcon("kontrast_logo_2.png"));        
    //panel.add(im);        
    panel.add(dialogOdp);        
    panel.add(continueGame);        
    frameKontrastGame.getContentPane().add(panel);        
    frameKontrastGame.setLocationByPlatform(true);        
}});
4

2 に答える 2

0

コードを変更するだけです

    final JButton continueGame = new JButton();
    JButton dialogOdp;     // declare here
    continueGame.setPreferredSize(new Dimension(1000, 30)); 
    continueGame.setLocation(95, 45);
    continueGame.setText("<html>Continue</html>");
    continueGame.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent ev) {
            panel.remove(continueGame);
            SwingUtilities.updateComponentTreeUI(frameKontrastGame);
                if(RandomNrJeden <= 50)
                {
                    JOptionPane.showMessageDialog(frameKontrastGame, "Eggs are not supposed to be green.");
                    dialogOdp = new JButton(); // initialize here


                }
                else
                {
                    JOptionPane.showMessageDialog(frameKontrastGame, "Eggs are not supposed to be red.");
                } 
    }});




//final JLabel im = new JLabel(new ImageIcon("kontrast_logo_2.png"));
//panel.add(im);
if(dialogOdp != null) // check for null
    panel.add(dialogOdp);
panel.add(continueGame);
frameKontrastGame.getContentPane().add(panel);
frameKontrastGame.setLocationByPlatform(true);






}});
于 2013-06-02T18:18:38.440 に答える