2015-10-05 33 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

相关问题