Я написал код для динамического создания JPanels. Теперь мне нужен способ захвата данных, которые пользователь создает в этих JPanels. В настоящее время у меня есть кнопка «Создать». При запуске я хочу, чтобы эта кнопка создавала объект, используя 3 разных типа данных в каждой панели: имя (String), count (int) и логическое значение.Создание объектов из динамических JPanels
Я довольно сырой при программировании, но основным препятствием, с которым я столкнулся, является то, как я могу получить доступ к динамическим объектам, которые не называются такими панелями? Я понял, как их добавлять и удалять, но я не могу понять методы захвата данных из полей в панелях.
Спасибо, код выглядит следующим образом:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class MainSnakeGUI {
public static void main(String[] arg) {
new MainSnakeGUI();
}
public MainSnakeGUI() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
//Create the frame
JFrame frame = new JFrame("Snake Channels");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SnakePane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class SnakePane extends JPanel {
public SnakePane() {
//Create buttons and labels
JButton btnAdd = new JButton("Add New Snake");
JButton btnRemove = new JButton("Delete Snake");
JLabel headerLabel = new JLabel("Create a Split Snake");
JButton btnCreate = new JButton("Create");
setLayout(new BorderLayout());
//Create JPanels
JPanel buttons = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel header = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel footer = new JPanel(new FlowLayout(FlowLayout.CENTER));
//add items to JPanels
buttons.add(btnAdd);
buttons.add(btnRemove);
header.add(headerLabel);
footer.add(btnCreate);
//add JPanels to window
add(header, BorderLayout.NORTH); //sets header at the top of window
header.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
add(buttons, BorderLayout.BEFORE_LINE_BEGINS); //sets the button to the left of the display pane
buttons.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
add(footer, BorderLayout.AFTER_LAST_LINE); //sets buttons on the bottom of window
footer.setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
//add panel to hold new frames
final JPanel content = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weighty = 1;
content.add(new JPanel(), gbc);
//add scroll pane?
add(new JScrollPane(content));
//add listener for button
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SplitSnakeCreationPane pane = new SplitSnakeCreationPane(); //create a new SnakeCreationPane
int insertAt = Math.max(0, content.getComponentCount() - 1); //insert it at the end of the list
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
content.add(pane, gbc, insertAt); //add the pane to the window
content.revalidate();
content.repaint();
}
});
btnRemove.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int paneCount = content.getComponentCount()-1; //get count of the pane
content.remove(content.getComponent(paneCount-1)); //remove the pane at paneCount
content.revalidate(); //update window with changes
content.repaint();
SplitSnakeCreationPane.snakeCount--; //update count for snakes
}
});
btnCreate.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int paneCount = content.getComponentCount()-1; //get count of the pane
for (int i=0; i<paneCount;){
}
}
});
}
//set size of window
@Override
public Dimension getPreferredSize() {
return new Dimension(1200, 400);
}
}
public static class SplitSnakeCreationPane extends JPanel {
//Count of how many snakes created
private static int snakeCount = 1;
public SplitSnakeCreationPane() {
//creates Snake Panes
setLayout(new GridLayout(2, 4, 10, 10));
add(new JLabel("Split Snake " + (snakeCount++) + "."));
add(new JLabel("Snake Name"));
add(new JLabel("Channel Count"));
add(new JLabel(""));
add(new JLabel(""));
JTextField snakeName = new JTextField(30);
JTextField channelCount = new JTextField("0",3);
JCheckBox artistSupplied = new JCheckBox("Artist Supplied?");
add(snakeName); //Snake Name field
add(channelCount); //Channel Count field
add(artistSupplied); //Artist supplied checkbox
setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(10, 10, 10, 10)));
}
}
}
, что вы имеете в виду * создать объект с помощью 3-х различных типов данных в каждой панели * я не мог получить это – Madhan
я с трудом могу понять вопрос. – Mordechai
Извините, если я запутался, я попытаюсь уточнить. Если вы запустите программу, вы увидите, что она создает большую панель с тремя кнопками: «Добавить, удалить и создать». Когда вы нажимаете кнопку «Добавить», создается небольшая панель с двумя текстовыми полями и флажком. Каждый раз, когда вы нажимаете add, он добавляет новую панель с этими тремя полями. Я хочу захватить пользовательский ввод из этих текстовых полей и флажков. Поскольку эти Jpanels являются динамическими, они не объявляются в коде, и я не знаю достаточно, чтобы выяснить, как получить к ним доступ. – TravisinSeattle