java - 动态改变JBouton的位置

标签 java swing jframe jpanel jbutton

在我的 JFrame 中,每一行都有两个 label 和一个 JComboBox orderred,如下所示:

Label    JComboBox    Label
**Bouton**

用户可以添加具有相同实体的新行。因此,我添加了一个按钮,当用户提交时创建一个新行。

    Label    JComboBox    Label
    **Bouton**
    Label    JComboBox    Label

但是按钮的位置保持相同的位置。即使创建了新行,如何更改按钮的位置(在页面末尾)?

我的代码如下:

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);
            }
        });

    }

}

最佳答案

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

下面的代码可以根据您的情况说明这个概念。 使用风险自负:)

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);
            }
        });
    }
}

关于java - 动态改变JBouton的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32178305/

相关文章:

java - JPanel:显示图像的最简单方法是什么?

java - 程序中的各种 JFrame

java - java 中我的框架上的 Jbutton 位置不起作用

java - 启用按钮组

java - 如何打印带或不带标题栏的JFrame?

java - 如何对返回 Java 字符串的方法进行单元测试?

java - 递归查找字符串中字母出现的次数

java - 使用 Elasticsearch 搜索后应用过滤器

java - 在自定义线程中处理 RunTimeException

java - 为具有 2 列(字符串、整数)和 n 行的 JTable 实现自定义 CellEditor