2015-08-24 116 views
1

在我的JFrame,我在每個線兩條label和一個JComboBox ordred如下:改變位置動態

Label JComboBox Label 
**Bouton** 

用戶可以添加一個新行具有相同的實體。所以,我添加了一個bouton,並在用戶提交一個新行時創建。

Label JComboBox Label 
    **Bouton** 
    Label JComboBox Label 

但是bouton的位置保持相同的位置。即使創建了新行,我如何更改bouton的位置(在頁面末尾)?

我的代碼是波紋管:

public class Display extends JFrame{ 

    public Display(){ 
     super("Test"); 
     setTitle("Test"); 
     setSize(800,800); 
     setResizable(false); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     initComponents(); 
    } 

    private void initComponents(){ 
     setLayout(new GridLayout(0, 3)); 

     JPanel po = new JPanel(); 
     JLabel label = new JLabel("Resource"); 
     po.add(label); 
     add(po); 

     JPanel po2 = new JPanel(); 
     po2.add(new JComboBox<>(new String[] { "option1", "option2", "option3",})); 
     add(po2); 


     JPanel po3 = new JPanel(); 
     JLabel label3 = new JLabel("Something"); 
     po3.add(label3); 
     add(po3); 


     //new 
     JPanel PanelBouton = new JPanel(); 
     JButton bouton = new JButton(new AddResourceAction("Add new")); 
     add(bouton); 
     PanelBouton.add(bouton); 
     add(PanelBouton); 

     PanelBouton = new JPanel(); 
     JLabel vide = new JLabel(""); 
     PanelBouton.add(vide); 
     add(PanelBouton); 

     PanelBouton = new JPanel(); 
     vide = new JLabel(""); 
     PanelBouton.add(vide); 
     add(PanelBouton); 

    } 

class AddResourceAction extends AbstractAction { 

     public AddResourceAction(String n){ 
      super(n); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 

      JPanel po = new JPanel(); 
      JLabel label = new JLabel("Resource"); 
      po.add(label); 
      add(po); 

      JPanel po2 = new JPanel(); 
      po2.add(new JComboBox<>(new String[] { "option1", "option2", "option3",})); 
      add(po2); 


      JPanel po3 = new JPanel(); 
      JLabel label3 = new JLabel("Something"); 
      po3.add(label3); 
      add(po3); 

      revalidate(); 

     } 
} 




    public static void main(String[] args) { 
     /*display panel*/ 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override public void run() { 
       new Display().setVisible(true); 
      } 
     }); 

    } 

} 
+1

我找到了解決辦法。如果有人需要解決這個問題的未來。您可以刪除Bouton並在創建新行後重新創建一個新的Bouton。不要忘記驗證()和重繪()到JFrame。歡呼聲 – Mehdi

+0

這看起來像表格數據給我。爲此,使用'JTable'。 –

回答

2

創建一個分屏,有兩個容器。一個容器(A)用於動態按鈕,另一個容器用於「添加新的」按鈕。將新組件添加到A容器。

查找下面的代碼,說明這個概念與您的情況。 請自擔風險使用 :)

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

public class Display 
    extends JFrame 
{ 
    Box upperBox = new Box(BoxLayout.X_AXIS); 
    Box dynamicBox = new Box(BoxLayout.Y_AXIS); 
    Box staticBox = new Box(BoxLayout.X_AXIS); 

    public Display() 
    { 
     super("Test"); 
     setTitle("Test"); 
     setSize(800,800); 
     setResizable(false); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     initComponents(); 
    } 

    private void initComponents() 
    { 
     //This will be the parent panel for other panels. 
     JPanel panel = new JPanel(); 
     panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 

     upperBox.add(new JLabel("Resource")); 
     upperBox.add(new JComboBox<>(new String[] { "option1", "option2", "option3",})); 
     upperBox.add(new JLabel("Something")); 

     panel.add(upperBox); 

     staticBox.add(new JButton(new AddResourceAction("Add new"))); 

     panel.add(dynamicBox); //just add this box now, it will be filled later with components 
     panel.add(staticBox); 

     add(panel); 
    } 

    class AddResourceAction extends AbstractAction 
    { 
     public AddResourceAction(String n) 
     { 
      super(n); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 

      Box box = new Box(BoxLayout.X_AXIS); 
      box.add(new JLabel("Resource")); 
      box.add(new JComboBox<>(
         new String[] { "option1", "option2", "option3",})); 
      box.add(new JLabel("Something")); 

      dynamicBox.add(box); 

      revalidate(); 
     } 
    } 

    public static void main(String[] args) 
    { 
     /*display panel*/ 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       new Display().setVisible(true); 
      } 
     }); 
    } 
}