Java JFrame多次使用一个组件

Java JFrame多次使用一个组件,java,swing,jframe,Java,Swing,Jframe,我正在创建一个固定程序。我有一个页面,上面有“创建”按钮。当用户点击create时,它会在下一页添加一个新按钮。我怎样才能得到它,当用户点击CREATE时,它会添加一个新按钮,而之前的按钮会向下移动50像素(例如) 同样,您应该创建一个工厂方法来创建JPanel i、 e 编辑 你问 这有什么区别 不能将单个组件添加到多个容器中,但可以创建类似的组件并将其添加到多个容器中。使用工厂方法可以做到这一点。有关问题的更多细节,请考虑创建和发布. 作为旁白,您似乎在使用null布局并在组件上调用se

我正在创建一个固定程序。我有一个页面,上面有“创建”按钮。当用户点击create时,它会在下一页添加一个新按钮。我怎样才能得到它,当用户点击CREATE时,它会添加一个新按钮,而之前的按钮会向下移动50像素(例如)


同样,您应该创建一个工厂方法来创建JPanel

i、 e


编辑
你问


这有什么区别

不能将单个组件添加到多个容器中,但可以创建类似的组件并将其添加到多个容器中。使用工厂方法可以做到这一点。有关问题的更多细节,请考虑创建和发布.


作为旁白,您似乎在使用null布局并在组件上调用setBounds(…)。虽然对于新手来说,这似乎是创建复杂GUI的更好方法,但这是一种谬论,创建越多Swing GUI,您就越能学会尊重和使用布局管理器,并看到这些生物极大地帮助创建灵活、美观的复杂GUI(如果需要)


编辑2
注意:如果组件有足够重要的独特行为,那么我同意nachokk,创建一个类来封装它,然后创建这个类的实例,而不是简单的工厂方法


编辑3

关键是,虽然不能将组件多次添加到容器中,但可以多次添加使用相同模型的新组件,这几乎是一样的。您的组件的组件甚至可以共享相同的模型,例如,如果需要,您的jbutton可以共享相同的操作,您的JTextField可以共享相同的文档


编辑4 你说:

一种在用户每次单击“创建”时向下递增按钮的方法。因此,它每次都会添加一个按钮,但它们每次都会向下移动,而不是在同一位置产卵

在这里,布局管理器是关键。用按钮存放JPanel的容器需要使用一个布局管理器,该管理器将接受一个新的JPanel并将其放置在正确的位置。考虑:

  • 接受新JPanel的容器可以使用GridLayout(0,1)——这意味着一个具有一列和可变行数的网格
  • 使用JPanel将此GridLayout放置到使用JPanel的BorderLayout.NORTH位置。这将把网格面板压缩到最小的合理大小,这样JPanel就不会扩展
  • 使用JPanel将BorderLayout放置到JScrollPane中
  • 添加新JPanel时,请使用JPanel将其添加到GridLayout,然后使用JPanel在GridLayout上调用
    revalidate()
    repaint()
    ,以便新JPanel能够正确定位 再次,关于您的问题的更多细节,考虑创建和发布.

编辑5
例如,我的:


您应该提供代码来演示如何消除所有不必要的干扰,以便更快地获得更好的帮助!。。基于你所写的问题(我没有读代码,实际上我读了,但是我读了SETFLASH(NULL),我停止阅读)我想你只需要创建那个对象的新实例。你应该考虑创建一个创建你的JPAND的工厂方法。作为旁白,您似乎在使用null布局并在组件上调用setBounds(…)。虽然对于新手来说,这似乎是创建复杂GUI的更好方法,但这是一种谬论,创建越多Swing GUI,您就越能学会尊重和使用布局管理器,并看到这些生物极大地帮助创建灵活、美观的复杂GUI。@Nachokhow你是什么意思?我的问题在这段代码中,因为JFrame只允许您使用一次组件,我希望它在用户每次点击“创建”按钮时添加一个按钮。对不起,如果有必要,我会尝试添加更多细节。感谢您的反馈。允许您只显示一次同一组件(类的实例也称为对象),但您可以有该组件的多个实例class@HovercraftFullOfEels,我正在使用CardLayout,我将如何使用north等?这有什么区别?@HarryKitchener:请参见编辑来回答。谢谢,因此,我将能够在两个不同的容器上使用一个组件,因为它通常只出现在一个容器上container@HarryKitchener:否,如您所知,您只能将一个组件添加到一个容器中。您的多个组件都必须共享一个相似或相同的模型。想想MVC。谢谢,我把你的答案标记为有用,但没有把它标记为可接受的答案,因为它不是我想要的答案。再次感谢。
createFixtures.setLayout(null);     //Create a new fixture panel components
        createPanelLabel.setBounds(160, 30, 500, 50);
        createPanelLabel.setFont(new Font("TimesRoman", Font.PLAIN, 50));
        createPanelLabel.setForeground(Color.WHITE);
        southBar4.setBounds(0, 730, 500, 1);
        northBar4.setBounds(0, 100, 500, 1);
        backButton2.setBounds(20, 750, 150, 60);
        createButton2.setBounds(320, 750, 150, 60);
        sportLabel.setBounds(30, 150, 500, 40);
        sportLabel.setFont(new Font("TimesRoman", Font.PLAIN, 35));
        sportLabel.setForeground(Color.WHITE);
        descriptionLabel.setBounds(30, 300, 500, 40);
        descriptionLabel.setFont(new Font("TimesRoman", Font.PLAIN, 35));
        descriptionLabel.setForeground(Color.WHITE);
        dateLabel.setBounds(30, 450, 500, 40);
        dateLabel.setFont(new Font("TimesRoman", Font.PLAIN, 35));
        dateLabel.setForeground(Color.WHITE);
        resultLabel.setBounds(30, 600, 500, 40);
        resultLabel.setFont(new Font("TimesRoman", Font.PLAIN, 35));
        resultLabel.setForeground(Color.WHITE);
        sportDropDown.setBounds(250, 150, 200, 40);
        descriptionField.setBounds(250, 300, 200, 100);
        descriptionField.setFont(new Font("TimesRoman", Font.PLAIN, 20));
        descriptionField.setLineWrap(true);
        descriptionField.setWrapStyleWord(true);
        createFixtures.add(southBar4);
        createFixtures.add(northBar4);
        createFixtures.add(createPanelLabel);
        createFixtures.add(backButton2);
        createFixtures.add(createButton2);
        createFixtures.add(sportLabel);
        createFixtures.add(descriptionLabel);
        createFixtures.add(dateLabel);
        createFixtures.add(resultLabel);
        createFixtures.add(sportDropDown);
        createFixtures.add(descriptionField);

container.add(fixtures, "2");   //Labels each panel with a number allowing me to call the number when switching panels also adds panels to main container
        container.add(loginPanel, "3");
        container.add(createFixtures, "4");
        container.add(editFixtures, "5");
        container.add(adminFixturesFootball, "6");
        container.add(adminFixturesSwimming, "7");
        container.add(adminFixturesTennis, "8");

createButton2.addActionListener(new ActionListener() {   //Back button listener, switches back to ADMIN fixtures panel
            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.show(container, "6");
                String descriptionText = descriptionField.getText();
                fixtureDescButton.setText( descriptionText );
                fixtureDescButton2.setText( descriptionText );
            }
        });
public JPanel createPanel(String buttonText, String dataOnPanel) {
  JPanel panel = new JPanel();
  JButton myButton = new JButton(buttonText);
  myButton.addActionListener(new ButtonListener());
  panel.add(myButton);
  // add jtextarea if need be with the dataOnPanel String,...

  return panel;
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class GridButtons extends JPanel {
   private JPanel gridPanel = new JPanel(new GridLayout(0, 1));

   public GridButtons() {
      JPanel topPanel = new JPanel();
      topPanel.add(new JButton(new AddRowAction("Add Row")));

      JPanel middlePanel = new JPanel(new BorderLayout());
      middlePanel.add(gridPanel, BorderLayout.NORTH);
      JScrollPane scrollpane = new JScrollPane(middlePanel);
      scrollpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
      scrollpane.setPreferredSize(new Dimension(400, 400)); 

      setLayout(new BorderLayout());
      add(topPanel, BorderLayout.NORTH);
      add(scrollpane, BorderLayout.CENTER);
   }

   private class AddRowAction extends AbstractAction {

      public AddRowAction(String name) {
         super(name);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         // my factory method here
         JPanel panel = new JPanel();
         panel.add(new JButton("Button"));
         panel.add(new JTextField(20));
         gridPanel.add(panel);
         gridPanel.revalidate();
         gridPanel.repaint();
      }

   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("GridButtons");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new GridButtons());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}