java gui awt swing,使用Java混合AWT和Swing的GUI编程

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值