秋千的 BoxModel 如何工作?

发布于 2024-09-01 01:00:42 字数 292 浏览 4 评论 0原文

假设我想创建一个简单的计算器。它由 3 个字段组成。用于显示结果的文本字段、用于选择系统的带有复选框的字段以及带有数字的字段。

我应该为每个元素使用什么样的组件? 如何在窗口中定位元素? 如何在组件内定位元素(即复选框)?

这就是我想要实现的目标。

http://img28.imageshack.us/img28/7691/lab8c.jpg

Let's say I would like to create a simple calculator. It consists of 3 fields. Text field to show result, field with checkboxes to select system and field with numbers.

What kind of component should I use for each element ?
How can I position elements in my window ?
How can I position elements inside component (ie checkboxes) ?

This is what I'm trying to achieve.

http://img28.imageshack.us/img28/7691/lab8c.jpg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深居我梦 2024-09-08 01:00:42

我将使用

  • JTextField 作为数字窗口
  • JRadioButton 作为单选按钮,使用
  • JButton 作为按钮。

组件的布局应该服从所谓的布局管理器。 (查看使用布局管理器。在这种情况下, GridLayout 和/或 GridBagLayout 就可以了。

此代码应该可以帮助您入门:

import java.awt.*;

import javax.swing.*;


public class FrameTest {

    public static void main(String[] args) {
        final JFrame f = new JFrame("Frame Test");

        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        panel.add(new JTextField(), gbc);


        JPanel numSysPanel = new JPanel(new GridLayout(1, 3));
        numSysPanel.setBorder(BorderFactory.createTitledBorder("Number System"));
        numSysPanel.add(new JRadioButton("oct"));
        numSysPanel.add(new JRadioButton("dec"));
        numSysPanel.add(new JRadioButton("hex"));
        panel.add(numSysPanel, gbc);

        JPanel buttons = new JPanel(new GridLayout(4, 4, 2, 2));
        for (int i = 0; i < 16; i++)
            buttons.add(new JButton("" + i));
        panel.add(buttons, gbc);

        f.setContentPane(panel);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

在此处输入图像描述

I would use

  • JTextField for the number-window
  • JRadioButton for the radio buttons, and
  • JButton for the buttons.

The layout of the components should be deferred to a so called layout-manager. (Have a look at Using Layout Managers. In this case a GridLayout and/or a GridBagLayout would do fine.

This code should get you started:

import java.awt.*;

import javax.swing.*;


public class FrameTest {

    public static void main(String[] args) {
        final JFrame f = new JFrame("Frame Test");

        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;
        panel.add(new JTextField(), gbc);


        JPanel numSysPanel = new JPanel(new GridLayout(1, 3));
        numSysPanel.setBorder(BorderFactory.createTitledBorder("Number System"));
        numSysPanel.add(new JRadioButton("oct"));
        numSysPanel.add(new JRadioButton("dec"));
        numSysPanel.add(new JRadioButton("hex"));
        panel.add(numSysPanel, gbc);

        JPanel buttons = new JPanel(new GridLayout(4, 4, 2, 2));
        for (int i = 0; i < 16; i++)
            buttons.add(new JButton("" + i));
        panel.add(buttons, gbc);

        f.setContentPane(panel);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

enter image description here

酷到爆炸 2024-09-08 01:00:42

首先,您首先确定 您应该使用 LayoutManager 来排列您的三个字段。 GridBagLayout肯定会做你想做的事,但很难编程。您可以尝试使用更简单的 BorderLayout,这将使您的应用程序在调整大小时看起来很奇怪。您可以使用 GroupLayout< /a> 也是。 BoxLayoutGridLayoutFlowLayout 不是您想要使用的。现在您有很多选择来布置顶级元素。

使用 JTextField 的结果。使用 JCheckBox 对于复选框,您将其放入带有蚀刻边框的 JPanel 内部(通过 JPanel.setBorder(BorderFactory.createEtchedBorder()))和 FlowLayout代码>.不要忘记将复选框放在 复选框组。最后但并非最不重要的一点是,使用 JPanel 对呃按钮的 JButton 进行分组。使用 GridLayout(5行,4列)来排列这些按钮。

First off, you start with determining, which LayoutManager you should use, to arrange your three fields. GridBagLayout will definitely do what you want, but is quite hard to program. You could try to get away with the simpler BorderLayout, which will make your application look odd, while resizing. You can use GroupLayout too. BoxLayout, GridLayout and FlowLayout are not what you want to use. Now you have a lot of options, to lay out you top elements.

Use a JTextField for the result. Use JCheckBox for the checkboxes, which you put insider a JPanel with an etched border (via JPanel.setBorder(BorderFactory.createEtchedBorder())) and a FlowLayout. Don't forget to put the checkboxes in a CheckboxGroup. Last but not least use a JPanel to group the JButtons for the, uhh, buttons. Use a GridLayout (5 rows, 4 columns) to arrange these buttons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文