0

我有一个带有滚动窗格的 GUI。滚动窗格通过 JPanel 的 JPanel 滚动。我希望能够在 subJPanels 列表中再添加一个并让 JFrame 更新来做到这一点。

现在我更新了数组,但没有更新 JFrame。


package SSCCE;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class Add extends JFrame{
    private JPanel[] branches;
    private JPanel pane; //Pane that stores accounts
    private JScrollPane scroller;
    private JButton newBranch;

    public static void main(String[] args) {
        JFrame frame = new Add();
    }

    public Add(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500, 500);
        this.setTitle("How Do I add?");

        this.setLayout(new BorderLayout());

        this.add(statusBar(), BorderLayout.NORTH);

        populateBranches();

        pane = new JPanel();
        pane.setLayout(new GridLayout(branches.length,1));
        for (int i = 0; i < branches.length; i++){
            pane.add(branches[i]);
        }

        scroller = new JScrollPane(pane);
        scroller.createVerticalScrollBar();
        this.add(scroller,BorderLayout.CENTER);

        this.setVisible(true);
    }

    private JPanel statusBar(){
        JPanel statusBar = new JPanel();
        statusBar.setLayout(new FlowLayout());

        newBranch = new JButton("New Branch");
                newBranch.addActionListener(new ButtonEventHandler());

        statusBar.add(newBranch);
        return statusBar;
    }

    private void populateBranches(){
        branches = new JPanel[2];

        for (int i = 0; i < branches.length; i++){
            branches[i] = new JPanel();
            branches[i].setLayout(new FlowLayout());
            branches[i].add(new JTextField(20));
        }
    }

    private void newBranch(){
        JPanel[] tempBranches = new JPanel[branches.length + 1];
                System.out.println(tempBranches.length);

        for (int i = 0; i < branches.length; i++){
            tempBranches[i] = branches[i];
        }

        tempBranches[branches.length] = new JPanel();
        tempBranches[branches.length].setLayout(new FlowLayout());
        tempBranches[branches.length].add(new JTextField(20));
        branches = tempBranches;

        pane = new JPanel();
        pane.setLayout(new GridLayout(branches.length, 1));
        for (int i = 0; i < branches.length; i++){
            pane.add(branches[i]);
        }
                pane.repaint();
                pane.validate();

        scroller = new JScrollPane(pane);
        this.add(scroller, BorderLayout.CENTER);

        this.repaint();
        this.validate();
    }

    private class ButtonEventHandler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            String btnClkd = event.getActionCommand();
            if (btnClkd.equals("New Branch")){
                newBranch();
            } 
        }
    }
}
4

1 回答 1

1

如果您允许 GridLayout 增长,您可以简单地向其添加更多组件:

// 0 means new rows are added as needed
pane.setLayout(new GridLayout(0, 1));

// ...

private void newBranch(){
    // Create the component
    JPanel branch = new JPanel();
    branch.add(new JTextField(20));
    // + any additional subcomponents

    // and just add it where the others already are
    pane.add(branch);
    pane.revalidate();
}
于 2013-07-31T16:40:39.080 回答