Hallo ich versuche ein kleines Programm zu Programmieren, das wie Mastermind funktionieren soll.
Dazu habe ich 4 JButtons in ein Panel gesetzt, und per ActionListener dazu gebracht durch einen klick die Farbe zu wechseln.
Soweit so gut.....
Mein Wunsch ist, dass nach dem ich ok klicke, dieses Panel unabhängig vom ersten erneut erzeugt wird und dem Frame hinzu gefügt wird!
ich hab schon versucht, die add(panel) per als Methode zu programmieren und aufzurufen, nur da spielt der ActionListener nicht mit.
ja ich weiß dass geht alles noch schöner.....
Dazu habe ich 4 JButtons in ein Panel gesetzt, und per ActionListener dazu gebracht durch einen klick die Farbe zu wechseln.
Soweit so gut.....
Mein Wunsch ist, dass nach dem ich ok klicke, dieses Panel unabhängig vom ersten erneut erzeugt wird und dem Frame hinzu gefügt wird!
ich hab schon versucht, die add(panel) per als Methode zu programmieren und aufzurufen, nur da spielt der ActionListener nicht mit.
Java:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/** mainClass */
//{{{
class run
{
public static void main (String[] args)
{
System.out.println("starting...");
JFrame board = new board("Code Board");
board.setBounds(550, 250, 350, 550);
board.setVisible(true);
board.setResizable( false );
}
}
//}}}
/** boardFrame */
//{{{
class board extends JFrame implements ActionListener
{
int index1 = 1;
int index2 = 1;
int index3 = 1;
int index4 = 1;
JPanel row1 = new JPanel();
JButton f1 = new JButton("");
JButton f2 = new JButton("");
JButton f3 = new JButton("");
JButton f4 = new JButton("");
JButton ok = new JButton("ok");
public board(String titel)
{
super(titel);
System.out.println("creating board...");
setLayout( new FlowLayout() );
f1.addActionListener(this);
f2.addActionListener(this);
f3.addActionListener(this);
f4.addActionListener(this);
f1.setActionCommand("f1");
f2.setActionCommand("f2");
f3.setActionCommand("f3");
f4.setActionCommand("f4");
f1.setOpaque(true);
f2.setOpaque(true);
f3.setOpaque(true);
f4.setOpaque(true);
f1.setBackground(Color.red);
f2.setBackground(Color.red);
f3.setBackground(Color.red);
f4.setBackground(Color.red);
row1.add(f1);
row1.add(f2);
row1.add(f3);
row1.add(f4);
add(row1);
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
/* ActionListener */
//{{{
public void actionPerformed(ActionEvent evt)
{
System.out.println("getting action[board]...");
Color colors[] = new Color[6];
colors[0] = Color.red;
colors[1] = Color.orange;
colors[2] = Color.green;
colors[3] = Color.blue;
if (evt.getActionCommand().equals("f1"))
{
System.out.println("ctrl AC[f1]...");
if (index1 < 4)
{
f1.setBackground(colors[index1]);
index1 = index1 +1;
repaint();
}
else
{
index1 = 1;
f1.setBackground(colors[0]);
repaint();
}
}
if (evt.getActionCommand().equals("f2"))
{
System.out.println("ctrl AC[f2]...");
if (index2 < 4)
{
f2.setBackground(colors[index2]);
index2 = index2 +1;
repaint();
}
else
{
index2 = 1;
f2.setBackground(colors[0]);
repaint();
}
}
if (evt.getActionCommand().equals("f3"))
{
System.out.println("ctrl AC[f3]...");
if (index3 < 4)
{
f3.setBackground(colors[index3]);
index3 = index3 +1;
repaint();
}
else
{
index3 = 1;
f3.setBackground(colors[0]);
repaint();
}
}
if (evt.getActionCommand().equals("f4"))
{
System.out.println("ctrl AC[f4]...");
if (index4 < 4)
{
f4.setBackground(colors[index4]);
index4 = index4 +1;
repaint();
}
else
{
index4 = 1;
f4.setBackground(colors[0]);
repaint();
}
}
}
//}}}
}
//}}}
ja ich weiß dass geht alles noch schöner.....