我有一個帶滾動窗格的GUI。滾動窗格通過JPanels的JPanel滾動。我希望能夠再添加一個到subJPanels的列表,並有JFrame更新來做到這一點。我現在有數組更新,但不是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();
}
}
}
}
'JButton的newBranch'沒有獲得通過''加ButtonEventHandler' ActionListener'到'JButton newBranch'或我錯了刪除'this.'和'擴展JFrame {',創建'JFrame'作爲局部變量(如'JButton newBranch') – mKorbel
this.add(scroller,BorderLayout.CENTER); &this.repaint(); & this.validate();可以是myFrame.Xxx – mKorbel
this.setLayout(new BorderLayout()); JFrame在API中有BorderLayout,那麼這個代碼是沒用的,但BorderLayout不適合在運行時添加組件的LayoutManager,因爲只有一個JCOmponent可以放在第五個區域 – mKorbel