如何在JFrame中的圆圈内放置标签?

时间:2013-02-03 07:51:17

标签: java swing jframe

我正在设计一个与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);
        }
    }


}

2 个答案:

答案 0 :(得分:1)

可能,是的,推荐,也许不是......

enter image description here

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并将文本渲染为我自己(确保组件首先是透明的),但那只是我...

答案 1 :(得分:0)

@MadProgrammer的想法更好,但您可以使用dingbats表示带圆圈的数字:➀,➁,➂,...