D
Daini
Gast
Hallo,
ich versuche eine Ampel zu erstellen. Bin auch soweit fertig, bekomme jetzt beim Compilieren aber ein Fehler und weiß leider nicht wie ich diesen beheben kann.
Ich hoffe das hier welche sind die mir helfen können...
Danke schon mal im vorraus...
Hier der Quelltext
[JAVA=42]
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author
*/
public class GuiAmpel extends javax.swing.JFrame
{
/**
* Creates new form GuiAmpel
*/
public GuiAmpel()
{
initComponents();
rot.setBackground(Color.gray);
gelb.setBackground(Color.gray);
gruen.setBackground(Color.gray);
}
boolean startAmpel=false;
int zustand=0;
/**
* 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() {
startButton = new javax.swing.JButton();
stopButton = new javax.swing.JButton();
rot = new javax.swing.JPanel();
gelb = new javax.swing.JPanel();
gruen = new javax.swing.JPanel();
setBackground(new java.awt.Color(51, 51, 51));
setForeground(new java.awt.Color(51, 51, 51));
startButton.setText("Start");
startButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
startButtonMouseClicked(evt);
}
});
stopButton.setText("Stop");
stopButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
stopButtonMouseClicked(evt);
}
});
rot.setBackground(new java.awt.Color(153, 153, 153));
javax.swing.GroupLayout rotLayout = new javax.swing.GroupLayout(rot);
rot.setLayout(rotLayout);
rotLayout.setHorizontalGroup(
rotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
rotLayout.setVerticalGroup(
rotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 45, Short.MAX_VALUE)
);
gelb.setBackground(new java.awt.Color(153, 153, 153));
javax.swing.GroupLayout gelbLayout = new javax.swing.GroupLayout(gelb);
gelb.setLayout(gelbLayout);
gelbLayout.setHorizontalGroup(
gelbLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
gelbLayout.setVerticalGroup(
gelbLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 44, Short.MAX_VALUE)
);
gruen.setBackground(new java.awt.Color(153, 153, 153));
javax.swing.GroupLayout gruenLayout = new javax.swing.GroupLayout(gruen);
gruen.setLayout(gruenLayout);
gruenLayout.setHorizontalGroup(
gruenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 46, Short.MAX_VALUE)
);
gruenLayout.setVerticalGroup(
gruenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 43, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.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()
//.addContainerGap()
.addComponent(startButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(stopButton))
.addGroup(layout.createSequentialGroup()
.addGap(49, 49, 49)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(gruen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(rot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(gelb, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(rot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(gelb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(gruen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(startButton)
.addComponent(stopButton))
.addContainerGap())
);
}// </editor-fold>
private void startButtonMouseClicked(java.awt.event.MouseEvent evt) {
startAmpel=true;
rot.setBackground(Color.red);
while (startAmpel)
{
switch (zustand)
{
case 0:
{
try
{
Thread.currentThread().sleep(2000);
}
catch (InterruptedException ex)
{
Logger.getLogger(GuiAmpel.class.getName()).log(Level.SEVERE, null, ex);
}
gelb.setBackground(Color.yellow);
zustand = 1;
}
break;
case 1:
{
try
{
Thread.currentThread().sleep(3000);
}
catch (InterruptedException ex)
{
Logger.getLogger(GuiAmpel.class.getName()).log(Level.SEVERE, null, ex);
}
gruen.setBackground(Color.green);
gelb.setBackground(Color.gray);
rot.setBackground(Color.gray);
zustand = 2;
}
break;
case 2:
{
try
{
Thread.currentThread().sleep(2000);
}
catch (InterruptedException ex)
{
Logger.getLogger(GuiAmpel.class.getName()).log(Level.SEVERE, null, ex);
}
gruen.setBackground(Color.gray);
gelb.setBackground(Color.yellow);
rot.setBackground(Color.gray);
zustand = 3;
}
break;
case 3:
{
try
{
Thread.currentThread().sleep(3000);
}
catch (InterruptedException ex)
{
Logger.getLogger(GuiAmpel.class.getName()).log(Level.SEVERE, null, ex);
}
gruen.setBackground(Color.gray);
gelb.setBackground(Color.gray);
rot.setBackground(Color.red);
zustand = 0;
}
break;
}
}
}
private void stopButtonMouseClicked(java.awt.event.MouseEvent evt) {
startAmpel=false;
}
// Variables declaration - do not modify
private javax.swing.JPanel gelb;
private javax.swing.JPanel gruen;
private javax.swing.JPanel rot;
private javax.swing.JButton startButton;
private javax.swing.JButton stopButton;
// End of variables declaration
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new GuiAmpel().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
[/code]
und hier die Fehlermeldung:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: GroupLayout can only be used with one Container at a time
at javax.swing.GroupLayout.checkParent(GroupLayout.java:1116)
at javax.swing.GroupLayout.invalidateLayout(GroupLayout.java:1008)
at java.awt.Container.invalidate(Container.java:1578)
at java.awt.Component.addNotify(Component.java:6905)
at java.awt.Container.addNotify(Container.java:2758)
at javax.swing.JComponent.addNotify(JComponent.java:4743)
at java.awt.Container.addNotify(Container.java:2769)
at javax.swing.JComponent.addNotify(JComponent.java:4743)
at java.awt.Container.addNotify(Container.java:2769)
at javax.swing.JComponent.addNotify(JComponent.java:4743)
at javax.swing.JRootPane.addNotify(JRootPane.java:756)
at java.awt.Container.addNotify(Container.java:2769)
at java.awt.Window.addNotify(Window.java:770)
at java.awt.Frame.addNotify(Frame.java:487)
at java.awt.Window.show(Window.java:1031)
ich versuche eine Ampel zu erstellen. Bin auch soweit fertig, bekomme jetzt beim Compilieren aber ein Fehler und weiß leider nicht wie ich diesen beheben kann.
Ich hoffe das hier welche sind die mir helfen können...
Danke schon mal im vorraus...
Hier der Quelltext
[JAVA=42]
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author
*/
public class GuiAmpel extends javax.swing.JFrame
{
/**
* Creates new form GuiAmpel
*/
public GuiAmpel()
{
initComponents();
rot.setBackground(Color.gray);
gelb.setBackground(Color.gray);
gruen.setBackground(Color.gray);
}
boolean startAmpel=false;
int zustand=0;
/**
* 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() {
startButton = new javax.swing.JButton();
stopButton = new javax.swing.JButton();
rot = new javax.swing.JPanel();
gelb = new javax.swing.JPanel();
gruen = new javax.swing.JPanel();
setBackground(new java.awt.Color(51, 51, 51));
setForeground(new java.awt.Color(51, 51, 51));
startButton.setText("Start");
startButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
startButtonMouseClicked(evt);
}
});
stopButton.setText("Stop");
stopButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
stopButtonMouseClicked(evt);
}
});
rot.setBackground(new java.awt.Color(153, 153, 153));
javax.swing.GroupLayout rotLayout = new javax.swing.GroupLayout(rot);
rot.setLayout(rotLayout);
rotLayout.setHorizontalGroup(
rotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
rotLayout.setVerticalGroup(
rotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 45, Short.MAX_VALUE)
);
gelb.setBackground(new java.awt.Color(153, 153, 153));
javax.swing.GroupLayout gelbLayout = new javax.swing.GroupLayout(gelb);
gelb.setLayout(gelbLayout);
gelbLayout.setHorizontalGroup(
gelbLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
gelbLayout.setVerticalGroup(
gelbLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 44, Short.MAX_VALUE)
);
gruen.setBackground(new java.awt.Color(153, 153, 153));
javax.swing.GroupLayout gruenLayout = new javax.swing.GroupLayout(gruen);
gruen.setLayout(gruenLayout);
gruenLayout.setHorizontalGroup(
gruenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 46, Short.MAX_VALUE)
);
gruenLayout.setVerticalGroup(
gruenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 43, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.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()
//.addContainerGap()
.addComponent(startButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(stopButton))
.addGroup(layout.createSequentialGroup()
.addGap(49, 49, 49)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(gruen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(rot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(gelb, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(rot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(gelb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(gruen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(startButton)
.addComponent(stopButton))
.addContainerGap())
);
}// </editor-fold>
private void startButtonMouseClicked(java.awt.event.MouseEvent evt) {
startAmpel=true;
rot.setBackground(Color.red);
while (startAmpel)
{
switch (zustand)
{
case 0:
{
try
{
Thread.currentThread().sleep(2000);
}
catch (InterruptedException ex)
{
Logger.getLogger(GuiAmpel.class.getName()).log(Level.SEVERE, null, ex);
}
gelb.setBackground(Color.yellow);
zustand = 1;
}
break;
case 1:
{
try
{
Thread.currentThread().sleep(3000);
}
catch (InterruptedException ex)
{
Logger.getLogger(GuiAmpel.class.getName()).log(Level.SEVERE, null, ex);
}
gruen.setBackground(Color.green);
gelb.setBackground(Color.gray);
rot.setBackground(Color.gray);
zustand = 2;
}
break;
case 2:
{
try
{
Thread.currentThread().sleep(2000);
}
catch (InterruptedException ex)
{
Logger.getLogger(GuiAmpel.class.getName()).log(Level.SEVERE, null, ex);
}
gruen.setBackground(Color.gray);
gelb.setBackground(Color.yellow);
rot.setBackground(Color.gray);
zustand = 3;
}
break;
case 3:
{
try
{
Thread.currentThread().sleep(3000);
}
catch (InterruptedException ex)
{
Logger.getLogger(GuiAmpel.class.getName()).log(Level.SEVERE, null, ex);
}
gruen.setBackground(Color.gray);
gelb.setBackground(Color.gray);
rot.setBackground(Color.red);
zustand = 0;
}
break;
}
}
}
private void stopButtonMouseClicked(java.awt.event.MouseEvent evt) {
startAmpel=false;
}
// Variables declaration - do not modify
private javax.swing.JPanel gelb;
private javax.swing.JPanel gruen;
private javax.swing.JPanel rot;
private javax.swing.JButton startButton;
private javax.swing.JButton stopButton;
// End of variables declaration
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new GuiAmpel().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
[/code]
und hier die Fehlermeldung:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: GroupLayout can only be used with one Container at a time
at javax.swing.GroupLayout.checkParent(GroupLayout.java:1116)
at javax.swing.GroupLayout.invalidateLayout(GroupLayout.java:1008)
at java.awt.Container.invalidate(Container.java:1578)
at java.awt.Component.addNotify(Component.java:6905)
at java.awt.Container.addNotify(Container.java:2758)
at javax.swing.JComponent.addNotify(JComponent.java:4743)
at java.awt.Container.addNotify(Container.java:2769)
at javax.swing.JComponent.addNotify(JComponent.java:4743)
at java.awt.Container.addNotify(Container.java:2769)
at javax.swing.JComponent.addNotify(JComponent.java:4743)
at javax.swing.JRootPane.addNotify(JRootPane.java:756)
at java.awt.Container.addNotify(Container.java:2769)
at java.awt.Window.addNotify(Window.java:770)
at java.awt.Frame.addNotify(Frame.java:487)
at java.awt.Window.show(Window.java:1031)
Zuletzt bearbeitet von einem Moderator: