java 何时使用 JFrame、JLabel 和 JPanel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15776910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:49:23  来源:igfitidea点击:

When to use JFrame, JLabel, and JPanel

javaswingjframejpaneljlabel

提问by Mike Warren

I have been, for a long time, wondering when to use JFrame and JPanel. I was told by textbook, in-person, and via internet search some reasons, but of course, it took me looking at the Java documentation to figure out that a JPanel has the paintComponent(Graphics); method, which allows you to specify what to do to the object itself, unlike JFrame. Also, JPanels are nestable, allowing for more complex graphics than you could achieve with just one container. I also stumbled upon the fact that a JLabel is also a container. (It even has a freaking layout!) Now my question is: when should I be using a JLabel and when should I be using a JPanel?

很长一段时间以来,我一直想知道何时使用 JFrame 和 JPanel。教科书、面对面和通过互联网搜索告诉我一些原因,但当然,我看了 Java 文档才发现 JPanel 具有paintComponent(Graphics); 方法,与 JFrame 不同,它允许您指定对对象本身执行的操作。此外,JPanels 是可嵌套的,允许使用比仅使用一个容器所能实现的更复杂的图形。我还偶然发现 JLabel 也是一个容器。(它甚至有一个奇怪的布局!)现在我的问题是:我什么时候应该使用 JLabel,什么时候应该使用 JPanel?

//I know that you can put JLabels inside a JPanel, and by accident, that a JPanel is more expensive than a JLabel. I, in the long run, plan on making a cashier game that involves some lightweight container for the "money" that moves to the customer's hand upon clicking. I was thinking about using JLabels for the monetary amounts (dollar bills and coins), and the JPanel to serve as the overhead view of the transaction(s). I was also thinking about invoking repaint() (which, to my understanding, can simulate animation) on the "money labels" themselves. This is a double-question, but would you see this as the least expensive way to go about it?

//我知道你可以把JLabels放在JPanel里面,偶然发现JPanel比JLabel贵。从长远来看,我计划制作一个收银员游戏,其中包含一些轻量级的“钱”容器,点击后会移动到客户的手上。我正在考虑将 JLabels 用于货币金额(美元钞票和硬币),并将 JPanel 用作交易的俯视图。我也在考虑在“货币标签”本身上调用 repaint() (据我所知,它可以模拟动画)。这是一个双重问题,但您认为这是最便宜的方法吗?

回答by user2333258

For the most part, JPanel is a container that allows for better organization. When you need to organize your components of your gui, you should use a JPanel and define a layout for the JPanel. You can also nest JPanels within each other and etc. JLabels don't have the capability of defining layouts for further components to be displayed inside the JLabel.

在大多数情况下,JPanel 是一个允许更好组织的容器。当您需要组织 gui 的组件时,您应该使用 JPanel 并为 JPanel 定义布局。您还可以将 JPanel 嵌套在彼此之间等等。JLabel 无法定义要在 JLabel 内显示的进一步组件的布局。

回答by user2921779

Here's an example of a app that says "Hello World!!!" without the quotes:

这是一个应用程序示例,上面写着“Hello World!!!” 没有引号:

import javax.swing.JFrame;        
import javax.swing.JPanel;        
import javax.swing.JLabel;        
public class JFrame {        
    public static void main (strings args[]) {        
        JFrame frame = new JFrame();        
        String title = "Hello App";        
        frame.setTitle(title);        
        frame.setSize(300, 200);        
        frame.setDefaultCloseOperation        
            (JFrame.EXIT_ON_CLOSE);        
        frame.setVisible(true);        
        JPanel panle = new JPanel();        
        frame.add(panle);        
        JLabel lable = new JLabel("Hello World!!!");        
        panle.add(lable);        
    }
}

The file name is "JFrame.java" without the quotes.

文件名为“JFrame.java”,不带引号。