我正在使用FlowLayout
並試圖使JLabels
和JTextField
依次排列,但我被卡住了,不知道該怎麼做。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應該被做以使線路在JLabel
和LTextField
中發生故障?
這聽起來像'FlowLayout'僅僅是爲您的目的錯誤的佈局管理器。我無法理解您實際嘗試實現的佈局,但「FlowLayout」只是按文本順序(在大多數語言環境中從左到右,從上到下)佈置組件,而沒有任何額外的空間。 –
有幾種方法可以做到這一點,但我個人建議GridBagLayout – ControlAltDel
感謝您的信息:: – MASOOMI