2015-10-05 36 views
0

我正在使用FlowLayout並試圖使JLabelsJTextField依次排列,但我被卡住了,不知道該怎麼做。JLabel和JTextField在Flow Layout中的新行

public class SoftwareProducts extends JPanel implements ActionListener { 
    JTextField ramtf; 
    JTextField processortf; 
    JTextField productIDtf; 
    JTextField productNametf; 
    JTextField productYeartf; 
    JTextField productPublishHousetf; 

    JButton CompleteOrder; 

    JLabel Ramlb = new JLabel("RAM:"); 
    JLabel processorlb = new JLabel("Processor:"); 
    JLabel productIDlb = new JLabel("Product ID"); 
    JLabel productNamelb = new JLabel("Product Name:"); 
    JLabel productYearlb = new JLabel("Product Year:"); 
    JLabel PublishHouselb = new JLabel("Publish House:"); 

    JPanel softwarePanel; 
    CardLayout c2 = new CardLayout(); 

    CompleteOrder completeOrder; 
    public static void main(String[] args) { 
     new SoftwareProducts(); 
    } 

    public SoftwareProducts() { 
     setSize(500, 300); 
     setLayout(new FlowLayout()); 
     add(Ramlb); 
     Ramlb.setFont(new Font("Arial", 5, 28)); 
     ramtf = new JTextField(15); 
     add(ramtf); 
     ramtf.addActionListener(this); 
     add(processorlb); 
     processortf = new JTextField(15); 
     processortf.addActionListener(this); 
     add(processortf); 
     add(productIDlb); 
     productIDtf = new JTextField(10); 
     productIDtf.addActionListener(this); 
     add(productIDtf); 
     add(productNamelb); 
     add(productNamelb); 
     productNametf = new JTextField(10); 
     productNametf.addActionListener(this); 
     add(productNametf); 
     add(productYearlb); 
     productYeartf = new JTextField(10); 
     productYeartf.addActionListener(this); 
     add(productYeartf); 
     add(PublishHouselb); 
     productPublishHousetf = new JTextField(10); 
     productPublishHousetf.addActionListener(this); 
     add(productPublishHousetf); 
     CompleteOrder = new JButton("CompleteOrder"); 
     CompleteOrder.setSize(25, 40); 
     CompleteOrder.addActionListener(this); 
     add(CompleteOrder); 
     softwarePanel = new JPanel(new FlowLayout()); 
     softwarePanel.add(Ramlb); 
     softwarePanel.setLayout(c2); 
     completeOrder = new CompleteOrder(); 
     softwarePanel.add(new JPanel(), "empty"); 
     softwarePanel.add(completeOrder, "completeOrder"); 
     this.add(softwarePanel); 
     this.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent e) { 
     if (e.getSource() == CompleteOrder) { 
      c2.show(softwarePanel, "completeOrder"); 
      float ram = Float.parseFloat(ramtf.getText()); 
      float processor = Float.parseFloat(processortf.getText()); 
      int productID = Integer.parseInt(productIDtf.getText()); 
      String productName = productNametf.getText(); 
      int productYear = Integer.parseInt(productYeartf.getText()); 
      String productPublishHouse = productPublishHousetf.getText(); 

      main.softwareList.add(new Software(productID, productName, productYear, productPublishHouse)); 
      ramtf.setText(""); 
      System.out.println("Saved"); 
     } 
    } 
} 

什麼樣的代碼會導致它在每一行中出現新行? Shat應該被做以使線路在JLabelLTextField中發生故障?

+0

這聽起來像'FlowLayout'僅僅是爲您的目的錯誤的佈局管理器。我無法理解您實際嘗試實現的佈局,但「FlowLayout」只是按文本順序(在大多數語言環境中從左到右,從上到下)佈置組件,而沒有任何額外的空間。 –

+0

有幾種方法可以做到這一點,但我個人建議GridBagLayout – ControlAltDel

+0

感謝您的信息:: – MASOOMI

回答

2

這是兩個完全不同的問題,它們都與LayoutManager沒有任何關係。

對於JTextField: 取自docs

JTextField是一個輕量級組件,允許文本

單行的編輯有沒有辦法在所有加入新行到JTextField(至少沒有合法的方式)。改爲使用JTextArea;換行符就像在控制檯\n中一樣。

JLable像大多數其他JComponent s也可以處理和顯示HTML代碼。因此,在一個JLabel換行是這樣的:

JLabel twoLined = new JLabel("This is one line</br>And this is another one"); 
+0

感謝它很有用 – MASOOMI

+0

'新JLabel(「這個......好的措施。 –

+0

@JoopEggen是的,這是更準確的方式,應該產生相同的結果 – Paul

相關問題