Java Arraylist和Jframe
我在这个项目中遇到了Java Arraylist和Jframe,java,arraylist,netbeans,Java,Arraylist,Netbeans,我在这个项目中遇到了数组的问题。我必须保存他们各自班级的所有学生和教师,特别是在这种情况下,我需要能够添加教师并从各自班级中删除他们,同时显示每个班级的信息Aulascoligo是一个ArrayList列表,我在其中存储所有的课程,教授老师也一样。问题是,将类添加到主类中的ArrayList,我认为它不会在JFrame上显示它们。我所有的问题都与在哪里添加教师和班级有关,以及如何使他们也在JFrame上,反之亦然。我真的希望能有一些解决办法 import javax.swing.JOptionP
数组的问题。我必须保存他们各自班级的所有学生和教师,特别是在这种情况下,我需要能够添加教师并从各自班级中删除他们,同时显示每个班级的信息Aulascoligo
是一个ArrayList
列表,我在其中存储所有的课程,教授老师也一样。问题是,将类添加到主类中的ArrayList
,我认为它不会在JFrame
上显示它们。我所有的问题都与在哪里添加教师和班级有关,以及如何使他们也在JFrame
上,反之亦然。我真的希望能有一些解决办法
import javax.swing.JOptionPane;
/**
*
*
*/
public class ventDirector extends javax.swing.JFrame {
/**
* Creates new form ventDirector
*/
public ventDirector() {
initComponents();
this.setLocationRelativeTo(null);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
txtClase = new javax.swing.JTextField();
bttInfoClase = new javax.swing.JButton();
bttAlta = new javax.swing.JButton();
bttBaja = new javax.swing.JButton();
txtProfesor = new javax.swing.JTextField();
txtClaseProfesor = new javax.swing.JTextField();
bttAtras = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Información clase");
txtClase.setText("Introduzca nombre de la clase");
bttInfoClase.setText("GO");
bttInfoClase.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
bttInfoClaseMouseClicked(evt);
}
});
bttInfoClase.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bttInfoClaseActionPerformed(evt);
}
});
bttAlta.setText("Alta profesor");
bttAlta.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
bttAltaMouseClicked(evt);
}
});
bttAlta.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bttAltaActionPerformed(evt);
}
});
bttBaja.setText("Baja profesor");
bttBaja.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
bttBajaMouseClicked(evt);
}
});
txtProfesor.setText("Nombre profesor");
txtProfesor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtProfesorActionPerformed(evt);
}
});
txtClaseProfesor.setText("Nombre clase");
bttAtras.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sudoku/back-arrow_icon-icons.com_72866 (1).png"))); // NOI18N
bttAtras.setContentAreaFilled(false);
bttAtras.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
bttAtrasMouseClicked(evt);
}
});
bttAtras.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bttAtrasActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(92, 92, 92)
.addComponent(bttAlta)
.addGap(53, 53, 53)
.addComponent(bttBaja))
.addGroup(layout.createSequentialGroup()
.addGap(54, 54, 54)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(txtClase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bttInfoClase))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(txtClaseProfesor, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtProfesor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE))))
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(bttAtras, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(46, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(74, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtClase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bttInfoClase))
.addGap(50, 50, 50)
.addComponent(txtProfesor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtClaseProfesor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bttBaja)
.addComponent(bttAlta))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bttAtras, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(17, 17, 17))
);
pack();
}// </editor-fold>
private void bttInfoClaseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void bttInfoClaseMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
boolean flag =false;
for(int i =0; i<aulasColegio.size();i++){
if (aulasColegio.get(i).getNombreClase() == txtClase.getText()){
JOptionPane.showMessageDialog(this, aulasColegio.get(i).getNombreClase()+ "\n" +
aulasColegio.get(i).getProfesores()+ "\n" + aulasColegio.get(i).getAlumnos() + aulasColegio.get(i).getNumAlumnos());
flag =true;
}
}
if (flag = false) JOptionPane.showMessageDialog(this,"No existe la clase");
}
private void txtProfesorActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void bttAltaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void bttAltaMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
boolean flag =false;
Profesor profe = new Profesor(txtProfesor.getText());
for(int i =0; i<aulasColegio.size();i++){
if (aulasColegio.get(i).getNombreClase() == txtClase.getText()){
aulasColegio.get(i).anadeProfesor(profe);
JOptionPane.showMessageDialog(this, "Añadido con éxito");
flag =true;
}
else JOptionPane.showMessageDialog(this, "No es esta clase");
}
if (flag = false) JOptionPane.showMessageDialog(this, "Ese profesor no se encuentra en la clase");
}
private void bttBajaMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
boolean flag =false;
for(int i =0; i<aulasColegio.size();i++){
if (aulasColegio.get(i).getNombreClase() == txtClase.getText()){
for(int j = 0; j<profesores.size(); j++){
if (profesores.get(j).getProfesor() == txtProfesor.getText()){
aulasColegio.get(i).eliminaProfesor(profesores.get(j));
JOptionPane.showMessageDialog(this, "Eliminado con éxito");
flag= true;
}
}
}
}
if (flag = false) JOptionPane.showMessageDialog(this, "Ese profesor no se encuentra en la clase");
}
private void bttAtrasActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void bttAtrasMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
login_1 L = new login_1();
L.setVisible(true);
dispose();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ventDirector.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ventDirector.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ventDirector.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ventDirector.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ventDirector().setVisible(true);
}
});
}
private ArrayList<Clase> aulasColegio = new ArrayList<Clase>();
private ArrayList<Profesor> profesores = new ArrayList<Profesor>();
// Variables declaration - do not modify
private javax.swing.JButton bttAlta;
private javax.swing.JButton bttAtras;
private javax.swing.JButton bttBaja;
private javax.swing.JButton bttInfoClase;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField txtClase;
private javax.swing.JTextField txtClaseProfesor;
private javax.swing.JTextField txtProfesor;
// End of variables declaration
}
import javax.swing.JOptionPane;
/**
*
*
*/
公共类ventDirector扩展了javax.swing.JFrame{
/**
*创建新表单ventDirector
*/
公共文特目录(){
初始化组件();
此.setLocationRelativeTo(空);
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
jLabel1=newjavax.swing.JLabel();
txtClase=newjavax.swing.JTextField();
bttInfoClase=newjavax.swing.JButton();
bttAlta=newjavax.swing.JButton();
bttBaja=newjavax.swing.JButton();
txtProfesor=newjavax.swing.JTextField();
txtClaseProfesor=newjavax.swing.JTextField();
bttAtras=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText(“Información clase”);
setText(“clase名称介绍”);
bttInfoClase.setText(“GO”);
bttInfoClase.addMouseListener(新java.awt.event.MouseAdapter(){
public void mouseClicked(java.awt.event.MouseEvent evt){
bttInfoClaseMouseClicked(evt);
}
});
bttInfoClase.addActionListener(新的java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
bttInfoClaseActionPerformed(evt);
}
});
bttAlta.setText(“Alta profesor”);
bttAlta.addMouseListener(新java.awt.event.MouseAdapter(){
public void mouseClicked(java.awt.event.MouseEvent evt){
BTTALTAMOUSE(evt);
}
});
bttAlta.addActionListener(新的java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行的BTTAL操作(evt);
}
});
bttBaja.setText(“巴哈教授”);
addMouseListener(新java.awt.event.MouseAdapter(){
public void mouseClicked(java.awt.event.MouseEvent evt){
BTTbajamouseClacked(evt);
}
});
txtProfesor.setText(“Nombre profesor”);
addActionListener(新java.awt.event.ActionListener()){
public void actionPerformed(java.awt.event.ActionEvent evt){
txtProfessorActionPerformed(evt);
}
});
txtClaseProfesor.setText(“Nombre clase”);
bttAtras.setIcon(新的javax.swing.ImageIcon(getClass().getResource(“/sudoku/back-arrow_icon-icons.com_72866(1.png));//NOI18N
bttAtras.setContentAreaFilled(false);
addMouseListener(新java.awt.event.MouseAdapter(){
public void mouseClicked(java.awt.event.MouseEvent evt){
bttAtrasMouseClicked(evt);
}
});
bttAtras.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行的bttatrasaction(evt);
}
});
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(92,92,92)
.addComponent(bttAlta)
.addGap(53,53,53)
.addComponent(bttBaja))
.addGroup(layout.createSequentialGroup()
.addGap(54,54,54)
.addComponent(jLabel1,javax.swing.GroupLayout.PREFERRED_SIZE,107,javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(txtClase,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bttInfoClase))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.training,false)
.addComponent(txtClaseProfesor、javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtProfesor,javax.swing.GroupLayout.Alignment.LEADING,javax.swing.GroupLayout.DEFAULT\u SIZE,95,Short.MAX\u VALUE)))
.addGroup(layout.createSequentialGroup()
import java.util.ArrayList;
/**
*
* @author
*/
public class SudokuNino {
private String nombreColegio;
private String nombreDirector;
private ArrayList<Clase> aulasColegio = new ArrayList<Clase>();
//private Partida[] partidas;
public void SudokuNiño(){};
/*EXCEPCION */
public void SudokuNiño(String c, String d){
this.nombreColegio= c;
this.nombreDirector= d;
};
public static void main(String[] args) {
// TODO code application logic here
SudokuNino main = new SudokuNino();
Clase clase1 = new Clase("girasoles");
main.aulasColegio.add(clase1);
}
}
for(int i = 0; i<aulasColegio.size();i++){
...
}
String test=new String("hello");
String test2=new String("hello");
System.out.println(test == test2);
System.out.println(test.equals(test2));
false
true