gpt4 book ai didi

java - 如何将新元素添加到 JFrame 并显示它

转载 作者:行者123 更新时间:2023-12-01 14:17:39 25 4
gpt4 key购买 nike

我有一个带有滚动 Pane 的 GUI。滚动 Pane 正在 JPanel 的 JPanel 中滚动。我希望能够向 subJPanel 列表中再添加一个,并通过 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();
}
}
}
}

最佳答案

如果您允许 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();
}

关于java - 如何将新元素添加到 JFrame 并显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17975473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com