My first Question:
Why doesn't the method "controlButtonsEvent" work?
My Code:
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class newGui{
GridLayout g;
JFrame f;
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
static String [][]code=new String [9][9];
static JButton[][] myButtons;
public static void main(String[]args){
newGui myB=new newGui();
myB.draw();
newGui.read();
myB.controlButtonsEvent();
}
public void draw()
{
f=new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1.setPreferredSize(new Dimension(162,20));
p1.setMaximumSize(new Dimension(162,20));
f.getContentPane().add(BorderLayout.NORTH,p1);
p2.setBackground(Color.lightGray);
p2.setPreferredSize(new Dimension(162,45));
p2.setMaximumSize(new Dimension(162, 45));
f.getContentPane().add(BorderLayout.CENTER, p2);
p3.setPreferredSize(new Dimension(162,160));
p3.setMaximumSize(new Dimension(162,160));
f.getContentPane().add(BorderLayout.SOUTH,p3);
p3.setLayout((g=new GridLayout(9,9)));
myButtons=new JButton[9][9];
for(int j=0;j<9;j++)
for(int i=0;i<9;i++)
{
myButtons[j][i]=new JButton();
p3.add(myButtons[j][i]);
}
f.setSize(170,260);
f.setVisible(true);
}
public static void read()//read from a file
{
Scanner s=null;
try{
s=new Scanner (new File("minesweeper.txt"));
for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
code[i][j]=s.next();
}
catch(IOException e){}
}
public void controlButtonsEvent()
{
myButtons[0][0].addActionListener(new Listener00());
//add action listener to other buttons
}
class Listener00 implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(code[0][0]=="b")
myButtons[0][0].setBackground(Color.LIGHT_GRAY);
}
}
//here goes other inner class for other buttons
}
File "minesweeper.txt" is like this:
(b for blank, and * as bombs)
b b b 1 1 2 * 1 b
b 1 1 2 * 2 1 1 b
b 1 * 2 1 1 b b b
1 2 3 2 1 b b b b
1 * 2 * 2 1 b b b
1 1 2 2 * 1 b b b
b b b 1 2 2 1 b b
b 1 2 2 2 * 1 1 1
b 1 * * 2 1 1 1 *

New Topic/Question
Reply




MultiQuote








|