java - 在单击时添加 GUI 元素仅适用一次
问题描述
问题很简单。单击一个按钮将添加另一个按钮。这也有效。但只有一次,我希望每次单击都会在前一个按钮下方添加另一个按钮。
你能告诉我问题出在哪里吗?
这是我已经尝试过的:不同的布局,因为我假设按钮是在彼此之上的。一个“GUI Refresh”,因为我以为点击后界面应该更新了……但是没有用。在我最后一次尝试中,我每次单击时都创建了一个新的类对象......不幸的是它也不起作用。
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MVCView();
}
});
}
}
public class MVCView extends JFrame {
private static final long serialVersionUID = 1L;
public MVCView() {
init();
}
private void init() {
BorderLayout bl = new BorderLayout();
setLayout(bl);
this.setTitle("Test");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setExtendedState(this.MAXIMIZED_BOTH);
this.getContentPane().add(addNewRow(), BorderLayout.SOUTH);
this.pack();
this.setVisible(true);
}
public JComponent addNewRow() {
JPanel getSouthPanel = new JPanel();
getSouthPanel.setLayout(new BorderLayout());
JButton plus = new JButton("Add new Row");
plus.addActionListener(new MVCControllerAddLine(this));
getSouthPanel.add(plus, BorderLayout.SOUTH);
return getSouthPanel;
}
}
public class MVCControllerAddLine implements ActionListener{
private MVCView view;
MVCViewNodeContainer mvcViewNodeContainer = new MVCViewNodeContainer();
public MVCControllerAddLine(MVCView view) {
this.view = view;
}
@Override
public void actionPerformed(ActionEvent e) {
view.getContentPane().add(mvcViewNodeContainer.addNewNodeContainer());
view.revalidate();
view.validate();
view.repaint();
}
}
public class MVCViewNodeContainer{
private JPanel panel;
MVCView view;
public MVCViewNodeContainer() {
addNewNodeContainer();
}
public JComponent addNewNodeContainer() {
panel = new JPanel();
panel.setLayout(new GridBagLayout());
JButton addNode = new JButton("Add Node");
addNode.setPreferredSize(new Dimension(100, 120));
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.EAST;
gbc.anchor = GridBagConstraints.NORTHEAST;
gbc.weighty = 1;
gbc.weightx = 1;
panel.add(addNode, gbc);
panel.setMaximumSize(new Dimension(150, 100));
return panel;
}
}
如果您需要更多代码,请告诉我。我试图省略不必要的。
非常感谢您的帮助。
许多问候和一个愉快的周末
A456B123
解决方案
我相信问题是您试图错误地使用BorderLayout。
边界布局布置了一个容器,对其组件进行排列和调整大小以适应五个区域:北、南、东、西和中心。每个区域可能包含不超过一个组件,并由相应的常量标识:NORTH、SOUTH、EAST、WEST 和 CENTER。
你的代码在这里:
@Override
public void actionPerformed(ActionEvent e) {
view.getContentPane().add(mvcViewNodeContainer.addNewNodeContainer());
view.revalidate();
view.validate();
view.repaint();
}
每次都将您的“NodeContainers”添加到 BorderLayout 的同一区域。您需要有一个具有不同布局的面板,您将其放置在其中一个区域中,并将您的按钮或“NodeContainers”添加到该面板中。
推荐阅读
- javascript - 如何在 Vue.js 中为 2 个事件调用一次相同的方法?
- json - 为什么我只能在邮递员测试中检查“数字”,而在针对模式进行测试时检查“整数”?
- linux - 在 IntelliJ IDEA 和 Ubuntu 中打开终端
- html - href 下载仅适用于某些扩展
- angular - 离子单元测试警报控制器
- python - Pandas 系列 - 如何使用字符串进行分组并以更好的方式执行值的平均值
- c - 使用 fgets() 利用 C 程序
- c# - 通过指向对象来获取对象名称 Unity Raycast
- reactjs - 如何在 ReactJS 中的 http://localhost:3000/ 和 TMDB api 之间启用 CORS?
- sql-server - 如何在sql server中检查特定用户的组