У меня возникла проблема, и я не знаю, как ее решить. В моем коде ниже вы можете увидеть, что я пытаюсь создать графический интерфейс, который автоматически заполняет 20 jRadioButtons с предоставленной информацией, но когда я запускаю программу, кнопки не видны. Может ли кто-нибудь указать мне, что я делаю неправильно? упаковка человек;Как автоматически заполнять jRadioButtons с информацией?
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class PersonInfoUI extends javax.swing.JFrame {
public PersonInfoUI() {
initComponents();
ButtonGroup buttonGroup1 = new ButtonGroup();
JRadioButton jRadioButton1 = new JRadioButton();
personSelectorUI.add(jRadioButton1);
personSelectorUI.revalidate();
personSelectorUI.repaint();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
personSelectorUI = new javax.swing.JPanel();
exitButton = new javax.swing.JButton();
clearButton = new javax.swing.JButton();
personInfoOutput = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
exitButton.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
exitButton.setText("Exit");
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitButtonActionPerformed(evt);
}
});
clearButton.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
clearButton.setText("Clear");
clearButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout personSelectorUILayout = new
javax.swing.GroupLayout(personSelectorUI);
personSelectorUI.setLayout(personSelectorUILayout);
personSelectorUILayout.setHorizontalGroup(
personSelectorUILayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA DING)
.addGroup(personSelectorUILayout.createSequentialGroup()
.addContainerGap()
.addGroup(personSelectorUILayout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING)
.addComponent(personInfoOutput,
javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
personSelectorUILayout.createSequentialGroup()
.addComponent(exitButton,
javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(clearButton,
javax.swing.GroupLayout.PREFERRED_SIZE, 349,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
personSelectorUILayout.setVerticalGroup(
personSelectorUILayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA DING)
.addGroup(personSelectorUILayout.createSequentialGroup()
.addGap(151, 151, 151)
.addComponent(personInfoOutput,
javax.swing.GroupLayout.PREFERRED_SIZE, 121,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(personSelectorUILayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(exitButton, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
.addComponent(clearButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(personSelectorUI, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(personSelectorUI, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>
private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {
personInfoOutput.setText("");
}
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
ButtonGroup buttonGroup1 = new ButtonGroup();
List<Person> people = new ArrayList<>();
people.add(new Person ("Nate Stoll", true, true, 1981));
people.add(new Person ("Ashley Stoll", true, true, 1985));
people.add(new Person ("Brooke Jackson", true, true, 1972));
people.add(new Person ("Reed Stoll", true, true, 1983));
people.add(new Person ("Reeda Stoll", true, true, 1942));
people.add(new Person ("John Stoll", true, true, 1940));
people.add(new Person ("Clark Kent", true, true, 1912));
people.add(new Person ("Reed Richards", true, true, 1992));
people.add(new Person ("Peter Parker", true, true, 1924));
people.add(new Person ("Charles Xavier", true, true, 1905));
people.add(new Person ("Bruce Banner", true, true, 1980));
people.add(new Person ("Cheri Monaghan", true, true, 1979));
people.add(new Person ("Matthew Groathouse", true, true, 1949));
people.add(new Person ("John Williams", true, true, 1958));
people.add(new Person ("Jake Holmes", true, true, 1998));
people.add(new Person ("Bradley Cooper", true, true, 2015));
people.add(new Person ("Shirley Temple", true, true, 1907));
people.add(new Person ("Natalie Stoll", true, true, 1900));
people.add(new Person ("Lindsay Gonzalez", true, true, 1970));
people.add(new Person ("Tommy Chong", true, true, 1957));
for(Person aPers : people){
final JRadioButton jRadioButton1 = new JRadioButton(aPers.getName());
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
if (jRadioButton1.isSelected()){
personInfoOutput.setText(jRadioButton1.getText()); }
}
});
buttonGroup1.add(jRadioButton1);
}
new PersonInfoUI().setVisible(true);
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new PersonInfoUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton clearButton;
private javax.swing.JButton exitButton;
private static javax.swing.JTextField personInfoOutput;
private static javax.swing.JPanel personSelectorUI;
// End of variables declaration
}
Спасибо!
Большое вам спасибо! Это помогло мне так сильно, и я понял, что выяснено. –