• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Jbutton in JPanel

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
{

}
}
}
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our Swing/AWT forum.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Looks like you're putting the boxes too much to the right.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
displayed OK for me (once I stretched the panel wider)
reply
    Bookmark Topic Watch Topic
  • New Topic