Java 动态更改JBouton的位置

Java 动态更改JBouton的位置,java,swing,jframe,jpanel,jbutton,Java,Swing,Jframe,Jpanel,Jbutton,在我的JFrame中,每行有两个label和一个JComboBox顺序如下: Label JComboBox Label **Bouton** 用户可以添加具有相同实体的新行。所以,我添加了一个bouton,当用户提交时,就会创建一个新行 Label JComboBox Label **Bouton** Label JComboBox Label 然而,波顿的位置保持不变。即使创建了新行,如何更改bouton的位置(在页面末尾) 我

在我的JFrame中,每行有两个
label
和一个
JComboBox
顺序如下:

Label    JComboBox    Label
**Bouton**
用户可以添加具有相同实体的新行。所以,我添加了一个bouton,当用户提交时,就会创建一个新行

    Label    JComboBox    Label
    **Bouton**
    Label    JComboBox    Label
然而,波顿的位置保持不变。即使创建了新行,如何更改bouton的位置(在页面末尾)

我的代码如下:

public class Display extends JFrame{

    public Display(){
        super("Test");
        setTitle("Test");
        setSize(800,800);
        setResizable(false);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initComponents();
    }

    private void initComponents(){
        setLayout(new GridLayout(0, 3));

        JPanel po = new JPanel();
        JLabel label = new JLabel("Resource");
        po.add(label);
        add(po);

        JPanel po2 = new JPanel();
        po2.add(new JComboBox<>(new String[] { "option1", "option2", "option3",}));
        add(po2);


        JPanel po3 = new JPanel();
        JLabel label3 = new JLabel("Something");
        po3.add(label3);
        add(po3);


        //new 
        JPanel PanelBouton = new JPanel();
        JButton bouton = new JButton(new AddResourceAction("Add new"));
        add(bouton);
        PanelBouton.add(bouton);
        add(PanelBouton);

        PanelBouton = new JPanel();
        JLabel vide = new JLabel("");
        PanelBouton.add(vide);
        add(PanelBouton);

        PanelBouton = new JPanel();
        vide = new JLabel("");
        PanelBouton.add(vide);
        add(PanelBouton);

    }

class AddResourceAction extends AbstractAction {

        public AddResourceAction(String n){
            super(n);
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            JPanel po = new JPanel();
            JLabel label = new JLabel("Resource");
            po.add(label);
            add(po);

            JPanel po2 = new JPanel();
            po2.add(new JComboBox<>(new String[] { "option1", "option2", "option3",}));
            add(po2);


            JPanel po3 = new JPanel();
            JLabel label3 = new JLabel("Something");
            po3.add(label3);
            add(po3);

            revalidate();

        }
}




    public static void main(String[] args) {
        /*display panel*/
        SwingUtilities.invokeLater(new Runnable() {
            @Override public void run() {
                new Display().setVisible(true);
            }
        });

    }

}
公共类显示扩展JFrame{
公开展览(){
超级(“测试”);
设置标题(“测试”);
设置大小(800800);
可设置大小(假);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
初始化组件();
}
私有组件(){
setLayout(新的GridLayout(0,3));
JPanel po=新的JPanel();
JLabel标签=新的JLabel(“资源”);
采购订单添加(标签);
添加(采购订单);
JPanel po2=新的JPanel();
添加(新JComboBox(新字符串[]{“option1”、“option2”、“option3”、“}));
添加(po2);
JPanel po3=新的JPanel();
JLabel label3=新的JLabel(“某物”);
po3.添加(标签3);
添加(po3);
//新的
JPanel PanelBouton=新的JPanel();
JButton bouton=newjbutton(newaddresourceaction(“addnew”);
添加(bouton);
PanelBouton.添加(bouton);
添加(Bouton);
PanelBouton=新的JPanel();
JLabel vide=新JLabel(“”);
PanelBouton.添加(参见);
添加(Bouton);
PanelBouton=新的JPanel();
vide=新的JLabel(“”);
PanelBouton.添加(参见);
添加(Bouton);
}
类AddResourceAction扩展了AbstractAction{
公共AddResourceAction(字符串n){
超级(n);
}
@凌驾
已执行的公共无效操作(操作事件e){
JPanel po=新的JPanel();
JLabel标签=新的JLabel(“资源”);
采购订单添加(标签);
添加(采购订单);
JPanel po2=新的JPanel();
添加(新JComboBox(新字符串[]{“option1”、“option2”、“option3”、“}));
添加(po2);
JPanel po3=新的JPanel();
JLabel label3=新的JLabel(“某物”);
po3.添加(标签3);
添加(po3);
重新验证();
}
}
公共静态void main(字符串[]args){
/*显示面板*/
SwingUtilities.invokeLater(新的Runnable(){
@重写公共无效运行(){
新显示().setVisible(true);
}
});
}
}

创建一个有两个容器的分隔面板。一个容器(A)用于动态按钮,另一个容器用于按钮“添加新”。将新组件添加到容器中

请在下面查找说明此概念的代码。使用风险自担:)

import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公开课展示
扩展JFrame
{ 
长方体上方长方体=新长方体(长方体布局X_轴);
Box dynamicBox=新框(BoxLayout.Y_轴);
长方体静态长方体=新长方体(长方体布局X_轴);
公开展览()
{
超级(“测试”);
设置标题(“测试”);
设置大小(800800);
可设置大小(假);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
初始化组件();
}
私有组件()
{
//这将是其他面板的父面板。
JPanel面板=新的JPanel();
panel.setLayout(新的BoxLayout(panel,BoxLayout.Y_轴));
大写框。添加(新JLabel(“资源”));
add(新的JComboBox(新字符串[]{“option1”,“option2”,“option3”,“}));
大写框。添加(新JLabel(“某物”);
面板。添加(大写框);
add(newjbutton(newaddresourceaction(“addnew”));
panel.add(dynamicBox);//现在只需添加此框,稍后将用组件填充它
面板。添加(静态框);
添加(面板);
}
类AddResourceAction扩展了AbstractAction
{
公共AddResourceAction(字符串n)
{
超级(n);
}
@凌驾
已执行的公共无效操作(操作事件e){
长方体=新长方体(长方体布局X_轴);
框。添加(新JLabel(“资源”));
添加(新JComboBox)(
新字符串[]{“option1”、“option2”、“option3”、});
添加(新JLabel(“某物”);
dynamicBox.add(框);
重新验证();
}
}
公共静态void main(字符串[]args)
{
/*显示面板*/
SwingUtilities.invokeLater(新的Runnable()
{
@凌驾
公开募捐
{
新显示().setVisible(true);
}
});
}
}

我找到了解决方案。如果有人在这个问题的未来需要解决。创建新行后,您可以删除Bouton并重新创建一个新的Bouton。不要忘记将validate()和repaint()复制到JFrame。干杯在我看来像是表格数据。为此,请使用
JTable
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Display 
    extends JFrame
{ 
    Box upperBox   = new Box(BoxLayout.X_AXIS);
    Box dynamicBox = new Box(BoxLayout.Y_AXIS);
    Box staticBox  = new Box(BoxLayout.X_AXIS);

    public Display()
    {
        super("Test");
        setTitle("Test");
        setSize(800,800);
        setResizable(false);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initComponents();
    }

    private void initComponents()
    {
        //This will be the parent panel for other panels.
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        upperBox.add(new JLabel("Resource"));
        upperBox.add(new JComboBox<>(new String[] { "option1", "option2", "option3",}));
        upperBox.add(new JLabel("Something"));

        panel.add(upperBox);

        staticBox.add(new JButton(new AddResourceAction("Add new")));

        panel.add(dynamicBox); //just add this box now, it will be filled later with components
        panel.add(staticBox);  

        add(panel); 
    }

    class AddResourceAction extends AbstractAction 
    {
        public AddResourceAction(String n)
        {
            super(n);
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            Box box = new Box(BoxLayout.X_AXIS);
            box.add(new JLabel("Resource"));
            box.add(new JComboBox<>(
                        new String[] { "option1", "option2", "option3",}));
            box.add(new JLabel("Something"));

            dynamicBox.add(box);

            revalidate(); 
        }
    }

    public static void main(String[] args) 
    {
        /*display panel*/
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override 
            public void run() 
            {
                new Display().setVisible(true);
            }
        });
    }
}