Hallo,
ich hoffe ich bin hier im richtigen Bereich gelandet.
Zunächst einmal das Problem: Ich möchte gern für ein Programm eine kleine Ein- bzw. Ausgabe basteln. Das inner steht soweit, es existiert auch bereits eine Ausgabe aber die ist nicht wirklich schön. Um das zu ändern habe ich mich NetBeans zugewendet. Die einzelnen Teile im Designe hin und her schieben geht auch super, aber sobald es ans Event-handling geht geht das Drama los. Leider fehlt mir jegliche fundierte Ausbildung in Bezug auf Java, so dass ich mir alles irgendwie selbst aneignen muss. Mein ziel ist es nach Eingabe 2er Parameter ein Objekt "TSP" zu erzeugen (Kunden gleich-verteilt in einem 50*50 Raster) und diese anschließend neben der Eingabe auszugeben. Problem wie bekomme ich das Programm dazu, nachdem der Button gedrückt wurde, das Panel mit Leben zu Füllen. Die Klasse Ausgabe funktioniert in anderem Kontext in erweiteter form super aber hier bekomme ich sie einfach nicht in das Panel.Mein Ansatz sieht bisher so aus:
Hauptklasse
Ausgabe:
Vielleicht kann mir jemand einen heißen Tipp geben oder eine Empfehlung abgeben wo ich suchen könnte (Print-Literatur bevorzugt). Oder Stichworte nach denen ich suchen könnte...
ich hoffe ich bin hier im richtigen Bereich gelandet.
Zunächst einmal das Problem: Ich möchte gern für ein Programm eine kleine Ein- bzw. Ausgabe basteln. Das inner steht soweit, es existiert auch bereits eine Ausgabe aber die ist nicht wirklich schön. Um das zu ändern habe ich mich NetBeans zugewendet. Die einzelnen Teile im Designe hin und her schieben geht auch super, aber sobald es ans Event-handling geht geht das Drama los. Leider fehlt mir jegliche fundierte Ausbildung in Bezug auf Java, so dass ich mir alles irgendwie selbst aneignen muss. Mein ziel ist es nach Eingabe 2er Parameter ein Objekt "TSP" zu erzeugen (Kunden gleich-verteilt in einem 50*50 Raster) und diese anschließend neben der Eingabe auszugeben. Problem wie bekomme ich das Programm dazu, nachdem der Button gedrückt wurde, das Panel mit Leben zu Füllen. Die Klasse Ausgabe funktioniert in anderem Kontext in erweiteter form super aber hier bekomme ich sie einfach nicht in das Panel.Mein Ansatz sieht bisher so aus:
Hauptklasse
Java:
package vorarbeit;
import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class test extends javax.swing.JFrame {
public test() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Problemgroesse = new javax.swing.JTextField();
LabelProblemgroesse = new javax.swing.JLabel();
LabelSeed = new javax.swing.JLabel();
Seed = new javax.swing.JTextField();
Create = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Problemgroesse.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ProblemgroesseActionPerformed(evt);
}
});
LabelProblemgroesse.setText("Problemgröße:");
LabelSeed.setText("Seed:");
Seed.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SeedActionPerformed(evt);
}
});
Create.setText("Problem erzeugen");
Create.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CreateActionPerformed(evt);
}
});
jPanel1.addContainerListener(new java.awt.event.ContainerAdapter() {
public void componentAdded(java.awt.event.ContainerEvent evt) {
jPanel1ComponentAdded(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 562, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 423, Short.MAX_VALUE)
);
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, false)
.addGroup(layout.createSequentialGroup()
.addComponent(LabelProblemgroesse)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Problemgroesse))
.addGroup(layout.createSequentialGroup()
.addComponent(LabelSeed, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Seed))
.addComponent(Create, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(146, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(84, 84, 84)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(LabelProblemgroesse)
.addComponent(Problemgroesse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(LabelSeed, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Seed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Create))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(127, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
int probsize;
private void ProblemgroesseActionPerformed(java.awt.event.ActionEvent evt) {
try {
probsize=( Integer.parseInt( Problemgroesse.getText() )) ;
}
catch ( NumberFormatException e ) { }
}
int seed;
private void SeedActionPerformed(java.awt.event.ActionEvent evt) {
try {
seed=( Integer.parseInt( Seed.getText() )) ;
}
catch ( NumberFormatException e ) { }
}
TSP test=new TSP(1,2);
private void CreateActionPerformed(java.awt.event.ActionEvent evt) {
jPanel1ComponentAdded(null);
}
private void jPanel1ComponentAdded(java.awt.event.ContainerEvent evt) {
jPanel1.add(new Ausgabe(test));
}
public static void main(String args[]) {
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(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(test.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 test().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Create;
private javax.swing.JLabel LabelProblemgroesse;
private javax.swing.JLabel LabelSeed;
private javax.swing.JTextField Problemgroesse;
private javax.swing.JTextField Seed;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
Ausgabe:
Java:
package vorarbeit;
import java.awt.Color;
import java.awt.Graphics;
import java.util.List;
import javax.swing.JPanel;
public class Ausgabe extends JPanel {
TSP Instanz;
Ausgabe(TSP Inst){
Instanz=Inst;
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.RED);
for(int i=0;i<Instanz.getKoordX().size();i++){
int x= (int) Instanz.getKoordX().get(i);
int y= (int) Instanz.getKoordY().get(i);
g.fillOval(10 + (x), 10 + (y) ,6,6);
g.drawString(""+i, 10 + (x), 10 + (y));
}
}
}
Vielleicht kann mir jemand einen heißen Tipp geben oder eine Empfehlung abgeben wo ich suchen könnte (Print-Literatur bevorzugt). Oder Stichworte nach denen ich suchen könnte...