0

所以我在这里有这段代码,它应该根据用户按下的次数动态添加文本字段和按钮,New Column但它没有添加任何内容。

newColumn.setOnAction(new EventHandler<ActionEvent>(){
        @Override
        public void handle(ActionEvent e){
                String column = columns.getText();
                columns.clear();
                final HBox hbox1 = new HBox();
                final TextField textField = new TextField();
                textField.setText(column);
                Button delete = new Button("X");
                vbox.getChildren().add(hbox1);

                delete.setOnAction(new EventHandler<ActionEvent>(){
                    @Override
                    public void handle(ActionEvent e){
                        vbox.getChildren().remove(hbox1);
                    }
                });
            }
        });

这是应该添加新按钮的代码部分。这是显示窗口和其他所有代码的其余代码:

package GUI;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Example extends Application{

/**
 * @param args
 */
public static void main(String[] args) {
    launch(args);

}

@Override
    public void start(Stage primaryStage) {
    Scene scene = new Scene(new Group());
    primaryStage.setTitle("Parameters");
    primaryStage.setWidth(500);
    primaryStage.setHeight(800);

    showWindow(scene);

    primaryStage.setScene(scene);
    primaryStage.show();
}

public void showWindow(Scene scene){
    final VBox vbox = new VBox();
    final HBox hbox = new HBox();
    final TextField columns = new TextField();
    Button newColumn = new Button("New Column");
    Button done = new Button("Done");

    hbox.setSpacing(5);
    hbox.getChildren().addAll(columns, newColumn);
    vbox.setSpacing(5);
    vbox.setPadding(new Insets(20, 0, 0, 20));
    vbox.getChildren().addAll(hbox);

    newColumn.setOnAction(new EventHandler<ActionEvent>(){
        @Override
        public void handle(ActionEvent e){
                String column = columns.getText();
                columns.clear();
                final HBox hbox1 = new HBox();
                final TextField textField = new TextField();
                textField.setText(column);
                Button delete = new Button("X");
                vbox.getChildren().add(hbox1);

                delete.setOnAction(new EventHandler<ActionEvent>(){
                    @Override
                    public void handle(ActionEvent e){
                        vbox.getChildren().remove(hbox1);
                    }
                });
            }
        });

        vbox.getChildren().addAll(done);
        ((Group) scene.getRoot()).getChildren().addAll(vbox);

}

}

如果有帮助,我也在使用 JavaFX。

4

1 回答 1

2

如果您想动态地添加和删除JComponents,请考虑将它们存储在ArrayListorVector中。一开始会有一个默认值JTextField,然后用户会一个接一个地添加。

actionPerformed()JButton,创建一个JTextField您想要的任何大小的新文件。调用invalidate()您的内容窗格,JFrame然后添加您需要的任何组件。

好走!

SSCCE

package stack;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class DynamicButtons extends JFrame{

    JButton addMore = new JButton("AddMore");
    Container contentPane = null;

    public DynamicButtons(){

        contentPane = this.getContentPane();
        contentPane.setLayout(new FlowLayout());

        addMore.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton newButton = new JButton("ABCD");
                contentPane.invalidate();
                contentPane.add(newButton);
                pack();

            }
        });

        contentPane.add(addMore);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run(){
                new DynamicButtons();
            }
        });
    }
}  

OP 需要 JavaFX
1。得到Pane你正在使用的任何东西。
2.调用yourPane.getChildren()
3.在按钮的监听器中,只要将孩子添加到你上面得到的列表中

于 2013-10-24T01:26:45.463 回答