【问题标题】:Multiple Canvas' next to each other多个 Canvas 并排
【发布时间】:2014-04-08 12:36:01
【问题描述】:

我正在为最多 10 位的七段显示计算器制作模拟器。 我有一个逻辑,我有一个大小为 10 的对象数组。这些对象接受一个数字并“打开”在画布上显示正确数字所需的段。一世 我试图让 10 个独立的画布彼此相邻,并有一个绘制功能 根据“打开”的段,每个数字中的正确数字。反正我能猜到 那部分。我似乎无法弄清楚的部分是如何在一个 JFrame 上让 10 个单独的画布彼此相邻。我尝试使用 10 个面板,但格式不起作用,我遇到了问题。关于如何制作这个有什么建议吗?

【问题讨论】:

  • GridLayout, GridBagLayout 甚至可能是FlowLayout
  • 但是您建议仍然使用面板?我试图让它看起来像这样。 __ __ __ __ __ __ __ __ __ | | | | | | | | | | | | | | | | | | | | |__| |__| |__| |__| |__| |__| |__| |__| |__| |__|
  • 结果很糟糕,但我希望你最后能看到我想要做什么
  • 你需要告诉我们什么是“画布”
  • 我想我真正的问题是我不知道。我指的是类似于 javascripts 画布对象但在 java 中的类似 Canvas 的对象。有这种事吗?

标签: java swing canvas jframe panel


【解决方案1】:

这是一个非常基本的 7 段显示设置示例,仅使用自定义 JPanels...

显示中的每个元素只是一个带有一些附加“属性”的JPanel,然后布置在另一个用作主显示的JPanel 上。您可以通过使用boolean 数组来影响打开和关闭哪些段,其中位置;

  • 0 是顶部
  • 1 & 2 是左右,上半部
  • 3 是中间
  • 4*5为左右,下半部
  • 6 是底部

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class TestSegmentDisplay {

    public static void main(String[] args) {
        new TestSegmentDisplay();
    }

    private SegmentDisplay segmentDisplay;
    private int count = 0;

    public TestSegmentDisplay() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                segmentDisplay = new SegmentDisplay();

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(segmentDisplay);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                final Timer timer = new Timer(1000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        setSegmentValue(segmentDisplay, count);
                        count++;
                        if (count > 9) {
                            count = 0;
                        }
                    }
                });
                timer.start();

            }
        });
    }

    public static void setSegmentValue(SegmentDisplay display, int value) {

        boolean[] states = new boolean[]{
            false,
            false, false,
            false,
            false, false,
            false
        };
        switch (value) {
            case 0:
                states = new boolean[]{
                    true,
                    true, true,
                    false,
                    true, true,
                    true};
                break;
            case 1:
                states = new boolean[]{
                    false,
                    false, true,
                    false,
                    false, true,
                    false};
                break;
            case 2:
                states = new boolean[]{
                    true,
                    false, true,
                    true,
                    true, false,
                    true};
                break;
            case 3:
                states = new boolean[]{
                    true,
                    false, true,
                    true,
                    false, true,
                    true};
                break;
            case 4:
                states = new boolean[]{
                    false,
                    true, true,
                    true,
                    false, true,
                    false};
                break;
            case 5:
                states = new boolean[]{
                    true,
                    true, false,
                    true,
                    false, true,
                    true};
                break;
            case 6:
                states = new boolean[]{
                    true,
                    true, false,
                    true,
                    true, true,
                    true};
                break;
            case 7:
                states = new boolean[]{
                    true,
                    false, true,
                    false,
                    false, true,
                    false};
                break;
            case 8:
                states = new boolean[]{
                    true,
                    true, true,
                    true,
                    true, true,
                    true};
                break;
            case 9:
                states = new boolean[]{
                    true,
                    true, true,
                    true,
                    false, true,
                    true};
                break;
        }

        display.setSegments(states);

    }

    public static class SegmentDisplay extends JPanel {

        private Segement[] segemnts;

        public SegmentDisplay() {
            segemnts = new Segement[7];
            segemnts[0] = new Segement(Segement.Direction.HORIZONTAL);
            segemnts[1] = new Segement(Segement.Direction.VERTICAL);
            segemnts[2] = new Segement(Segement.Direction.VERTICAL);
            segemnts[3] = new Segement(Segement.Direction.HORIZONTAL);
            segemnts[4] = new Segement(Segement.Direction.VERTICAL);
            segemnts[5] = new Segement(Segement.Direction.VERTICAL);
            segemnts[6] = new Segement(Segement.Direction.HORIZONTAL);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 1;
            gbc.gridy = 0;
            add(segemnts[0], gbc);

            gbc.gridx = 0;
            gbc.gridy = 1;
            add(segemnts[1], gbc);
            gbc.gridx = 2;
            add(segemnts[2], gbc);

            gbc.gridx = 1;
            gbc.gridy++;
            add(segemnts[3], gbc);

            gbc.gridx = 0;
            gbc.gridy++;
            add(segemnts[4], gbc);
            gbc.gridx = 2;
            add(segemnts[5], gbc);

            gbc.gridx = 1;
            gbc.gridy++;
            add(segemnts[6], gbc);
        }

        public void setSegments(boolean[] states) {

            if (states != null && states.length > 0 && states.length == segemnts.length) {

                for (int index = 0; index < segemnts.length; index++) {
                    segemnts[index].setOn(states[index]);
                }

            }

        }

    }

    public static class Segement extends JPanel {

        public enum Direction {

            VERTICAL,
            HORIZONTAL;
        }

        public static final int SMALL_SIZE = 10;
        public static final int LARGE_SIZE = SMALL_SIZE * 4;

        protected static final Color OUT_LINE = new Color(128, 0, 0);
        protected static final Color ON_COLOR = Color.RED;
        protected static final Color OFF_COLOR = new Color(64, 0, 0);

        private final Direction direction;
        private boolean on;

        public Segement(Direction direction) {
            this.direction = direction;
            setBorder(new LineBorder(OUT_LINE));
            setOn(true);
            setOn(false);
        }

        public void setOn(boolean value) {
            if (on != value) {
                on = value;
                if (on) {
                    setBackground(ON_COLOR);
                } else {
                    setBackground(OFF_COLOR);
                }
            }
        }

        public boolean isOn() {
            return on;
        }

        public Direction getDirection() {
            return direction;
        }

        @Override
        public Dimension getPreferredSize() {
            return getDirection() == Direction.VERTICAL ? new Dimension(SMALL_SIZE, LARGE_SIZE) : new Dimension(LARGE_SIZE, SMALL_SIZE);
        }

    }

}

可能有一个非常聪明的方法来设置switch 语句,但我现在真的不能被打扰......

然后我要做的是将每个 SegmentDisplay 添加到 JPanel 使用类似 GridBagLayoutFlowLayout...

【讨论】:

    猜你喜欢
    • 2012-05-26
    • 2013-11-05
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    相关资源
    最近更新 更多