2
我想建立一個註冊,以便當一個按鈕被點擊時,一個新的條目出現在一張桌子上。理想情況下,我正在尋找在屏幕左側創建一個表格,單擊按鈕時會出現兩行和一列新列。我希望表格具有固定的大小,但在一定數量的條目後它應該可以滾動。到目前爲止,我已經創建並格式化了我想要單擊的JButton
對象以顯示新條目。我也知道我應該使用JTable
來追求這一點。創建動態表添加新項與按鈕
我該如何去做這個動態表?
到目前爲止的代碼:
private void addRegister(JPanel pane) {
JPanel everythingPane = new JPanel();
JPanel pluPane = new JPanel();
Dimension button = new Dimension(200,150);
JTable pluTable = new JTable();
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.Y_AXIS));
//JPanel buttonPane = new JPanel (new FlowLayout (FlowLayout.LEFT));
JPanel subPane1 = new JPanel();
subPane1.setAlignmentX(LEFT_ALIGNMENT);
JPanel subPane2 = new JPanel();
subPane2.setAlignmentX(LEFT_ALIGNMENT);
JPanel subPane3 = new JPanel();
subPane3.setAlignmentX(LEFT_ALIGNMENT);
JPanel subPane4 = new JPanel();
subPane4.setAlignmentX(LEFT_ALIGNMENT);
JPanel subPane5 = new JPanel();
subPane5.setAlignmentX(LEFT_ALIGNMENT);
JPanel alignmentLayer = new JPanel();
JButton frappuccino = new JButton("Frappuccino");
JButton icedCoffee = new JButton("Iced Coffee");
frappuccino.setPreferredSize(button);
icedCoffee.setPreferredSize(button);
JButton arizona = new JButton("Arizona Green Tea");
JButton izze = new JButton("Izze");
arizona.setPreferredSize(button);
izze.setPreferredSize(button);
JButton snapple = new JButton("Snapple");
JButton gatorade = new JButton("Gatorade");
snapple.setPreferredSize(button);
gatorade.setPreferredSize(button);
JButton water = new JButton("Water");
JButton appleJuice = new JButton("Apple Juice");
water.setPreferredSize(button);
appleJuice.setPreferredSize(button);
JButton orangeJuice = new JButton("Orange Juice");
JButton mentos = new JButton("Mentos");
orangeJuice.setPreferredSize(button);
mentos.setPreferredSize(button);
JButton gum = new JButton("Gum");
JButton cliffBar = new JButton("Cliff Bar");
gum.setPreferredSize(button);
cliffBar.setPreferredSize(button);
JButton littleBites = new JButton("Little Bites");
JButton welchs = new JButton("Welch's Fruit Snacks");
littleBites.setPreferredSize(button);
welchs.setPreferredSize(button);
JButton fiberOneBar = new JButton("Fiber One Bar");
JButton fiberOneBrownie = new JButton("Fiber One Brownie");
fiberOneBar.setPreferredSize(button);
fiberOneBrownie.setPreferredSize(button);
JButton cheezeIts = new JButton("Cheeze Its");
JButton goldFish = new JButton("Gold Fish");
cheezeIts.setPreferredSize(button);
goldFish.setPreferredSize(button);
JButton teaBag = new JButton("Tea Bag");
JButton poptarts = new JButton("Poptarts");
teaBag.setPreferredSize(button);
poptarts.setPreferredSize(button);
JButton sampleButton = new JButton("Sample Button");
JButton sampleButton2 = new JButton("Sample Button");
sampleButton.setPreferredSize(button);
sampleButton2.setPreferredSize(button);
JButton sampleButton3 = new JButton("Sample Button");
JButton sampleButton4 = new JButton("Sample Button");
sampleButton3.setPreferredSize(button);
sampleButton4.setPreferredSize(button);
subPane1.add(frappuccino);
subPane1.add(icedCoffee);
subPane1.add(arizona);
subPane1.add(izze);
subPane2.add(snapple);
subPane2.add(gatorade);
subPane2.add(water);
subPane2.add(appleJuice);
subPane3.add(orangeJuice);
subPane3.add(mentos);
subPane3.add(gum);
subPane3.add(cliffBar);
subPane4.add(littleBites);
subPane4.add(welchs);
subPane4.add(fiberOneBar);
subPane4.add(fiberOneBrownie);
subPane5.add(cheezeIts);
subPane5.add(goldFish);
subPane5.add(teaBag);
subPane5.add(poptarts);
alignmentLayer.add(sampleButton);
alignmentLayer.add(sampleButton2);
alignmentLayer.add(sampleButton3);
alignmentLayer.add(sampleButton4);
buttonPane.add(subPane1);
buttonPane.add(subPane2);
buttonPane.add(subPane3);
buttonPane.add(subPane4);
buttonPane.add(subPane5);
buttonPane.add(Box.createRigidArea(new Dimension(5,100)));
buttonPane.add(alignmentLayer);
pluPane.add(pluTable);
everythingPane.add(pluPane);
everythingPane.add(buttonPane);
pane.add(everythingPane);
}
爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。請注意,垃圾代碼的SSCCE比問題中看到的不可編譯代碼片段短。 –
您可以在此[sscce](http://stackoverflow.com/a/19472190/230513)中使用'addRow()'和'addColumn()'進行試驗。 – trashgod