Hi Leute,
Ich habe folgendes Problem undzwar.
Wenn ich in NetBeans meine GUI also mein JPanel in das JFrame ziehen will hängt sich Netbeans auf. Bei meinem Klassenkamerad hat sich NetBeans auch aufgehängt als wir es auf seinem Pc versuchten. Da ich noch nicht so lange programmier und keine Ahnung hab an was das liegen könnte bitte ich euch um Hilfe.
Hier mal der Code
Java-Code:
JFrameCode:
JPanel Code
Ich habe folgendes Problem undzwar.
Wenn ich in NetBeans meine GUI also mein JPanel in das JFrame ziehen will hängt sich Netbeans auf. Bei meinem Klassenkamerad hat sich NetBeans auch aufgehängt als wir es auf seinem Pc versuchten. Da ich noch nicht so lange programmier und keine Ahnung hab an was das liegen könnte bitte ich euch um Hilfe.
Hier mal der Code
Java-Code:
JFrameCode:
Java:
package GUI;
/**
*
* @author Simon
*/
public class Oberflaeche extends javax.swing.JFrame {
/**
* Creates new form Oberflaeche
*/
public Oberflaeche() {
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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @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 [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Javaâ„¢ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url]
*/
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(Oberflaeche.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Oberflaeche.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Oberflaeche.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Oberflaeche.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 Oberflaeche().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
Java:
package GUI;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/**
*
* @author Simon
*/
public class GitterPanel extends javax.swing.JPanel {
SchiffeFeld sf = new GUI.SchiffeFeld();
/**
* Creates new form GUI
*/
public GitterPanel() {
initComponents();
addMouseListener(new mMouseAdapter());
this.sf.setzeAlleSchiffe();
}
public class mMouseAdapter extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
System.out.println("Maus wurde geklickt");
}
public void mouseEntered(MouseEvent e) {
System.out.println("Maus ist drin");
}
public void mouseExited(MouseEvent e) {
System.out.println("Maus hat Feld verlassen");
}
public void mousePressed(MouseEvent e) {
System.out.println("Maus wurde gepresst");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Maus wurde lossgelassen");
sf.schießen(e.getX()/(getWidth()/10),e.getY()/(getHeight()/10));
repaint();
}
}
public void paintComponent(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.WHITE);
for (int x = 0; x < getWidth(); x += getWidth() / 12) {
g.drawLine(x, 0, x, getHeight());
}
for (int y = 0; y < getHeight(); y += getHeight() / 12) {
g.drawLine(0, y, getWidth(), y);
}
for (int y = 0; y < sf.getGroesseY(); y++) {
for (int x = 0; x < sf.getGroesseX(); x++) {
switch (sf.getElement(x, y)) {
// Schiff 3
// Wasser 1
// Schifftreffer 2
// Wasserschuss/kein Treffer 0
case 0:
g.setColor(Color.RED);
break;
case 1:
g.setColor(Color.BLUE);
break;
case 2:
g.setColor(Color.GREEN);
break;
case 3:
g.setColor(Color.BLACK);
}
g.fillRect(getWidth() / 10 * x, getHeight() / 10 * y, getWidth() / 10, getHeight() / 10);
}
}
}
/**
* 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() {
setForeground(new java.awt.Color(0, 0, 204));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1067, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 671, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
}
Zuletzt bearbeitet von einem Moderator: