My First GUI

 This week I decided to take a break from working on my application to focus on developing my coding skills. I took up on creating a GUI or graphical user interface. I believe this to be one of the more important skills that I can learn as a software engineer. I built my code within EclipseIDE. 


As I started building my code, I used a JFrame to build the initial size of the window I wanted to show. I also added 

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("GUI");
frame.pack();
frame.setVisible(true);

and this allowed me to set the window to fully close when exited, name the window, made the window match a certain size, and make the window visible. After that, I wanted to add something simple to give the window a purpose so I added the code to create a button. The button didn't do anything at first so I thought what would be something simple but fun to do. I made the window a bit larger to accommodate more text and a button. After that, I change the title of the window to "Click Tracker". I then made the button more tempting to click by including the text "Click here" on the button with the text "Number of clicks: 0" under it. At first I was stuck because I had to hardcode the 0 into the text and had to do some research to figure out how to use an ActionListener. It was my first time using ActionListener so figuring out how to implement it took a bit of time but I was able to get it to work.

After I added the ActionListener I set the label object to change from the hardcoded 0 to increase by 1 everytime the button was pressed by using the code
label.setText("Number of clicks: " + count);
By doing this, I'm able to change the number of clicks to start at 0 or whatever I need when I originalyl initialize "label" and I can change the number to go up by however much I want. I was able to make my first small GUI and I'm very happy with the way it turned out. Although it is very basic and limited, I was able to learn the framework of how to build GUIs and have a little fun by messing with the window sizes, window name, name of the button, and changing what the button does. I hope to learn more about GUIs in the future to be able to make more functional programs.

Comments

  1. Powerful stuff here, mastering these skill will certainly be fantastic for your portfolio. I like that you keep yourself thoroughly engaged while juggling different topics.

    ReplyDelete

Post a Comment

Popular posts from this blog

Java Refresher

Rising of OLED

Random Class in Java