如何在Netbeans上定义jRadioButton的标签位置?

时间:2013-04-11 16:57:37

标签: java swing netbeans label jradiobutton

我想在Netbeans上的buttonGroup上定义jRadioButtons的标签位置,因此标签将位于其radioButton下。可以吗?

2 个答案:

答案 0 :(得分:5)

JRadioButton#setText()setVerticalTextPosition(SwingConstants.BOTTOM)一起使用。

JRadioButton jrb = new JRadioButton();
jrb.setText("Label");
jrb.setVerticalTextPosition(JRadioButton.BOTTOM);
jrb.setHorizontalTextPosition(JRadioButton.CENTER);

enter image description here

答案 1 :(得分:2)

您必须同时使用r.setVerticalTextPosition(JRadioButton.BOTTOM);r.setHorizontalTextPosition(JRadioButton.CENTER);在一起。否则,它将无法正常工作

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

public class PersonFrame extends JFrame
{
    public PersonFrame()
    {
        JRadioButton r = new JRadioButton();
r.setText("Text");
r.setVerticalTextPosition(JRadioButton.BOTTOM);
r.setHorizontalTextPosition(JRadioButton.CENTER);

        JPanel testPanel = new JPanel();
        testPanel.setLayout(new FlowLayout());
        testPanel.add(r);

        this.add(testPanel);
        this.setSize(100,100);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[]args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {

            @Override
            public void run() {
                new PersonFrame();
            }
        });

    }
}