• Post Reply Bookmark Topic Watch Topic
  • New Topic

Disappearing buttons  RSS feed

 
Shawn Zhan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructGrid method constructs a grid of buttons. When first created after pressing the "new game button", the buttons are fine and visibile, but after you click one of the buttons or the frame background all of the buttons dissapear. Here is all of the code. Also, another smaller issue is that the menu bar appears above the top of the frame.

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


public class Javalization
{

private static void Construct()
{
JFrame.setDefaultLookAndFeelDecorated(true);
final JFrame frame = new JFrame("Javalization v1.0");
final JFrame game = new JFrame("Javalization v1.0");

MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();

menuFile.setLabel("File");
menuFileExit.setLabel("Exit");

menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
);

menuFile.add(menuFileExit);
menuBar.add(menuFile);

frame.setMenuBar(menuBar);

JButton butan = new JButton("Exit");
JButton nGame = new JButton("New Game");

Container con = frame.getContentPane();
Container gameCon = game.getContentPane();

frame.setResizable(false);
game.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel emptyLabel = new JLabel("");
emptyLabel.setLayout(null);
emptyLabel.setPreferredSize(new Dimension(512,384));
emptyLabel.setBackground(Color.CYAN);
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);


JLabel gameLabel = new JLabel("");
gameLabel.setLayout(null);
gameLabel.setPreferredSize(new Dimension(512,512));
gameLabel.setBackground(Color.CYAN);
game.getContentPane().add(gameLabel, BorderLayout.CENTER);

frame.pack();
frame.setVisible(true);
game.pack();
game.setVisible(false);

//Image icon = Toolkit.getDefaultToolkit().getImage("Logo.jpg");

//frame.setIconImage(icon);
//game.setIconImage(icon);
nGame.setBounds(80,304,100,50);
butan.setBounds(206,304,100,50);


butan.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}});

nGame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent x)
{
frame.setVisible(false);
game.setVisible(true);
}
});

con.add(butan);
con.add(nGame);

constructGrid(gameCon,500,500,25,25);


}

private static void constructGrid(Container container, int xPane, int yPane, int xSize, int ySize )
{
int xMod = (xPane/xSize);
int yMod = (yPane/ySize);
int row = 0;
int column = 0;
int ConstructedCounter = (xMod*yMod) + 1;
JButton [][] ButtonArray = new JButton[xMod][yMod + 1];

while (ConstructedCounter >= 0)
{
ConstructedCounter --;

JButton current = new JButton();

current.setBounds(xMod * row + 50, yMod * column + 50, xMod, yMod);

current.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent x)
{

}
});

ButtonArray[row][column] = current;
container.add(ButtonArray[row][column]);
column++;

if (column >= xMod)
{
column = 0;
row ++;
}

JButton last = new JButton();
last.setBounds(xMod * row + 50, yMod * column + 50, xMod, yMod);
ButtonArray[row][column] = last;
container.add(ButtonArray[row][column]);

}

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
Construct();
}
});
}
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short answer is that if you're going to use setBounds() to specify the size and location of your components, then you must set the layout manager of the container to null, or the LayoutManager will move your components around and resize them.

The longer answer is that, despite what you may be used to in Visual Basic, or despite how other GUI frameworks work, in Java, you don't set the size and location of components, but rather, you set up their relationships and let a LayoutManager satisfy them. Have a look at this part of Sun's Java tutorial.

I'm moving this to our Swing/AWT forum.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!