I read on SO that mixing awt and swing is not really a good approach for GUI programming in Java. I cannot though, find any examples which does not use some awt components while using swing. For example, even when using swing, most of the example I have encountered will use awt for layout and event handling.
That being said, what does it mean not to mix swing and awt in GUI programming using Java? Does it just mean not to borrow graphical widgets such as button, canvas from swing and awt at the same time or just completely use swing or awt alone? Or it is okay to use graphical units ( like buttons, panels) from swing and event handling and layout from awt?
Most of the time the imports will look at least like this:
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
I might be wrong but I have not seen example using completely swing without awt or looking at this in a wrong way since I am new to Java GUI.
解决方案
You'll use the AWT layouts, action listeners and so on when using Swing - this is ok and by design, the Swing framework builds on AWT in this respect. This is in contrast to JavaFX which is a complete GUI framework in its own right, and contains its own layouts, event handlers and so on.
What you should avoid if possible is mixing the actual components you place in the GUI - using a JButton inside a Frame (rather than a JFrame) for example, or using a Button and JButton side by side.
There are some cases where it's necessary - using VLCJ in a Swing application for instance requires a heavyweight (AWT) canvas if you want to use a normal embedded video player. However, without a good use case like the above, it's something that should be avoided.