如何在不使用 Swing 类/方法的情况下嵌套布局管理器? [英] How do I nest layout managers without using swing classes/methods?

查看:27
本文介绍了如何在不使用 Swing 类/方法的情况下嵌套布局管理器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用 Java 6 制作一个小程序,640*480 像素,底部有一个工具栏,上面有一些按钮、滚动条和标签.我能想到的最好方法是使用 BorderLayout,使 BorderLayout.SOUTH 区域成为 GridBagLayout(将包含控件),以及BorderLayout 区域的其余部分为空,作为使用控件绘制图形的地方.我在网上找不到任何不使用 Swing 的资源,而且我对 Swing 一无所知以推断他们在做什么或如何将其转换为 awt 代码.这就是我现在所在的地方.代码在 init() 中突然结束,因为这是布局管理器开始的地方.感谢您提供的任何帮助.如果您需要更多信息,请告诉我这里有什么.

I need to make an applet in Java 6, 640*480 pixels, with a toolbar on the bottom with some buttons, scrollbars, and labels. The best way I could think of was using a BorderLayout, making the BorderLayout.SOUTH region a GridBagLayout (which would contain the controls), and the rest of the BorderLayout area null, as a place for grapics to be drawn with the controls. I can't find any resources online that don't use swing, and I don't know anything about swing to deduce what they are doing or how to translate it into awt code. Here is where I am now. The code ends abruptly in init(), since that's where the layout mangers start. Thank you for any help you have. Let me know if you need more information then what is here.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Bounce extends Applet implements ActionListener, AdjustmentListener
{
    private static final long serialVersionUID = 1L;
    private Graphics page;
    //buttons
    String shapeButtonText = "Square";
    Button shape = new Button(shapeButtonText);
    Button quit = new Button("Quit");
    Button run = new Button("Run");
    Button tail = new Button("Tail");
    Button clear = new Button("Clear");

    //labels
    Label speedLabel = new Label("Speed", Label.CENTER);
    Label sizeLabel = new Label("Size", Label.CENTER);

    //scrollbars
    private final int barHeight = 20;
    private final int SLIDER_WIDTH = 10;
    private final int MAXSPEED = 110;
    private final int MINSPEED = 0;
    private final int UNIT_INC = 1;
    private final int BLOC_INC = 10;
    private final int MAX_SIZE = 110;
    private final int MIN_SIZE = 10;
    Scrollbar speedBar = new Scrollbar(Scrollbar.HORIZONTAL, MINSPEED, SLIDER_WIDTH, 0, MAXSPEED);  
    Scrollbar sizeBar = new Scrollbar(Scrollbar.HORIZONTAL, MIN_SIZE, SLIDER_WIDTH, 0, MAX_SIZE);


    //methods   
    public void init()
    {
        //set up objects
        //speed scroll bar
        speedBar.setUnitIncrement(UNIT_INC);
        speedBar.setBlockIncrement(BLOC_INC);
        speedBar.setValue(MAXSPEED/2);

        //size scrollbar
        sizeBar.setUnitIncrement(UNIT_INC);
        sizeBar.setBlockIncrement(BLOC_INC);
        sizeBar.setValue(MAX_SIZE/2);

        //draw the window
        BorderLayout window = new BorderLayout();
        GridBagLayout toolbar = new GridBagLayout();
        //?
    }

    public void start()
    {
    }

    public void run()
    {
    }

    public void actionPerformed(ActionEvent e)
    {
    }

    public void adjustmentValueChanged(AdjustmentEvent e)
    {
    }

    public void stop()
    {
    }

    public void destory()
    {
    }
}

推荐答案

让我们澄清一些事情.LayoutManagerContainer(例如 FramePanelApplet)使用code>) 来计算 Container 中组件的位置和大小.这意味着谈论嵌套 LayoutManagers"是不正确的.另一方面,您可以将 Container 嵌套在彼此内部,并为每个容器提供自己的 LayoutManager.我相信这就是你想要做的.

Let's clarify a few things. A LayoutManagers are used by a Container (such as Frame, Panel, or Applet) to calculate position and size of the components inside the Container. This means that it is incorrect to talk about "nesting LayoutManagers". On the other hand you can nest Containers inside each other and give each one its own LayoutManager. I believe this is what you want to do.

让我用一个人为的例子来说明这一点:

Let me illustrate this with a contrived example:

public class MyGUI {
    public static void main(String[] args) {
        Frame f = new Frame("Layout Example");
        Panel mainPanel = new Panel(new BorderLayout());
        f.add(mainPanel);

        Panel toolBar = new Panel(new FlowLayout());
        toolBar.add(new Button("Button 1"));
        toolBar.add(new Button("Button 2"));
        mainPanel.add(tollBar.NORTH);

        Panel statusBar = new Panel(new FlowLayout());
        statusBar.add(new Label("Status"));
        mainPanel.add(statusBar);

        f.pack();
        f.show();
    }
}

请注意,您需要为每个 LayoutManager 创建一个新的 Panel.或者更确切地说,您创建的每个 Panel 都需要一个 LayoutManager.此外,通过将 Frame 替换为 JFrame,将 Panel 替换为 JPanel,可以轻松地将这个示例从 AWT 更改为 Swing,Button 带有 JButtonLabel 带有 JLabel.

Notice that you need to create a new Panel for each LayoutManager. Or rather each Panel that you create needs a LayoutManager. Also, this example can easily be changed from AWT to Swing by replacing Frame with JFrame, Panel with JPanel, Button with JButton, and Label with JLabel.

附言上面的代码没有经过测试.不过,它应该说明这里涉及的概念.

p.s. The above code is not tested. It should illustrate the concepts involved here, though.

这篇关于如何在不使用 Swing 类/方法的情况下嵌套布局管理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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