I have a project in school.  This is the game that I've created, but I need to make some improvements. I put a swing.timer inside the action event to create a score/time decrements effects.  But, I have a problem because there is a textfield inside the frame and I can't type any words there because of the timer.setRepeats(true). This is the only way that I know at the moment.  If you have some problem in understanding my code, please tell me and I will try to explain.  I need some improvement/suggestion for my program.import java.awt.BorderLayout;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.Timer;public class Puzzle extends JFrame {    /**     * author: Victor Emmanuel C. Estomo     */    private static final long serialVersionUID = 1L;    static int l = 1;    static int sx = 100000;    static int sx1 = 10000 ;    static int tx = 4060;    public static void main(String[] args) {        //Frame        final JFrame f = new JFrame();        f.setTitle(Picture Puzzle);        f.setSize(500,500);        f.setLocationRelativeTo(null);        f.setResizable(false);        f.setDefaultCloseOperation(EXIT_ON_CLOSE);        f.setVisible(true);        //panels in the game        //start panels        final JPanel p1 = new JPanel(new GridBagLayout());        final JPanel p2 = new JPanel(new GridBagLayout());        final JPanel p20 = new JPanel(new GridBagLayout());        final JPanel p3 = new JPanel(new GridBagLayout());        final JPanel p4 = new JPanel(new GridBagLayout());        // first picture panels        final JPanel p5 = new JPanel(new GridBagLayout());        final JPanel p50 = new JPanel(new BorderLayout());        final JPanel p6 = new JPanel(new BorderLayout());        // first picture panels --30 seconds        final JPanel p1stblur01 = new JPanel(new GridBagLayout());        final JPanel p1stblur02 = new JPanel(new BorderLayout());        final JPanel p1stblur03 = new JPanel(new BorderLayout());        // first picture panels --60 seconds        final JPanel p2ndblur01 = new JPanel(new GridBagLayout());        final JPanel p2ndblur02 = new JPanel(new BorderLayout());        final JPanel p2ndblur03 = new JPanel(new BorderLayout());        // second picture panels        final JPanel p7 = new JPanel(new BorderLayout());        final JPanel p70 = new JPanel(new BorderLayout());        final JPanel p501 = new JPanel(new GridBagLayout());        // second picture panels --30 seconds        final JPanel p3rdblur01 = new JPanel(new GridBagLayout());        final JPanel p3rdblur02 = new JPanel(new BorderLayout());        final JPanel p3rdblur03 = new JPanel(new BorderLayout());        // second picture panels --60 seconds        final JPanel p4thblur01 = new JPanel(new GridBagLayout());        final JPanel p4thblur02 = new JPanel(new BorderLayout());        final JPanel p4thblur03 = new JPanel(new BorderLayout());        // third picture panels        final JPanel p8 = new JPanel(new BorderLayout());        final JPanel p80 = new JPanel(new BorderLayout());        final JPanel p502 = new JPanel(new GridBagLayout());        // fourth picture panels        final JPanel p9 = new JPanel(new BorderLayout());        final JPanel p90 = new JPanel(new BorderLayout());        final JPanel p503 = new JPanel(new GridBagLayout());        // last picture panels        final JPanel p10 = new JPanel(new BorderLayout());        final JPanel p100 = new JPanel(new BorderLayout());        final JPanel p504 = new JPanel(new GridBagLayout());                  //p1 components (Main frame)        ImageIcon titlepicture = new ImageIcon(C:\\java pics\\jigsaw.png);        JLabel title = new JLabel(titlepicture);        JButton start = new JButton(   Start  );        JButton about = new JButton(  About );        JButton credits = new JButton(Credits);        //p2,p20 components (start frame)        ImageIcon loading = new ImageIcon(C:\\java pics\\loading.gif);        JLabel startLoading = new JLabel(loading);        JButton continue1 = new JButton(Continue);        //p3 components (about frame)        JLabel aboutmsg = new JLabel(About the Game:);        JLabel aboutlabel = new JLabel(<html>1. This game is just for fun.                + <br>2. You need to solve the puzzle to continue.                + <br>3. Each level has a corresponding difficulty.                + <br>4. Enjoy the game.</html>);        JButton back1f1 = new JButton(Back);            //p4 components(credits frame)        JLabel creditmsg1 = new JLabel(<html>Victor Emmanuel Estomo            + <br>Ferdinand Gelle            + <br>Marthie Lim</html>);        JLabel creditmsg2 = new JLabel(Created by:);        JButton back2f1 = new JButton(Back);        //---first picture---//        f.revalidate();        f.repaint();   }}I've updated my code, and I hope I'm doing this right.  This time I removed all the components in the mainframe.  This code is 100% running but 40% done. Give me some suggestion to improve my game.package projects;import java.awt.BorderLayout;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.Timer;public class GuessingGame extends JFrame implements ActionListener {/** *  */private static final long serialVersionUID = 1L;private static JPanel startpanel;private static JPanel aboutpanel;private static JPanel creditpanel;private static JPanel loadingEffect;private static JPanel LST;private static JPanel LSTfinal;private static JPanel Next0Prev;private JPanel loadingEffectBtn;private JPanel firstPicblur;private JPanel firstPicblurA;private JPanel firstPiclabelA;private ImageIcon guess,loading;private JLabel    title,aboutmsg,aboutlabel,creditmsg,creditlabel,startLoading,level01,score01,time01,clueNo1;private JButton start,about,credits,back2f1A,back2f1B,continueButton,Next,Prev;private JTextField TypeHere;static int gTime = 1000;static int gScore = 1000000;public GuessingGame(){//Title page    startpanel = new JPanel(new GridBagLayout());   guess = new ImageIcon(C:\\java pics\\jigsaw.png);title = new JLabel(guess);start = new JButton(   Start  );about = new JButton(  About  );credits = new JButton( Credits);GridBagConstraints c = new GridBagConstraints();c.insets = new Insets(10, 10, 10, 10);c.gridx = 200;c.gridy = 150;startpanel.add(title,c);c.gridx = 200;c.gridy = 250;startpanel.add(start,c);c.gridx = 200;c.gridy = 300;startpanel.add(about,c);c.gridx = 200;c.gridy = 350;startpanel.add(credits,c);//About pageaboutpanel = new JPanel(new GridBagLayout());aboutmsg = new JLabel(About the Game);aboutlabel = new JLabel(This game is created for educational purpose);back2f1A = new JButton(Back);c.gridx = 100;c.gridy = 5;aboutpanel.add(aboutmsg,c);c.gridx = 100;c.gridy = 100;aboutpanel.add(aboutlabel,c);c.gridx = 100;c.gridy = 150;aboutpanel.add(back2f1A,c);//Credit pagecreditpanel = new JPanel(new GridBagLayout());creditmsg = new JLabel(Created by);creditlabel = new JLabel(estomo, Lim ,gelle);back2f1B = new JButton(Back);c.gridx = 100;c.gridy = 5;creditpanel.add(creditmsg,c);c.gridx = 100;c.gridy = 150;creditpanel.add(creditlabel,c);c.gridx = 100;c.gridy = 200;creditpanel.add(back2f1B,c);//loading effectloadingEffect = new JPanel(new GridBagLayout());loadingEffectBtn = new JPanel(new GridBagLayout());loading = new ImageIcon(C:\\java pics\\loading.gif);startLoading = new JLabel(loading);continueButton = new JButton(Continue);c.gridx = 200;c.gridy = 150;loadingEffect.add(startLoading,c);c.gridx = 200;c.gridy = 250;loadingEffectBtn.add(continueButton,c);//first pictureLST = new JPanel();level01 = new JLabel(Level: 1);score01 = new JLabel(Score :__);time01 = new JLabel(Time :__);LST.add(level01);LST.add(score01);LST.add(time01);//Next0Prev Next0Prev = new JPanel(new BorderLayout());Next = new JButton(NEXT);Prev = new JButton(PREV);TypeHere = new JTextField(Guess Who);clueNo1 = new JLabel(Picture Number 01);Next0Prev.add(Next,BorderLayout.EAST);Next0Prev.add(Prev,BorderLayout.WEST);Next0Prev.add(TypeHere,BorderLayout.CENTER);Next0Prev.add(clueNo1,BorderLayout.SOUTH);//adding listener to all buttonsstart.addActionListener(this);continueButton.addActionListener(this);credits.addActionListener(this);back2f1A.addActionListener(this);about.addActionListener(this);back2f1B.addActionListener(this);}public static void main(String[] args){    GuessingGame gg = new GuessingGame();    gg.setTitle(Guesing Game);    gg.setVisible(true);    gg.setSize(500,500);    gg.setLocationRelativeTo(null);    gg.add(startpanel);}@Overridepublic void actionPerformed(ActionEvent e) {    // TODO Auto-generated method stub    if (e.getSource() == about) {        add(aboutpanel);        aboutpanel.setVisible(true);        aboutpanel.repaint();        aboutpanel.revalidate();        startpanel.setVisible(false);    }    else if (e.getSource() == back2f1A){        add(startpanel);        startpanel.setVisible(true);        startpanel.repaint();        startpanel.revalidate();        aboutpanel.setVisible(false);    }    else if (e.getSource() == back2f1B){        add(startpanel);        startpanel.setVisible(true);        startpanel.repaint();        startpanel.revalidate();        creditpanel.setVisible(false);    }    else if(e.getSource() == credits){        add(creditpanel);        creditpanel.setVisible(true);        creditpanel.repaint();        creditpanel.revalidate();        startpanel.setVisible(false);    }    else if(e.getSource() == start){        add(loadingEffect);        loadingEffect.setVisible(true);        loadingEffect.repaint();        loadingEffect.revalidate();        startpanel.setVisible(false);        Timer t = new Timer(1000, new ActionListener() {            public void actionPerformed(ActionEvent e) {                add(loadingEffectBtn);                loadingEffectBtn.setVisible(true);                loadingEffectBtn.repaint();                loadingEffect.invalidate();                loadingEffect.setVisible(false);                }                });                t.start();                t.setRepeats(false);     }    else if(e.getSource() == continueButton){        add(Next0Prev);        Next0Prev.setVisible(true);        Next0Prev.repaint();        Next0Prev.revalidate();        loadingEffectBtn.setVisible(false);        Timer t = new Timer(1000, new ActionListener() {            public void actionPerformed(ActionEvent e) {                add(LST,BorderLayout.NORTH);                time01.setText(time:  + gTime);                gTime--;         Timer t = new Timer(100, new ActionListener() {             public void actionPerformed(ActionEvent e) {                score01.setText(score:  + gScore);                gScore--;                     }                });                t.start();                t.setRepeats(true);                }                });                t.start();                t.setRepeats(true);        }    }}
Improving my Guess Who? game
java;swing
I'm not going to go through all of this code since it's like 800 lines and would take me all day, but I can give you some good general tips just by glancing at it.Choose better variable names.  Things like next07, next08, [...], and p80, p90, [...] don't tell anyone anything about what those variables are actually supposed to represent and will end up confusing even yourself in the long run.Split out your logic into different methods.  All of your processing is in the main() method.  Why?  Having an 800 line method is almost always the sign of a bad design.  I grant you that this rule gets stretched a little bit when it comes to Swing, but not like this.Any logic that you constantly repeat should be encapsulated into its own method so that it's reusable.  For example, you have all these lines where you keep making new ActionListener() implementations.  This could easily be turned into a method like....public ActionListener createActionListener(JPanel add, JPanel remove) {    return new ActionListener() {        @Override        public void actionPerformed(ActionEvent e) {            f.add(add);            f.remove(remove);            f.setVisible(true);            f.revalidate();            f.repaint();        }    };}This could then be called again and again, which would shorten your source code dramatically.next05.addActionListener(createActionListener(p90, p80));next06.addActionListener(createActionListener(p70, p80));//... and so onYou could also simply have one class which implements ActionListener rather than having this factory-like method.You've got about a thousand Timers being created and within nested ActionListeners.  I have no idea what you're trying to accomplish with all that, but I guarantee you that's not how you need to be going about it.  Creating a new Timer within another Timer's ActionListener doesn't make sense.  If you can expand upon what you're trying to do with all that logic, I might be able to provide a better alternative or design choice.Why are you using HTML tags in your JLabel when you only needed the line breaks?  You can just use \n.You don't have to have a separate JLabel for every question in your game.  You can just use JLabel.setText(String) to change one label in your frame... That goes for all of your Swing elements.  Most of them are reusable.  You have exponentially more than you need.I'm sure there's a lot more, but that's what I noticed off the bat.  If you shorten the code a bit given the above comments, I'll take a deeper look at it all.Good luck!