Java GUI programming: coin game

Source: Internet
Author: User

Java GUI programming: coin game
Preface: I am very interested in learning java se GUI programming. I don't know why the teacher told me not to spend too much time on GUI programming. "I promise you won't be able to use java GUI all your life !" Tang yf's words are still lingering in my ears! I learned a lot from him, which is undeniable. But what I think about is this book Java programming. Liang Yong! I was so bold that I never dared to look straight into Thanking in java. I bought Thanking in java last year and now I have a genuine one. I still keep it on my shelves!
I learned 16 chapters and obviously slowed down. For another reason, almost every example is a small case or a small project! Learning is interesting and challenging! However, the Knowledge System of java is still very long. In order to catch up with the progress, I had to discard the precision! There will be internships next semester. Therefore, there will be 36 exercises in chapter 16, or 36 interesting cases. You can only write these 11 questions! Instead of finishing every question, just jump to the last question!

Exercise 16.36: write a program that displays the front H or back T of nine coins ,. When you click a grid, the coin is turned over. A grid is a JLabel. Write a custom lattice class that extends the JLabel and uses the mouse listener to process these clicks. When the program starts, all the grids are initialized to H.


Train of Thought Analysis: first write a main class, which inherits JFrame and is used to display the frame size and position! Among them, a member attribute such as has a Panel, which inherits JPanel. The panel uses a grid layout and a 9x9 grid layout. Place a Label on each grid. label is defined by itself and inherited from JLabel. The default value is "H". When a Jlabel mouse event is triggered, the display content of the label is changed!

Well, the above is the analysis idea. Maybe I cannot express it clearly. However, you may have a clear idea of the Code!


 
 
  1. Import java. awt. BorderLayout;

  2. Import javax. swing .*;
  3. /*
  4. * Function: displays nine coins. The front is H or the opposite is T. When you click a grid, the coin is turned over.
  5. * Date: 2015.4.1
  6. */

  7. Public class OpenCoin extends JFrame {
  8. Private CoinPanel cp = null;

  9. Public OpenCoin (){
  10. Cp = new CoinPanel ();
  11. This. add (cp, BorderLayout. CENTER );
  12. }

  13. Public static void main (String [] args ){
  14. OpenCoin frame = new OpenCoin ();
  15. Frame. setTitle ("coin game ");
  16. Frame. setSize (300,300 );
  17. Frame. setLocation (400,200 );
  18. Frame. setVisible (true );
  19. }
  20. }


 
 
  1. import java.awt.*;
  2. import javax.swing.*;

  3. public class CoinPanel extends JPanel {
  4. private CoinLabel[] jlbs = new CoinLabel[9];

  5. public CoinPanel() {
  6. this.setLayout(new GridLayout(3, 3, 1, 1));

  7. for(int i = 0; i < 9; i++) {
  8. jlbs[i] = new CoinLabel();
  9. jlbs[i].setHorizontalAlignment(SwingConstants.CENTER);
  10. jlbs[i].setFont(new Font("TimesRoman", Font.PLAIN, 30));
  11. jlbs[i].setBorder(BorderFactory.createBevelBorder(1));
  12. this.add(jlbs[i]);
  13. }
  14. }
  15. }


 
 
  1. import java.awt.event.*;

  2. import javax.swing.*;

  3. public class CoinLabel extends JLabel {
  4. private boolean isFont = true;

  5. public CoinLabel() {
  6. this("H");
  7. }

  8. public CoinLabel(String s) {
  9. super(s);

  10. this.addMouseListener(new MouseAdapter() {

  11. @Override
  12. public void mousePressed(MouseEvent e){
  13. CoinLabel.this.isFont = !CoinLabel.this.isFont;

  14. if(isFont) {
  15. CoinLabel.this.setText("H");
  16. } else {
  17. CoinLabel.this.setText("T");
  18. }
  19. }
  20. });
  21. }
  22. }

Share with the children's shoes that are learning java!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.