0

我正在设计一个与 DFA 相关的程序..所以我想把 Q0 或 Q1 之类的东西放在圈子里,但我不知道怎么做..有人可以看看我的代码并告诉我怎么做吗?如果可以直接放置一个将出现在圆圈内的名称,那就太好了,因为我认为很难为 JLabel 设置位置。这是我的代码:

    import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class midterm extends JFrame
{
    JPanel mainpanel;
    JPanel gamepanel;
    JPanel controls;



    ExitButtonListener end=new ExitButtonListener();



    public midterm()
    {
        super("My DFA Design");
        setSize(700,700);
        setLocation(400,100);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);

        panel();
        this.getContentPane().add(mainpanel);
        this.pack();
        setVisible(true);   
        setSize(400,400);
    }



    public static void main(String[] args)
    {
        midterm frame=new midterm();
    }



    void panel()
    {
        mainpanel=new JPanel();
        mainpanel.setLayout(new BorderLayout());


        gamepanel=new JPanel();
        gamepanel.setBorder(BorderFactory.createTitledBorder("Deterministic Finite Automata"));
        gamepanel.setLayout(new GridLayout(2,3));


        controls = new JPanel();
        controls.setLayout(new BorderLayout());
        controls.setBorder(BorderFactory.createTitledBorder("Control"));

        JButton newGame = new JButton("Reset");
        newGame.addActionListener(new NewButtonListener());
        controls.add(newGame, BorderLayout.NORTH);


        JButton exitGame = new JButton("Exit");
        exitGame.addActionListener(end);
        controls.add(exitGame, BorderLayout.SOUTH);

        mainpanel.add(gamepanel, BorderLayout.CENTER);
        mainpanel.add(controls, BorderLayout.EAST);

        mainpanel.setVisible(true);

        q1 c1=new q1();
        q2 c2=new q2();
        q3 c3=new q3();
        gamepanel.add(c1);
        gamepanel.add(c2);
        gamepanel.add(c3);      

    }


    class ExitButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            System.exit(0);
        }
    }

    class NewButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {


        }
    }

    class q1 extends JPanel
    {
        final int radius=25;
        public void paint(Graphics gr)
        {
            setBackground(Color.black);
            gr.drawOval((100/2-radius),(100/2-radius), radius*2, radius*2);
        }
    }

    class q2 extends JPanel
    {
        final int radius=25;
        public void paint(Graphics gr)
        {
            setBackground(Color.black);
            gr.drawOval((95/2-radius),(100/2-radius), radius*2, radius*2);
        }
    }

    class q3 extends JPanel
    {
        final int radius=25;
        public void paint(Graphics gr)
        {
            setBackground(Color.black);
            gr.drawOval((250/2-radius),(50/2-radius), radius*2, radius*2);
        }
    }


}
4

2 回答 2

1

可能,是,推荐,也许不是……

在此处输入图像描述

public class TestLabel {

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

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

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            add(new CircleLable("1"));
            add(new JLabel("What is the capital of SegWay?"));
        }

    }

    public class CircleLable extends JLabel {

        public CircleLable() {
            init();
        }

        public CircleLable(String text) {
            super(text);
            init();
        }

        protected void init() {
            setHorizontalAlignment(CENTER);
            setVerticalAlignment(CENTER);
            setBorder(new EmptyBorder(2, 2, 2, 2));
        }

        @Override
        public Dimension getPreferredSize() {
            Dimension size = super.getPreferredSize();
            size.width = Math.max(size.width, size.height);
            size.height = size.width;
            return size;
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            Insets insets = getInsets();
            int width = (getWidth() - (insets.left + insets.right));
            int height = getHeight() - (insets.top + insets.bottom);
            int radius = Math.max(width, height);

            int x = insets.left + ((width - radius) / 2);
            int y = insets.top + ((height - radius) / 2);

            g2d.drawOval(x, y, radius, radius);

            super.paintComponent(g2d);

            g2d.dispose();
        }

    }

}

我对这个解决方案的问题是有很多可能出错的地方。添加一个图标,更改文本对齐等可以通过它关闭。

就个人而言,我会简单地使用自定义JPanel并呈现我自己的文本(确保组件首先是透明的),但这只是我......

于 2013-02-03T08:15:04.510 回答
0

@MadProgrammer 的想法更好,但您可以将dingbats用于带圆圈的数字:➀、➁、➂、...</p>

于 2013-02-04T14:15:05.883 回答