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 のアイデアの方が優れていますが、丸で囲まれた数字には絵文字を使用できます: ➀、➁、➂、…</p >

于 2013-02-04T14:15:05.883 に答える