简体   繁体   English

如何为JPanel的文本/组件之间留空格?

[英]How to do spaces between text/components for JPanel?

    JRadioButton choice1 = new JRadioButton ("Daytime (6:00 A.M. through 5:59 P.M.)"+"$0.07");
    choice1.setActionCommand("0.07");

    JRadioButton choice2 = new JRadioButton ("Evening (6:00 P.M. through 11:59 P.M.)"+"$0.12");
    choice2.setActionCommand("0.12");

    JRadioButton choice3 = new JRadioButton ("Off-Peak (12:00 A.M. through 5:59 A.M.)"+"$0.05");
    choice3.setActionCommand("0.05");

    JButton calculate = new JButton ("Calculate");

    ButtonGroup group = new ButtonGroup();
    group.add(choice1);
    group.add(choice2);
    group.add(choice3);

    JPanel panel = new JPanel ();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    panel.add(choice1);
    panel.add(choice2);
    panel.add(choice3);
    panel.add(calculate);
    JLabel result = new JLabel("Total Cost is: ");
    panel.add(result);
    window.add(panel);

    window.setVisible(true);

As you see, I created a JPanel that has a group of RadioButtons that gives 3 choices to pick. 如您所见,我创建了一个具有一组RadioButtons的JPanel,它提供了3个选择。 What I'm creating is a phone call payment list. 我要创建的是电话付款清单。 I want to have spaces between my time intervals and the fees corresponding to them. 我想在时间间隔和与之对应的费用之间留空格。 For example, having word "Daytime(6:00 AM through 5:59 PM)" appear on the very left, and value "$0.07" on the very right of the window. 例如,在窗口的最左边显示单词“ Daytime(6:00 AM到5:59 PM)”,在窗口的最右边显示值“ $ 0.07”。 How would I do that? 我该怎么做? Thanks very much! 非常感谢!

I do not think it is possible by keeping it the text from the JRadioButton but if you make an appart JLabel for the fee, it should be ok. 我认为不可能通过保留JRadioButton的文本来实现,但是如果您为此付费制作了一个Appart JLabel ,那应该没问题。

JLabel#setHorizontalAlignment(int alignment) takes a JLabel constant as argument and you can align your text to the JLabel.LEFT , JLabel.RIGHT or JLabel.CENTER . JLabel#setHorizontalAlignment(int alignment)JLabel常量作为参数,您可以将文本与JLabel.LEFTJLabel.RIGHTJLabel.CENTER

Here is the output you could expect, it is up to you to find the perfect Layout for your program of course 这是您可能期望的输出,当然,您可以根据自己的程序找到理想的布局

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM