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!
- Import java. awt. BorderLayout;
- Import javax. swing .*;
- /*
- * Function: displays nine coins. The front is H or the opposite is T. When you click a grid, the coin is turned over.
- * Date: 2015.4.1
- */
- Public class OpenCoin extends JFrame {
- Private CoinPanel cp = null;
- Public OpenCoin (){
- Cp = new CoinPanel ();
- This. add (cp, BorderLayout. CENTER );
- }
- Public static void main (String [] args ){
- OpenCoin frame = new OpenCoin ();
- Frame. setTitle ("coin game ");
- Frame. setSize (300,300 );
- Frame. setLocation (400,200 );
- Frame. setVisible (true );
- }
- }
- import java.awt.*;
- import javax.swing.*;
- public class CoinPanel extends JPanel {
- private CoinLabel[] jlbs = new CoinLabel[9];
- public CoinPanel() {
- this.setLayout(new GridLayout(3, 3, 1, 1));
- for(int i = 0; i < 9; i++) {
- jlbs[i] = new CoinLabel();
- jlbs[i].setHorizontalAlignment(SwingConstants.CENTER);
- jlbs[i].setFont(new Font("TimesRoman", Font.PLAIN, 30));
- jlbs[i].setBorder(BorderFactory.createBevelBorder(1));
- this.add(jlbs[i]);
- }
- }
- }
- import java.awt.event.*;
- import javax.swing.*;
- public class CoinLabel extends JLabel {
- private boolean isFont = true;
- public CoinLabel() {
- this("H");
- }
- public CoinLabel(String s) {
- super(s);
- this.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e){
- CoinLabel.this.isFont = !CoinLabel.this.isFont;
- if(isFont) {
- CoinLabel.this.setText("H");
- } else {
- CoinLabel.this.setText("T");
- }
- }
- });
- }
- }
Share with the children's shoes that are learning java!