java swing计算器布局

时间:2014-07-23 12:09:15

标签: java swing layout-manager grid-layout flowlayout

我需要做一个作业并创建一个计算器。它是一门初学Java课程,所以请记住,我不是专家。它不应该看起来很壮观,所以实现以下目标的最简单方法就是很棒。

它的内部运作很好,但吸引它真的很头疼。

到目前为止,我们只接触过flowlayout ......在这种情况下,它根本不是我想要的。让我先告诉你我正在寻找的布局:

  • 在顶部,一个标题在计算器上蔓延,可能是一个 背景填充。
  • 然后在下面,2个按钮彼此相邻。
  • 在此之下,两个标签彼此相邻。
  • 然后两个文本字段彼此相邻。
  • 在此之下,两个标签彼此相邻。
  • 然后两个文本字段彼此相邻。

我尝试在这里绘图,但它没有正确格式化。如果我可以把它放在HTML中它基本上是一个简单的表,有6行和2列。但是顶行必须跨越两列。

Flowlayout只是将所有内容从左到右放在一起。

之后我尝试使用GridLayout,但最重要的标题是这里的问题,因为它没有跨越两个列。

到目前为止,这是我的代码:

public class TripCalculator extends JFrame implements ActionListener {

    public static final int WIDTH = 400;
    public static final int HEIGHT = 300;
    public static final int NUMBER_OF_CHAR = 4;
    public JTextField stopTime, distance, tripTime, speed;

    public TripCalculator() {
        setSize(WIDTH, HEIGHT);
        WindowDestroyer listener = new WindowDestroyer();
        addWindowListener(listener);

        Container contentPane = getContentPane();

        contentPane.setLayout(new FlowLayout());

        JLabel heading = new JLabel("HEADING");
        contentPane.add(heading);

        contentPane.setLayout(new FlowLayout());

        JButton addStop = new JButton("BUTTON1");
        addStop.addActionListener(this);

        JButton addLeg = new JButton("BUTTON2");
        addLeg.addActionListener(this);

        contentPane.add(addStop);
        contentPane.add(addLeg);

        JLabel subHead1 = new JLabel("LABEL1");
        contentPane.add(subHead1);

        JLabel subHead2 = new JLabel("LABEL2");
        contentPane.add(subHead2);

        stopTime = new JTextField(NUMBER_OF_CHAR);
        contentPane.add(stopTime);

        distance = new JTextField(NUMBER_OF_CHAR);
        contentPane.add(distance);

        JLabel subHead3 = new JLabel("LABEL3");
        contentPane.add(subHead3);

        JLabel subHead4 = new JLabel("LABEL4");
        contentPane.add(subHead4);

        tripTime = new JTextField(NUMBER_OF_CHAR);
        contentPane.add(tripTime);

        speed = new JTextField(NUMBER_OF_CHAR);
        contentPane.add(speed);
    }
}

如果有人能向我展示正确的方向,我将不胜感激。

2 个答案:

答案 0 :(得分:2)

Flowlayout或Gridlayout本身对你没有帮助。您可以使用Gridbaglayout,也可以使用FlowLaout + Gridlayout等布局组合。

如果您正在绘制计算器,我假设您正在绘制这样的东西:

enter image description here

顶部有标题信息,中间有计算器键盘,底部有其他按钮:

这可以通过垂直框布局实现,顶部和底部有铺设布局,中间是所有数字键的网格布局。

但是......如果你没有显示出你想要的东西的图表很难说。

答案 1 :(得分:1)

以下是使用多个Layout Managers的示例,因为您可以看到可以使用多个here,但您应该使用多个JPanel来实现您想要的效果。

另外一个建议是:不要从JFrame扩展,而是像我在本例中那样创建一个JFrame对象和{{3}}这就是为什么你不应该去做。

import java.awt.*;
import javax.swing.*;
public class LayoutManagersExample {
    public static void main(String args[]) {
        new LayoutManagersExample();
    }

    public LayoutManagersExample() {
        JFrame frame = new JFrame("Layout Managers Example");
        JPanel topPane = new JPanel();
        JPanel midPane = new JPanel();
        JPanel panesHolder = new JPanel();
        JLabel label = new JLabel("Top label");
        JTextField field = new JTextField();
        field.setColumns(5);

        topPane.setLayout(new FlowLayout());
        midPane.setLayout(new GridLayout(3, 2));

        topPane.add(label);
        topPane.add(field);

        midPane.add(new JButton("Button 1"));
        midPane.add(new JButton("Button 2"));
        midPane.add(new JButton("Hello I'm a button"));
        midPane.add(new JButton("HEY! Click me :)"));
        midPane.add(new JButton("I love you"));
        midPane.add(new JButton("This is another button"));

        panesHolder.setLayout(new BoxLayout(panesHolder, BoxLayout.Y_AXIS));
        panesHolder.add(topPane);
        panesHolder.add(midPane);

        frame.add(panesHolder);
        frame.setSize(400, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
    }
}

这就是它的样子:

enter image description here

相关问题