Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Jbutton in JPanel  RSS feed

 
Jay Kamdar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can’t get the Jbutton to display. I am making minesweeper.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class MinePanel extends JPanel
{
private JMenuBar menuBar;
private JButton[][] boxes;
public final int boxSize = 20;



public MinePanel()
{
initialize(10,10,10);
}

public MinePanel(int height, int width, int mines)
{
initialize(height, width, mines);
}

private void initialize(int height, int width, int numOfmines)
{
setPreferredSize(new Dimension(25 * width ,25 * height));
setLayout(null);

menuBar = new JMenuBar();
JMenu menuGame = new JMenu(“Game” ;
JMenuItem menuItemNewGame = new JMenuItem(“New Game” ;
JMenuItem menuItemExit = new JMenuItem(“Exit” ;
JMenu menuHelp = new JMenu(“Help” ;
JMenuItem menuItemHelpContents = new JMenuItem(“Help Contents” ;
TimePlayed label = new TimePlayed();
MinesLeft mineLabel = new MinesLeft();

boxes = buttons(10,10);

menuItemExit.addActionListener(new ExitAction());
menuItemNewGame.addActionListener(new ResetListener());

menuGame.add(menuItemNewGame);
menuGame.add(menuItemExit);
menuHelp.add(menuItemHelpContents);

menuBar.add(menuGame);
menuBar.add(menuHelp);

menuBar.setBounds(0,0,(int) getPreferredSize().getWidth(),(int) getPreferredSize().getHeight() / 20);

label.setBounds((int) getPreferredSize().getWidth() * 3 / 40,(int) getPreferredSize().getHeight() * 5 / 80,
(int) getPreferredSize().getWidth() / 4,(int) getPreferredSize().getHeight() * 3 / 40);

mineLabel.setBounds((int) getPreferredSize().getWidth() * 27 / 40,(int) getPreferredSize().getHeight() * 5 / 80,
(int) getPreferredSize().getWidth() / 4,(int) getPreferredSize().getHeight() * 3 / 40);

add(label);
add(mineLabel);
add(menuBar);
}

private JButton[][] buttons(int row, int col)
{
int height = (int) getPreferredSize().getHeight() * 7 / 40;
int width = (int) getPreferredSize().getWidth();

boxes = new JButton[row][col];

for(int i = 0; i < boxes.length; i++)
{
for(int j = 0; j < boxes[0].length; j++)
{
boxes[i][j] = new JButton();
boxes[i][j].setBounds(width + boxSize * j,height + boxSize * i ,boxSize, boxSize);
boxes[i][j].setEnabled(true);
boxes[i][j].setBorder(new LineBorder(Color.blue, 1));
add(boxes[i][j]);
}
}
return boxes;
}

private class ExitAction extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}

private class ResetListener extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{

}
}
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our Swing/AWT forum.
 
Matti Poro
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Looks like you're putting the boxes too much to the right.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
displayed OK for me (once I stretched the panel wider)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!