使用Java混合AWT和Swing的GUI编程 [英] mixing awt and swing in GUI programming using Java

查看:393
本文介绍了使用Java混合AWT和Swing的GUI编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了,这样混合AWT和Swing是不是真的在Java GUI编程一个不错的办法。我不能,虽然,发现在使用摇摆不使用一些AWT组件的例子。例如,使用摆动甚至当,最让我遇到了会使用AWT布局和事件处理的例子。

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.

话虽这么说,这是什么意思不是使用Java混合,在GUI编程Swing和AWT?这是否仅仅意味着不借用Swing和AWT图形部件,如按钮,帆布同时或只是完全独自使用Swing或AWT?或者,它是正常使用的图形单元(如按钮,面板)从AWT摆动和事件处理和布局?

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;

我可能是错的,但使用完全我没见过例如摆动,且不会AWT或以错误的方式在看这个,因为我是新来的Java GUI。

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.

推荐答案

您将使用AWT布局,动作监听器等使用时,摆在 - 这是确定和设计,摇摆框架建立在AWT在这方面。这是相对于JavaFX的是在它自己右边的完整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.

你应该尽可能避免在混合实际的组件的在GUI的地方 - 用一个JButton例如帧(而不是一个JFrame)里面,或者用一个按钮和一个JButton侧一面。

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.

这篇关于使用Java混合AWT和Swing的GUI编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆