2015-12-06 28 views
0
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); 

正如你看到的,我創建了擁有一批單選按鈕,讓3個選擇挑一個JPanel。我正在創建的是一個電話付款清單。我想在我的時間間隔和相應的費用之間留出空間。例如,在左邊出現「白天(上午6點到下午5點59分)」,並在窗口右側顯示「$ 0.07」。我會怎麼做?非常感謝!如何在JPanel的文本/組件之間做空格?

+0

你嘗試讓他們在JLabel? – JBALI

+0

你需要採取兩種不同成分的值如'JLabel',然後你可以用'BorderLayout.CONSTANTS'設置位置 – Adil

回答

0

我不認爲這是保持它從JRadioButton文本可能的,但如果你讓一個APPART JLabel了費的,應該沒問題。

JLabel#setHorizontalAlignment(int alignment)需要JLabel常數作爲參數,您可以對齊文本到JLabel.LEFTJLabel.RIGHTJLabel.CENTER

這裏是你可以期望的輸出,它是由你來找到你的課程方案的完美佈局

enter image description here

+0

非常感謝兄弟!爲什麼我以前沒有想到這個! :D – xsx

+0

現在我做了第一個:'JLabel payment1 = new JLabel(「$ 0.07」);' – xsx

+0

然後在底部'panel.add(payment1); \t \t payment1.setHorizo​​ntalAlignment(SwingConstants.NORTH);' – xsx

相關問題