2012-04-05 35 views

回答

9

通過將所需部件添加到JPanel來使用「複合組件」。例如。

TimeBeforeClass

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

class TimeBeforeClass { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JPanel gui = new JPanel(new FlowLayout(FlowLayout.LEFT, 3,3)); 
       gui.add(new JLabel("Open")); 
       gui.add(new JSpinner(new SpinnerNumberModel(15,0,20,1))); 
       gui.add(new JLabel("minutes before class")); 
       JOptionPane.showMessageDialog(null, gui); 
      } 
     }); 
    } 
} 

注意,我交換了「文本字段」的一個JSpinner - 一個更合適的組件,用於選擇「以分鐘時間」。

1

我想我還沒有明白。不過,我會嘗試:

你可以從一個TextField文本:

label.setText( 「開放」 + textField.getText()+ 「級之前分鐘」);

+0

這可能是一個很好的選擇,只是OP未能進一步闡述手頭的情況。 +1這個:-) – 2012-04-05 04:51:13

2
Can I have a textfield inside a label? 
  • 答案是,是的,你可以,這是AWT/Swing對象

  • JComboBoxJTableJListJSpinnerJFile(Color)Chooser ....是複合JComponents,可以提取的Java的基本屬性全部JComponent並再次放在一起。

  • 你可以在API中把任何的JComponents到另一個

  • JFrame/JDialog/JWindowJPanel已經得到實施LayoutManager默認情況下,for rest of then you have to implements proper LayoutManager

相關問題