Java

Soldato
Joined
17 Mar 2005
Posts
4,042
Location
Home
A little problem i currently have a grid numbered 0 through to 99 and i need to place a jpeg file which will be randomly added to any of the buttons ( the grid is made of 100 buttons ) and on clicking this button i need it to produce a message saying "blahblah".
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class bShip2 extends JFrame implements ActionListener
{

private JPanel Panel, jPanelButtons, jPanelCenter;
private JButton [] lButton = new JButton[100];
private JButton [] rButton = new JButton[100];
private JButton btnStart;
private Graphics g;
private JLabel message;
private boolean miss = false;


public bShip2 ()
{
super ("BattleShips V1.0");



// JOptionPane.showMessageDialog(null,"Welcome"); // Welcoming Message
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new BorderLayout());

message = new JLabel("Battleships Game");
message.setFont(new Font("Copperplate Gothic Bold",Font.BOLD,50));
c.add(message, BorderLayout.NORTH);

jPanelButtons = new JPanel(new GridLayout(10,10));
jPanelButtons.setPreferredSize(new Dimension(250,250));
for(int loop2 = 0; loop2 <100; loop2++)
{
lButton[loop2] = new JButton();
jPanelButtons.add(lButton[loop2]);
}
c.add(jPanelButtons, BorderLayout.EAST);

jPanelCenter = new JPanel(new GridLayout(10,10));
jPanelCenter.setPreferredSize(new Dimension(250,250));
for(int loop2 = 0; loop2 <100; loop2++)
{
rButton[loop2] = new JButton();
jPanelCenter.add(rButton[loop2]);
}
c.add(jPanelCenter, BorderLayout.WEST);
jPanelCenter.setBackground(new Color(255,255,128));

Panel = new JPanel();
Panel.setPreferredSize(new Dimension(50,75));
c.add(Panel, BorderLayout.CENTER);




btnStart = new JButton("Start"); // Button is made
btnStart.addActionListener(this); // Button is given instructions on pressing
c.add(btnStart, BorderLayout.SOUTH);


setSize(600,450);
show();



}

public static void main(String[] args)
{
bShip2 myShip = new bShip2();

}

What i have so far not sure entirely what that code you suggest does because im using arrays to assign numbers to each block and on a random block being clicked on i wish for a message coming up saying youve been hit or something and then from there i will work out how to assign an image instead.
 
Back
Top Bottom