Java 应用程序 - 添加、删除、重新排序 JButton 的元素
我正在开发一个Java 应用程序。
我使用 MockupScreens 创建了这个界面。 请看这些图片。
第一次,只有一个元素,用户必须输入信息(标题和描述),然后开始根据需要添加元素。 他可以随时编辑元素信息。 他也可以删除或更改该元素的顺序...
我怎样才能创建类似图片的东西???
提前致谢。 最好的问候,
阿里。
我知道 Java Swing 中的这些部分。 我的问题是如何动态插入这块按钮。
我得到一个想法,我必须将 JButtons 放在 JPanel 上,然后通过添加、删除和重新排序来操作 JPanel ... 因此,网格布局可以有效地在每个面板之后添加每个面板,但考虑重新排序顺序将非常困难......
请提出任何建议。 :)
经过搜索,我得到一个想法:
让我们将这些 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.
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.
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:
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();
}
});
}
}
Please can you help me in this code.
I need just the first push to go on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在按钮的 ActionListener 中:
In your ActionListener for the button:
漂亮的图像 :-)
如果您想在 Swing 中执行此操作,您需要这些部分:
查看这些,开始编码,然后返回如果你有具体的问题。祝你好运!
Nice images :-)
You need these parts, if you want to do this in Swing:
Look at these, start to code, and then come back if you have concrete problems. Good luck!
我认为您在这里使用了错误的习语。您想要完成的任务通常是使用 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
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