• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Disappearing buttons

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
});
}
}
 
author and iconoclast
Posts: 24207
46
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
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic