GUI in Java using Swing

I really need to be guided by how to create a GUI in Java. I am a college student, and this is one of those things that they never focus on. I currently have the rest of my program configured and working, and now I'm trying to make a GUI. To do this, I create a new GUI class. (I have the impression that this is best suited for this). I kind of understand how to make a basic setup, but I don’t understand how to interact with the graphical interface later. I want to make a window at startup, and then it displays two snapshots next to the label for each bottom. I want the images to be clickable, and when I click on them, two new images are loaded (labels are replaced at the bottom). I have not done anything like this, and I have also come across many ways to add an image, and I am wondering if there is any best practice.

I can provide the code if necessary, but I did not think that it would be necessary in order to ask a question.

Thanks in advance for your help.

+4
source share
3 answers

Some suggestions:

  • Put the images in ImageIcons. Think about having an ArrayList from ImageIcons.
  • Map your ImageIcons to JLabel. You can change the icon by calling setIcon (...).
  • Display your text in the same JLabel (or, if you want, the other JLabel that is under the JLabel image, both are under JPanel BorderLayout). Modify the JLabel text using the setText (...) method.
  • Add a MouseListener to the holding image JLabel and change the JLabel icon in the mouseener method for the listener. You can get a link to the label by clicking on the getSource () method using the mousePressed MouseEvent method.
  • The tutorials mentioned in the asgs comments will help you with this.

Change 1:

  • Even better - go with the Puce recommendation in the comments below my post!
+7
source

Two personal tips from my 5 years experience at Swing.

Learn how to write graphical interfaces using the LayoutManager instead of the graphical interface. (MigLayout is the best choice)

Learn how to write a tableModel instead of using DefaultTableModel.

+3
source

There are also some good tutorials

0
source

All Articles