Auf Thema antworten

Ohps..in dem Fall hast du natürlich recht ;)


Aber bei meinen Listener übergebe ich eigentlich alles im Konstruktor. Vielleicht der Vollständigkeit halber nochmal die Klasse GUI:


[code=Java]import javax.swing.ButtonGroup;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JRadioButton;

import javax.swing.JScrollPane;

import javax.swing.JTextField;

import javax.swing.WindowConstants;


import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.FlowLayout;

import java.awt.Toolkit;

import java.awt.Dimension;

import java.util.ArrayList;

import java.util.List;


/**

 *

 * @author florian

 *

 * class which creates the frame with all buttons, textfields and panels

 *

 */


public class GUI{

   

    static final int Hgap = 30;

    static final int textFieldSize = 30;

    final Dimension frameDim;

    final JFrame frame;

   

    private static BoxPanel boxPanel;

    private static GridPanel gridPanel;

   

    private static BoxListener boxListener;

    private static GridListener gridListener;

   

    private Container contentContainer;

    private JTextField textField;

    private JButton    checkButton;

    private JButton exitButton;

   

    private JRadioButton four;

    private JRadioButton eight;

    private JRadioButton sixteen;

    private JRadioButton twenty4;

    private JRadioButton thirty2;

    private JRadioButton fourty8;

   

   

    public GUI(){

       

        Dimension dim = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());

        dim.setSize(dim.getWidth(), dim.getHeight() - 75);

        frameDim = dim;

       

        this.frame = new JFrame();


    }

   

    public static void main(String [] args){

   

        GUI gui = new GUI();

       

        GUI.boxPanel = new BoxPanel(gui);

        GUI.gridPanel = new GridPanel(gui);

       

        boxListener = new BoxListener(gui);

        gridListener = new GridListener(gui);

       

        gui.initializeGridPanel(gridPanel);

        gui.initializeBoxPanel(GUI.boxPanel);

        gui.createInputPanel();

        gui.paintFrame(GUI.boxPanel,GUI.gridPanel);

       

    }

   

    /*

     * method to set the three inital starBoxes

     */

   

    private void initializeGridPanel(GridPanel gridPanel){

       

       

        gridPanel.setStarBox(gridPanel.getFourBitWidth()/2, gridPanel.getStandardHeight(), 45, 140);

        gridPanel.setStarBox(gridPanel.getFourBitWidth()/2, gridPanel.getStandardHeight(), 45, 140+gridPanel.getStandardHeight()*2+10);

        gridPanel.setStarBox(gridPanel.getFourBitWidth()/2, gridPanel.getStandardHeight(), 45, 140+gridPanel.getStandardHeight()+5);

        gridPanel.addMouseListener(gridListener);

       

    }


    /*

     * method to set the size of the BoxPanel and to fill it

     */

   

    private void initializeBoxPanel(BoxPanel boxPanel){

       

        boxPanel.setPreferredSize(new Dimension((int)frameDim.getWidth(), (int)frameDim.getHeight()/8));

        boxPanel.fillBoxPanel();

        boxPanel.addMouseListener(boxListener);

       

    }

   

    /*

     * method to fill the contentContainer and initialize all buttons

     */

   

    private void createInputPanel(){

       

        FlowLayout flow = new FlowLayout();

        flow.setAlignment(FlowLayout.CENTER);

        flow.setHgap(GUI.Hgap);

       

        this.contentContainer = new Container(); // contains all buttons and the textfield

        this.contentContainer.setLayout(flow);

       

        this.textField = new JTextField();

        this.textField.setColumns(textFieldSize);

        this.textField.setText("INPUT");

        this.textField.setEditable(false);

               

//        initialize the checkButton

       

        this.checkButton = new JButton("CHECK");

        this.checkButton.setEnabled(false);

        this.checkButton.setName("check");

       

//        initialize the exitButton

       

        this.exitButton = new JButton("EXIT");

        this.exitButton.setEnabled(true);

        this.exitButton.setName("exit");

       

//        adding the ActionListener and KeyListener to each button

       

        this.exitButton.addActionListener(new ExitListener());

        this.exitButton.addKeyListener(new MyExitKeyListener());

        this.checkButton.addActionListener(new CheckListener());

        this.checkButton.addKeyListener(new MyCheckKeyListener());

       

//        initialize the radioButtons

       

        this.four = new JRadioButton("4");

        this.eight = new JRadioButton("8");

        this.sixteen = new JRadioButton("16");

        this.twenty4 = new JRadioButton("24");

        this.thirty2 = new JRadioButton("32");

        this.fourty8 = new JRadioButton("48");

       

        ButtonGroup radioGroup = new ButtonGroup();

        radioGroup.add(four);

        radioGroup.add(eight);

        radioGroup.add(sixteen);

        radioGroup.add(twenty4);

        radioGroup.add(thirty2);

        radioGroup.add(fourty8);

       

        this.four.setEnabled(false);

        this.eight.setEnabled(false);

        this.sixteen.setEnabled(false);

        this.twenty4.setEnabled(false);

        this.thirty2.setEnabled(false);

        this.fourty8.setEnabled(false);

       

//        fill the contentContainer

       

        contentContainer.add(textField);

        contentContainer.add(four);

        contentContainer.add(eight);

        contentContainer.add(sixteen);

        contentContainer.add(twenty4);

        contentContainer.add(thirty2);

        contentContainer.add(fourty8);

        contentContainer.add(checkButton);

        contentContainer.add(exitButton);

       

//        contentContainer.setSize(new Dimension((int) frameDim.getWidth(), (int) frameDim.getHeight()/10));

    }

   

    /**

     * method to paint the frame

     */

   

    private void paintFrame(BoxPanel boxPanel, JPanel gridPanel){

        

        BorderLayout border = new BorderLayout();

        JScrollPane scrollbar = new JScrollPane(gridPanel);

        scrollbar.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

       

        frame.setLayout(border);

       

        frame.add(boxPanel, BorderLayout.NORTH);

        frame.add(scrollbar, BorderLayout.CENTER);

        frame.add(contentContainer, BorderLayout.SOUTH);

       

       

        frame.setTitle("Echo Request Puzzle");

        frame.setSize(frameDim);       

       

       

        frame.setVisible(true);

        frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

       

        frame.addWindowListener(new MyWindowListener());

    }

   

    public Color getFrameColor(){

        return frame.getContentPane().getBackground();

    }

   

    public List<JRadioButton> getRadioButtons(){

       

        ArrayList<JRadioButton> list = new ArrayList<JRadioButton>();

        list.add(four);

        list.add(eight);

        list.add(sixteen);

        list.add(twenty4);

        list.add(thirty2);

        list.add(fourty8);

       

        return list;

    }

   

    public List<JButton> getButtons(){

        ArrayList<JButton> list = new ArrayList<JButton>();

       

        list.add(checkButton);

        list.add(exitButton);

       

        return list;

    }

   

    public BoxPanel getBoxPanel(){

        return GUI.boxPanel;

    }

   

    public GridPanel getGridPanel(){

        return GUI.gridPanel;

    }

   

    public JTextField getInputField(){

        return this.textField;

    }

   

    public BoxListener getBoxListener(){

        return GUI.boxListener;

    }

   

    public GridListener getGridListener(){

        return GUI.gridListener;

    }

}[/code]


In dieser Klasse wird das Fenster erstellt. Jeder Listener bekommt im Konstruktor dann das gleiche GUI Element übergeben.

Um eine Komponente neu zu zeichnen, muss ich doch [code]repaint()[/code] auf die Komponente aufrufen, oder?


EDIT: Besonders wichtig sind hier vielleicht die Zeilen 63 bis 78 (die main)


noch ein EDIT:


Also nach klicken auf einer Box, ergibt sogar die Abfrage [code]getForeground().equals(Color.YELLOW)[/code] in der Klasse BoxListener (Zeile 57) true...

Also wird das wirklich alles übernommen, nur nicht gezeichnet, oder irre ich mich?



Oben