2014-07-25 24 views
0

我需要幫助這個代碼,我試圖做一個簡單的cookie點擊器類型的遊戲,我已經完成了大部分代碼,但由於某種原因,當我嘗試添加JLabel的框架,它創造了一個錯誤,我希望你們中的一個人能夠幫助我,我對Java相當陌生,感謝您的幫助!我想要一個JLabel出現後按JButton

//Variables 
static int clicked = 0; 
private FlowLayout layout; 
private Container container;  

public static void main(String [] args) { 

    //Declaring the buttons, panels, etc... 
    JButton button = new JButton("Click"); 
    JPanel panel = new JPanel(); 
    panel.add(button); 

    final JFrame frame = new JFrame("Button Pressed"); 
    frame.setSize(400, 200); 
    frame.setVisible(true); 
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    frame.add(panel); 

    //Action Listener Code 
    button.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) { 
      //Execute when button is pressed 
      clicked++; 
      System.out.println("Button pressed " + clicked + " times!"); 
     } 
    }  
} 

回答

1

您可以添加JLabel然後更新它的文本按鈕被點擊時。

說明:最後在調用JFrame.setVisible(true)時添加所有組件。

示例代碼:

// Declaring the buttons, panels, etc... 
JButton button = new JButton("Click"); 
final JLabel label = new JLabel(); 

button.addActionListener(new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent arg0) { 
     clicked++; 
     label.setText("Button pressed " + clicked + " times!"); 
    } 
}); 

JPanel panel = new JPanel(); 
panel.add(button); 
panel.add(label); 

final JFrame frame = new JFrame("Button Pressed"); 
frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
frame.add(panel); 
frame.setVisible(true); 

找到更多的例子herehere

+0

非常感謝的人! – Mrsoldier3201

+0

不客氣。 – Braj

+0

@ Mrsoldier3201你應該接受答案,如果它真的幫助你:) –

1

基本原理比較簡單。爲了向其他東西添加內容,首先需要訪問(或引用)您要添加的內容。

雖然有很多方法可以實現這一點,但最簡單的方法可能是使用實例/類字段。然後,該場將在類內的任何地方訪問,例如

import java.awt.EventQueue; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import static javax.swing.JFrame.EXIT_ON_CLOSE; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class ClickTest { 

    public static void main(String[] args) { 
     new ClickTest(); 
    } 

    private JPanel panel; 

    public ClickTest() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       //Declaring the buttons, panels, etc... 
       JButton button = new JButton("Click"); 

       panel = new JPanel(); 
       panel.add(button); 

       final JFrame frame = new JFrame("Button Pressed"); 
       frame.setSize(400, 200); 
       frame.setVisible(true); 
       frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
       frame.add(panel); 

       button.addActionListener(new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         panel.add(new JLabel("You clicked me")); 
         panel.revalidate(); 
        } 
       }); 
      } 
     }); 
    } 
} 

看看Creating a GUI With JFC/SwingUnderstanding Class Members更多細節

相關問題