Однако, если в текст что-то набрать поле, то нажатие кнопки «Добавить нового конкурента» приведет к добавлению поля, в котором нет ни одного подходящего компонента, и небольшого нечитаемого фрагмента текста вверху.
Класс GUI:
Код: Выделить всё
import java.awt.event.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;
public class GUI extends JFrame implements ActionListener {
private JPanel inputPanel, inputComponentPanel;
public GUI() {
super("GUI");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(CommonConstants.GUI_SIZE);
pack();
setLocationRelativeTo(null);
setLayout(CommonConstants.getLayout());
addGUIComponents();
}
private void addGUIComponents() {
//banner
JLabel bannerText = new JLabel("GUI Application");
//inputPanel
inputPanel = new JPanel();
inputPanel.setLayout(CommonConstants.getLayout());
//inputPanelContainer
inputComponentPanel = new JPanel();
inputComponentPanel.setLayout(CommonConstants.getLayout());
inputPanel.add(inputComponentPanel, "wrap");
//newCompButton
JButton newCompButton = new JButton(CommonConstants.NEW_COMP_BUTTON);
newCompButton.addActionListener(this);
inputPanel.add(newCompButton, "wrap");
//inputPanel
JScrollPane scrollPane = new JScrollPane(inputPanel);
inputComponentPanel.add(new InputComponent(inputComponentPanel), "wrap");
this.getContentPane().add(bannerText, "wrap");
this.getContentPane().add(scrollPane, "wrap");
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equalsIgnoreCase(CommonConstants.NEW_COMP_BUTTON)) {
InputComponent inputComponent = new InputComponent(inputComponentPanel);
inputComponentPanel.add(inputComponent, "wrap 10");
inputComponent.getPlay1TextField().requestFocus();
repaint();
revalidate();
}
}
}
Класс InputComponent:
Код: Выделить всё
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class InputComponent extends JPanel implements ActionListener {
private JLabel compLabel, play1Label, play2Label, play3Label, play4Label;
private JTextField compTextField, play1TextField, play2TextField, play3TextField, play4TextField;
private JButton setButton, deleteButton;
private JPanel parentPanel;
public JTextField getPlay1TextField() {
return play1TextField;
}
public InputComponent(JPanel parentPanel) {
this.parentPanel = parentPanel;
setLayout(CommonConstants.getLayout());
setBackground(Color.YELLOW);
compLabel = new JLabel("Competitor");
compTextField = new JTextField(30);
play1Label = new JLabel("Player 1");
play1TextField = new JTextField(25);
play2Label = new JLabel("Player 2");
play2TextField = new JTextField(25);
play3Label = new JLabel("Player 3");
play3TextField = new JTextField(25);
play4Label = new JLabel("Player 4");
play4TextField = new JTextField(25);
setButton = new JButton("Set");
deleteButton = new JButton("Delete");
deleteButton.addActionListener(this);
add(compLabel, "cell 0 0");
add(compTextField, "cell 1 0");
add(deleteButton, "cell 2 0");
add(play1Label, "cell 0 1");
add(play1TextField, "cell 1 1");
add(play2Label, "cell 0 2");
add(play2TextField, "cell 1 2");
add(setButton, "cell 2 2");
add(play3Label, "cell 0 3");
add(play3TextField, "cell 1 3");
add(play4Label, "cell 0 4");
add(play4TextField, "cell 1 4");
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase(CommonConstants.DELETE_INPUT)) {
parentPanel.remove(this);
parentPanel.repaint();
parentPanel.revalidate();
}
}
}
Кроме того, в каждом поле есть кнопка «Удалить», которая удаляет его из графического интерфейса. Если что-то введено в текстовое поле, а затем закрывающее поле удалено, проблема исчезает.
Графический интерфейс правильно отображает исходное поле.
Поле отображается неправильно после ввода текста в предыдущий JTextField
Подробнее здесь: https://stackoverflow.com/questions/790 ... jtextfield
Мобильная версия