Java 应用程序 - 添加、删除、重新排序 JButton 的元素

发布于 2024-10-20 06:32:42 字数 3060 浏览 4 评论 0原文

我正在开发一个Java 应用程序

我使用 MockupScreens 创建了这个界面。 请看这些图片。

在此处输入图像描述

在此处输入图像描述

第一次,只有一个元素,用户必须输入信息(标题和描述),然后开始根据需要添加元素。 他可以随时编辑元素信息。 他也可以删除或更改该元素的顺序...

我怎样才能创建类似图片的东西???

提前致谢。 最好的问候,

阿里。

我知道 Java Swing 中的这些部分。 我的问题是如何动态插入这块按钮。 在此处输入图像描述

我得到一个想法,我必须将 JButtons 放在 JPanel 上,然后通过添加、删除和重新排序来操作 JPanel ... 因此,网格布局可以有效地在每个面板之后添加每个面板,但考虑重新排序顺序将非常困难......

请提出任何建议。 :)


经过搜索,我得到一个想法:

enter image description here

让我们将这些 JButton 放在一个名为 btnsUnit 的 JPanel 中,然后通过添加、删除和重新排序来操作它...因此 GridLayout 将有效地在每个 JPanel 之后添加每个 JPanel ..

这就是为什么我创建了一个新的 JPanel,它将包含未知数量的 ListbtnsUnit JPanel,我将 10 固定为最大数量。

当你回复我时,我正在执行这些步骤。我还没有到达在 ListbtnsUnit JPanel 中添加 btnsUnit JPanel。

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;


public class setupDeviceList extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                setupDeviceList frame = new setupDeviceList();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public setupDeviceList() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 742, 335);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    final JPanel ListbtnsUnit = new JPanel();
    ListbtnsUnit.setBackground(Color.RED);
    ListbtnsUnit.setBounds(55, 56, 243, 191);
    contentPane.add(ListbtnsUnit);
    ListbtnsUnit.setLayout(new GridLayout(10, 0));

    final JButton btnAdd = new JButton("Add");
    btnAdd.setBounds(161, 11, 56, 23);
    btnAdd.setVisible(true);

    btnAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            final JPanel btnsUnit = new JPanel();
            btnsUnit.setBounds(343, 71, 243, 147);
            contentPane.add(btnsUnit);
            btnsUnit.setBackground(Color.ORANGE);
            btnsUnit.setLayout(null);
            btnsUnit.add(btnAdd);

            ListbtnsUnit.add(btnsUnit);
            ListbtnsUnit.revalidate();
            ListbtnsUnit.repaint();
        }
    });
}
}

在此处输入图像描述

请您帮助我编写此代码。 我只需要第一推动力就能继续下去。

I'm developping a Java application.

I created this interface with MockupScreens.
Please look at these pictures.

enter image description here

enter image description here

At first time, there's only one element, the user have to enter informations (title and description) then he starts adding elements as he needs.
He can edit elemnt infomrations at any time.
He can too delete or change the order of this elements ...

How can I do to create something like the pictures up?????

Thanks in advance.
Best regards,

Ali.

I know these parts in Java Swing.
My problem is how to insert this block of buttons dynamically.
enter image description here

I get an idea, I must put JButtons on a JPanel then manipulate the JPanel by adding, removing and reodering...
So a Grid Layout will be efficient to add each panel after each one, but thinking on reordering the order will be so hard ...

Any suggestions please. :)


After searching, I get an idea:

enter image description here

Let us put these JButtons in a JPanel called btnsUnit, then manipulate it by adding, removing and reodering... So a GridLayout will be efficient to add each JPanel after each one ..

Thats why I created a new JPanel which will contain an unknown number of ListbtnsUnit JPanel, I fixed 10 as the max number.

I'm just doing these steps when you reply me. I didn't arrived to add btnsUnit JPanel in ListbtnsUnit JPanel.

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;


public class setupDeviceList extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                setupDeviceList frame = new setupDeviceList();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public setupDeviceList() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 742, 335);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    final JPanel ListbtnsUnit = new JPanel();
    ListbtnsUnit.setBackground(Color.RED);
    ListbtnsUnit.setBounds(55, 56, 243, 191);
    contentPane.add(ListbtnsUnit);
    ListbtnsUnit.setLayout(new GridLayout(10, 0));

    final JButton btnAdd = new JButton("Add");
    btnAdd.setBounds(161, 11, 56, 23);
    btnAdd.setVisible(true);

    btnAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            final JPanel btnsUnit = new JPanel();
            btnsUnit.setBounds(343, 71, 243, 147);
            contentPane.add(btnsUnit);
            btnsUnit.setBackground(Color.ORANGE);
            btnsUnit.setLayout(null);
            btnsUnit.add(btnAdd);

            ListbtnsUnit.add(btnsUnit);
            ListbtnsUnit.revalidate();
            ListbtnsUnit.repaint();
        }
    });
}
}

enter image description here

Please can you help me in this code.
I need just the first push to go on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

面犯桃花 2024-10-27 06:32:42

但是考虑重新排序会很难......

在按钮的 ActionListener 中:

  1. 使用 getSource() 方法获取被单击的按钮,
  2. 使用按钮的 getParent() 方法查找按钮所属的
  3. 面板按钮面板的 getParent() 方法查找其父面板,
  4. 使用 getComponents 方法获取所有按钮面板的数组。然后循环遍历数组以查找您要搜索的面板的索引。
  5. 一旦找到增加或减少索引的索引,您就可以将面板添加回新索引处的父面板。
  6. 然后您 revalidate() 和 repaint() 父面板以在新位置显示面板。

but thinking on reordering the order will be so hard ...

In your ActionListener for the button:

  1. use getSource() method to get the button that was clicked
  2. use the getParent() method of the button to find the panel the button belongs to
  3. use the getParent() method of the buttons panel to find its parent panel
  4. use the getComponents method to get an Array of all the button panels. Then loop through the Array to find the index of the panel you are search for.
  5. once you find the index you increase of decrease the index, then you can add the panel back to its parent panel at the new index.
  6. then you revalidate() and repaint() the parent panel to display the panel in its new location.
简单爱 2024-10-27 06:32:42

漂亮的图像 :-)

如果您想在 Swing 中执行此操作,您需要这些部分:

  • javax.swing.JButton (多个按钮)
  • javax.swing.JTextField (单行文本输入组件)
  • javax.swing.JTextArea (多行 文本输入组件)行文本输入组件)
  • javax.swing.JLabel(输入字段的标签)
  • javax.swing.JPanel(将多个组件分组在一起的容器,例如按钮行或右侧的编辑组件)
  • javax.swing。 JFrame(所有的窗口)
  • javax.swing.Action(单击按钮时执行的操作 - 它们还可以包含按钮的图标和/或文本)

查看这些,开始编码,然后返回如果你有具体的问题。祝你好运!

Nice images :-)

You need these parts, if you want to do this in Swing:

  • javax.swing.JButton (the several buttons)
  • javax.swing.JTextField (the one line text input component)
  • javax.swing.JTextArea (the multi-line text input component)
  • javax.swing.JLabel (labels for your input fields)
  • javax.swing.JPanel (a container to group several components together, like your lines of buttons, or the editing components at the right)
  • javax.swing.JFrame (the window for all together)
  • javax.swing.Action (actions to be performed when a button is clicked - they also can contain an icon and/or text for the button)

Look at these, start to code, and then come back if you have concrete problems. Good luck!

海未深 2024-10-27 06:32:42

我认为您在这里使用了错误的习语。您想要完成的任务通常是使用 JList 完成的(可滚动的项目列表)。这样,您只需要在 JList。操作应该对 JList 中的选定项目进行操作(否则它们应该被禁用)

您的界面可能看起来有点像

在此处输入图像描述

您当前使用的习惯用法主要是在网络上完成的页。桌面 UI(尤其是 Swing)要丰富得多。

您可以在 http://download 中找到有关如何使用 JList 的更多信息。 oracle.com/javase/tutorial/uiswing/components/list.html

I think you are using a wrong idiom here. What you are trying to accomplish is usually done using JList (scrollable list of items). This way you need just one set of operations such as reorder, add and delete, next to the JList. Operations should operate on selected item in JList (otherwise they should be disabled)

Your interface may look sort of like

enter image description here

The idiom you're currently using is mostly done on web pages. Desktop UIs, (especially Swing) are much richer.

You can find more about how to use JList at http://download.oracle.com/javase/tutorial/uiswing/components/list.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文