03. januar 2008 - 10:42
Der er
5 kommentarer og 1 løsning
Hjælp til Javakode
Hej Jeg sidder fast i en java opgave og har brug for at få et sidste skub til at gøre den færdig. Bla. hjælp til en slet og find knap (fra data i en arrayliste) Er der nogen som kan hjælpe? Jeg sender koden (som zip) til "dig" på mail... Jeg bruger netbeans til at kode i.
Annonceindlæg fra SoftwareOne
03. januar 2008 - 16:32
#1
Send det til mig på perdalkri[at]gmail[dot]com
03. januar 2008 - 17:46
#2
Hvorfor ikke bare poste det her?
03. januar 2008 - 19:46
#3
Det drejer sig om følgende (klasserne JFrameVarelager indholder Arraylist og klassen Vare): 1) Sletknappen (jButtomSlet) skal virker således at man vælger en post på listen (jList1) og klikker på knappen, så den sletter. 2) Findknappen (jButtomFind) skal virke således at man skriver et heltal (int) i feltet "ljVarenummer" og klikker på knappen, så slår den varen op i de øvrige felter. 3) Antal i alt knappen (jButtonAntalialt) skal virke således, at når man klikker på knappen skal den tælle alle tal sammen i Arraylistens "Antalpaalager" (int) og skrive resultatet i feltet "jTextVareialt" Jeg er parat til at give 20o point for hvert rigtigt svar!! - Hvis man må det? Koden på klassen JFrameVarelager: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OptionalDataException; import java.util.ArrayList; public class JFrameVarelager extends javax.swing.JFrame { ArrayList<Vare> varer = new ArrayList<Vare>(); /** * Creates new form JFrameVarelager */ public JFrameVarelager() { initComponents(); } /** 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. */ // <editor-fold defaultstate="collapsed" desc=" Generated Code "> private void initComponents() { jTextFieldVarenummer = new javax.swing.JTextField(); jTextFieldVarenavn = new javax.swing.JTextField(); jTextFieldAntalpaalager = new javax.swing.JTextField(); jButtonTilføj = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jList1 = new javax.swing.JList(); jButtonGem = new javax.swing.JButton(); jTextFieldFejl = new javax.swing.JTextField(); jButtonHent = new javax.swing.JButton(); jlBesked = new javax.swing.JLabel(); ljVarenummer = new javax.swing.JLabel(); jlVarenavn = new javax.swing.JLabel(); jlAntalpaalager = new javax.swing.JLabel(); jlLagerliste = new javax.swing.JLabel(); jTextFieldVarebeskrivelse = new javax.swing.JTextField(); jlBeskrivelse = new javax.swing.JLabel(); jTextFieldSalgspris = new javax.swing.JTextField(); jlSalgspris = new javax.swing.JLabel(); jSeparator1 = new javax.swing.JSeparator(); jlLokation = new javax.swing.JLabel(); jTextFieldLokation = new javax.swing.JTextField(); jlLagerstyring = new javax.swing.JLabel(); jSeparator2 = new javax.swing.JSeparator(); jButtonSlet = new javax.swing.JButton(); jButtonFind = new javax.swing.JButton(); jSeparator3 = new javax.swing.JSeparator(); jButtonAntalialt = new javax.swing.JButton(); jTextVareialt = new javax.swing.JTextField(); jSeparator4 = new javax.swing.JSeparator(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButtonTilføj.setText("Tilf\u00f8j vare"); jButtonTilføj.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonTilføjActionPerformed(evt); } }); jList1.setModel(new javax.swing.AbstractListModel() { String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; public int getSize() { return strings.length; } public Object getElementAt(int i) { return strings[i]; } }); jScrollPane1.setViewportView(jList1); jButtonGem.setText("Gem"); jButtonGem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonGemActionPerformed(evt); } }); jTextFieldFejl.setText("Ingen fejl"); jButtonHent.setText("Hent"); jButtonHent.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonHentActionPerformed(evt); } }); jlBesked.setText("Besked:"); ljVarenummer.setText("Varenummer:"); jlVarenavn.setText("Varenavn:"); jlAntalpaalager.setText("Antal:"); jlLagerliste.setText("Lagerliste:"); jlBeskrivelse.setText("Beskrivelse:"); jlSalgspris.setText("Salgspris:"); jlLokation.setText("Lokation:"); jlLagerstyring.setText("Lagerstyring:"); jButtonSlet.setText("Slet vare"); jButtonSlet.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonSletActionPerformed(evt); } }); jButtonFind.setText("Find vare"); jButtonAntalialt.setText("Antal varer i alt p\u00e5 lager"); jButtonAntalialt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonAntalialtActionPerformed(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() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jlLagerstyring) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 202, Short.MAX_VALUE) .addComponent(jButtonHent) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonGem) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jlLokation) .addComponent(jlAntalpaalager) .addComponent(jlSalgspris) .addComponent(ljVarenummer) .addComponent(jlVarenavn) .addComponent(jlBeskrivelse)) .addGap(35, 35, 35) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jTextFieldVarenummer, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextFieldLokation, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE) .addComponent(jTextFieldVarebeskrivelse, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE) .addComponent(jTextFieldAntalpaalager, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE) .addComponent(jTextFieldSalgspris, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE) .addComponent(jTextFieldVarenavn, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jButtonTilføj) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonSlet, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonFind) .addGap(35, 35, 35)) .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) .addComponent(jlLagerliste) .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jButtonAntalialt) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextVareialt, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jSeparator4, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jlBesked) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE) .addComponent(jTextFieldFejl, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonHent) .addComponent(jButtonGem)) .addComponent(jlLagerstyring)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldVarenummer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(ljVarenummer)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldVarenavn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jlVarenavn)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldVarebeskrivelse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jlBeskrivelse)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldAntalpaalager, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jlAntalpaalager)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldSalgspris, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jlSalgspris)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldLokation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jlLokation)) .addGap(8, 8, 8) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonTilføj) .addComponent(jButtonSlet) .addComponent(jButtonFind)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jlLagerliste) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(16, 16, 16) .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonAntalialt) .addComponent(jTextVareialt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(11, 11, 11) .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldFejl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jlBesked)) .addGap(84, 84, 84)) ); pack(); }// </editor-fold> private void jButtonAntalialtActionPerformed(java.awt.event.ActionEvent evt) { } private void jButtonSletActionPerformed(java.awt.event.ActionEvent evt) { } private void jButtonHentActionPerformed(java.awt.event.ActionEvent evt) { try { GemOgHent goh = new GemOgHent(); varer = (ArrayList<Vare>) goh.hent("serial.dat"); jList1.setListData( varer.toArray() ); } // Rækkefølge skal være den der er opgivet prøv fx at flytte OptionalDataException nederst catch( Exception fejl) { jTextFieldFejl.setText("Primitive datatyper blev fundet i strømmen i stedet for objekter " + fejl.toString() ); } } private void jButtonGemActionPerformed(java.awt.event.ActionEvent evt) { try { GemOgHent goh = new GemOgHent(); goh.gem(varer,"serial.dat"); } catch( Exception fejl) // writeObject smider fx IOException. Generel valgt for eksemplets skyld { jTextFieldFejl.setText("Fejl ved skrivning til fil " + fejl.toString() ); } } private void jButtonTilføjActionPerformed(java.awt.event.ActionEvent evt) { Vare vare = new Vare(); vare.setVarenummer( Integer.parseInt( jTextFieldVarenummer.getText() ) ); vare.setVarenavn( jTextFieldVarenavn.getText()); vare.setVarebeskrivelse( jTextFieldVarebeskrivelse.getText()); vare.setAntalpaalager( Integer.parseInt( jTextFieldAntalpaalager.getText() ) ); vare.setSalgspris( Integer.parseInt( jTextFieldSalgspris.getText() ) ); vare.setLokation( jTextFieldLokation.getText()); varer.add(vare); jList1.setListData( varer.toArray() ); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new JFrameVarelager().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButtonAntalialt; private javax.swing.JButton jButtonFind; private javax.swing.JButton jButtonGem; private javax.swing.JButton jButtonHent; private javax.swing.JButton jButtonSlet; private javax.swing.JButton jButtonTilføj; private javax.swing.JList jList1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JSeparator jSeparator3; private javax.swing.JSeparator jSeparator4; private javax.swing.JTextField jTextFieldAntalpaalager; private javax.swing.JTextField jTextFieldFejl; private javax.swing.JTextField jTextFieldLokation; private javax.swing.JTextField jTextFieldSalgspris; private javax.swing.JTextField jTextFieldVarebeskrivelse; private javax.swing.JTextField jTextFieldVarenavn; private javax.swing.JTextField jTextFieldVarenummer; private javax.swing.JTextField jTextVareialt; private javax.swing.JLabel jlAntalpaalager; private javax.swing.JLabel jlBesked; private javax.swing.JLabel jlBeskrivelse; private javax.swing.JLabel jlLagerliste; private javax.swing.JLabel jlLagerstyring; private javax.swing.JLabel jlLokation; private javax.swing.JLabel jlSalgspris; private javax.swing.JLabel jlVarenavn; private javax.swing.JLabel ljVarenummer; // End of variables declaration }
03. januar 2008 - 23:10
#4
Mulig løsning på spm. 3: private void jButtonAntalialtActionPerformed(java.awt.event.ActionEvent evt){ jTextVareialt.setText("Antal varer ialt: " + varer.size()); } Er det noget i den retning du søger?
04. januar 2008 - 08:56
#5
Hej igen Nu har jeg løst 1 og 2 og har lavet følgende til pkt. 3. Men kan det skrives om, således at man indtaster fx varenummer "123" i jTextFieldVarenummer og når man klikker henter systemet alle øvrige vareoplysninger i de øvrige felter. Altså at man IKKE vælger på listen, men slår op efter varenummer. ------ private void jButtonFindActionPerformed(java.awt.event.ActionEvent evt) { try { Vare v = (Vare) jList1.getSelectedValue(); jTextFieldVarenummer.setText(String.valueOf(v.getVarenummer())); jTextFieldVarenavn.setText(v.getVarenavn()); jTextFieldVarebeskrivelse.setText(v.getVarebeskrivelse()); jTextFieldAntalpaalager.setText(String.valueOf(v.getAntalpaalager())); jTextFieldSalgspris.setText(String.valueOf(v.getSalgspris())); jTextFieldLokation.setText(v.getLokation()); } catch (Exception e) { jTextFieldFejl.setText("Ingen vare valgt"); } } ------ Hele koden: import java.util.ArrayList; public class JFrameVarelager extends javax.swing.JFrame { /** * */ private static final long serialVersionUID = 3156083901284091876L; ArrayList<Vare> varer = new ArrayList<Vare>(); /** * Creates new form JFrameVarelager */ public JFrameVarelager() { initComponents(); } /** 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. */ // <editor-fold defaultstate="collapsed" desc=" Generated Code "> private void initComponents() { this.setTitle("Varelager"); jTextFieldVarenummer = new javax.swing.JTextField(); jTextFieldVarenavn = new javax.swing.JTextField(); jTextFieldAntalpaalager = new javax.swing.JTextField(); jButtonTilføj = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jList1 = new javax.swing.JList(); jButtonGem = new javax.swing.JButton(); jTextFieldFejl = new javax.swing.JTextField(); jButtonHent = new javax.swing.JButton(); jlBesked = new javax.swing.JLabel(); ljVarenummer = new javax.swing.JLabel(); jlVarenavn = new javax.swing.JLabel(); jlAntalpaalager = new javax.swing.JLabel(); jlLagerliste = new javax.swing.JLabel(); jTextFieldVarebeskrivelse = new javax.swing.JTextField(); jlBeskrivelse = new javax.swing.JLabel(); jTextFieldSalgspris = new javax.swing.JTextField(); jlSalgspris = new javax.swing.JLabel(); jSeparator1 = new javax.swing.JSeparator(); jlLokation = new javax.swing.JLabel(); jTextFieldLokation = new javax.swing.JTextField(); jlLagerstyring = new javax.swing.JLabel(); jSeparator2 = new javax.swing.JSeparator(); jButtonSlet = new javax.swing.JButton(); jButtonFind = new javax.swing.JButton(); jSeparator3 = new javax.swing.JSeparator(); jButtonAntalialt = new javax.swing.JButton(); jTextVareialt = new javax.swing.JTextField(); jSeparator4 = new javax.swing.JSeparator(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButtonTilføj.setText("Tilf\u00f8j vare"); jButtonTilføj.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonTilføjActionPerformed(evt); } }); jList1.setModel(new javax.swing.AbstractListModel() { /** * */ private static final long serialVersionUID = -1281332013778059247L; String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }; public int getSize() { return strings.length; } public Object getElementAt(int i) { return strings[i]; } }); jScrollPane1.setViewportView(jList1); jButtonGem.setText("Gem"); jButtonGem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonGemActionPerformed(evt); } }); jTextFieldFejl.setText("Ingen fejl"); jButtonHent.setText("Hent"); jButtonHent.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonHentActionPerformed(evt); } }); jlBesked.setText("Besked:"); ljVarenummer.setText("Varenummer:"); jlVarenavn.setText("Varenavn:"); jlAntalpaalager.setText("Antal:"); jlLagerliste.setText("Lagerliste:"); jlBeskrivelse.setText("Beskrivelse:"); jlSalgspris.setText("Salgspris:"); jlLokation.setText("Lokation:"); jlLagerstyring.setText("Lagerstyring:"); jButtonSlet.setText("Slet vare"); jButtonSlet.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonSletActionPerformed(evt); } }); jButtonFind.setText("Find vare"); jButtonFind.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonFindActionPerformed(evt); } }); jButtonAntalialt.setText("Antal varer i alt p\u00e5 lager"); jButtonAntalialt.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonAntalialtActionPerformed(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() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jlLagerstyring) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 202, Short.MAX_VALUE) .addComponent(jButtonHent) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonGem) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jlLokation) .addComponent(jlAntalpaalager) .addComponent(jlSalgspris) .addComponent(ljVarenummer) .addComponent(jlVarenavn) .addComponent(jlBeskrivelse)) .addGap(35, 35, 35) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jTextFieldVarenummer, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextFieldLokation, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE) .addComponent(jTextFieldVarebeskrivelse, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE) .addComponent(jTextFieldAntalpaalager, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE) .addComponent(jTextFieldSalgspris, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE) .addComponent(jTextFieldVarenavn, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jButtonTilføj) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonSlet, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonFind) .addGap(35, 35, 35)) .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) .addComponent(jlLagerliste) .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jButtonAntalialt) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextVareialt, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jSeparator4, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jlBesked) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE) .addComponent(jTextFieldFejl, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonHent) .addComponent(jButtonGem)) .addComponent(jlLagerstyring)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldVarenummer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(ljVarenummer)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldVarenavn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jlVarenavn)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldVarebeskrivelse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jlBeskrivelse)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldAntalpaalager, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jlAntalpaalager)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldSalgspris, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jlSalgspris)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldLokation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jlLokation)) .addGap(8, 8, 8) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonTilføj) .addComponent(jButtonSlet) .addComponent(jButtonFind)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jlLagerliste) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(16, 16, 16) .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonAntalialt) .addComponent(jTextVareialt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(11, 11, 11) .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldFejl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jlBesked)) .addGap(84, 84, 84)) ); pack(); }// </editor-fold> private void jButtonAntalialtActionPerformed(java.awt.event.ActionEvent evt) { int sum = 0; for (Vare v: varer) { sum += v.getAntalpaalager(); } jTextVareialt.setText(String.valueOf(sum)); } private void jButtonSletActionPerformed(java.awt.event.ActionEvent evt) { try { int i = jList1.getSelectedIndex(); varer.remove(i); jList1.setListData( varer.toArray() ); } catch (Exception e) { jTextFieldFejl.setText("Vælg en vare på listen som skal slettes"); } } private void jButtonFindActionPerformed(java.awt.event.ActionEvent evt) { try { Vare v = (Vare) jList1.getSelectedValue(); jTextFieldVarenummer.setText(String.valueOf(v.getVarenummer())); jTextFieldVarenavn.setText(v.getVarenavn()); jTextFieldVarebeskrivelse.setText(v.getVarebeskrivelse()); jTextFieldAntalpaalager.setText(String.valueOf(v.getAntalpaalager())); jTextFieldSalgspris.setText(String.valueOf(v.getSalgspris())); jTextFieldLokation.setText(v.getLokation()); } catch (Exception e) { jTextFieldFejl.setText("Ingen vare valgt"); } } @SuppressWarnings("unchecked") private void jButtonHentActionPerformed(java.awt.event.ActionEvent evt) { try { GemOgHent goh = new GemOgHent(); varer = (ArrayList<Vare>) goh.hent("serial.dat"); jList1.setListData( varer.toArray() ); } // Rækkefølge skal være den der er opgivet prøv fx at flytte OptionalDataException nederst catch( Exception fejl) { jTextFieldFejl.setText("Primitive datatyper blev fundet i strømmen i stedet for objekter " + fejl.toString() ); } } private void jButtonGemActionPerformed(java.awt.event.ActionEvent evt) { try { GemOgHent goh = new GemOgHent(); goh.gem(varer,"serial.dat"); } catch( Exception fejl) // writeObject smider fx IOException. Generel valgt for eksemplets skyld { jTextFieldFejl.setText("Fejl ved skrivning til fil " + fejl.toString() ); } } private void jButtonTilføjActionPerformed(java.awt.event.ActionEvent evt) { try { Vare vare = new Vare(); vare.setVarenummer( Integer.parseInt( jTextFieldVarenummer.getText() ) ); vare.setVarenavn( jTextFieldVarenavn.getText()); vare.setVarebeskrivelse( jTextFieldVarebeskrivelse.getText()); vare.setAntalpaalager( Integer.parseInt( jTextFieldAntalpaalager.getText() ) ); vare.setSalgspris( Integer.parseInt( jTextFieldSalgspris.getText() ) ); vare.setLokation( jTextFieldLokation.getText()); varer.add(vare); jList1.setListData( varer.toArray() ); } catch (Exception e) { jTextFieldFejl.setText("Udfyld alle felter inden tilføj."); } } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new JFrameVarelager().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButtonAntalialt; private javax.swing.JButton jButtonFind; private javax.swing.JButton jButtonGem; private javax.swing.JButton jButtonHent; private javax.swing.JButton jButtonSlet; private javax.swing.JButton jButtonTilføj; private javax.swing.JList jList1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JSeparator jSeparator3; private javax.swing.JSeparator jSeparator4; private javax.swing.JTextField jTextFieldAntalpaalager; private javax.swing.JTextField jTextFieldFejl; private javax.swing.JTextField jTextFieldLokation; private javax.swing.JTextField jTextFieldSalgspris; private javax.swing.JTextField jTextFieldVarebeskrivelse; private javax.swing.JTextField jTextFieldVarenavn; private javax.swing.JTextField jTextFieldVarenummer; private javax.swing.JTextField jTextVareialt; private javax.swing.JLabel jlAntalpaalager; private javax.swing.JLabel jlBesked; private javax.swing.JLabel jlBeskrivelse; private javax.swing.JLabel jlLagerliste; private javax.swing.JLabel jlLagerstyring; private javax.swing.JLabel jlLokation; private javax.swing.JLabel jlSalgspris; private javax.swing.JLabel jlVarenavn; private javax.swing.JLabel ljVarenummer; // End of variables declaration }
05. januar 2008 - 21:34
#6
Ingen svar
Kurser inden for grundlæggende programmering