简体   繁体   English

何时使用JFrame,JLabel和JPanel

[英]When to use JFrame, JLabel, and JPanel

I have been, for a long time, wondering when to use JFrame and JPanel. 很长一段时间以来,我一直想知道何时使用JFrame和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); 我通过教科书,面对面和通过互联网搜索告诉我一些原因,但当然,我看了Java文档,弄清楚JPanel有paintComponent(Graphics); method, which allows you to specify what to do to the object itself, unlike JFrame. 方法,它允许您指定对象本身做什么,与JFrame不同。 Also, JPanels are nestable, allowing for more complex graphics than you could achieve with just one container. 此外,JPanels是可嵌套的,允许比仅使用一个容器更复杂的图形。 I also stumbled upon the fact that a JLabel is also a container. 我还偶然发现JLabel也是一个容器。 (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? (它甚至有一个怪异的布局!)现在我的问题是:我应该何时使用JLabel,何时应该使用JPanel?

//I know that you can put JLabels inside a JPanel, and by accident, that a JPanel is more expensive than a JLabel. //我知道你可以将JLabel放在JPanel中,偶然的是,JPanel比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). 我正考虑使用JLabel作为货币金额(美元钞票和硬币),并将JPanel用作交易的间接费用视图。 I was also thinking about invoking repaint() (which, to my understanding, can simulate animation) on the "money labels" themselves. 我也在考虑在“货币标签”本身上调用repaint()(根据我的理解,可以模拟动画)。 This is a double-question, but would you see this as the least expensive way to go about it? 这是一个双重问题,但你会认为这是最便宜的方式吗?

For the most part, JPanel is a container that allows for better organization. 在大多数情况下,JPanel是一个允许更好组织的容器。 When you need to organize your components of your gui, you should use a JPanel and define a layout for the JPanel. 当您需要组织gui的组件时,应该使用JPanel并为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. 您还可以将JPanels嵌套在一起等等.JLabel无法为JLabel中显示的其他组件定义布局。

Here's an example of a app that says "Hello World!!!" 这是一个应用程序的例子,上面写着“Hello World !!!” without the quotes: 没有引号:

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”,没有引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM