Reputation: 35
I'd like to ask to put a JMenubar into this Bordlerlayout here. It generates five circles at random buttons when I start it and i wanted to add a menu with an actionlistener to place the circles randomly again. So i'd like to ask if its possible this way or are there any easier ways ? And does anyone know why sometimes there are just 4 circles instead of five when I start it ?
package test2;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class action extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
int size = 5;
int[] randompoints = GetRandomPoints(size);
action gt = new action(size, randompoints);
gt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gt.pack();
gt.setVisible(true);
}
private static int[] GetRandomPoints(int size) {
int[] result = new int[size];
for (int i = 0; i < size; i++) {
int helper = (int) (Math.random() * (size * size) + 1);
for (int x = 0; x <= i; x++) {
if (helper == result[x]) {
i--;
}
}
result[i] = helper;
}
return result;
}
public action(int size, int[] randompoints) {
Container pane = getContentPane();
pane.setLayout(new GridLayout(size, size));
for (int i = 0; i < size * size; i++) {
JButton button = new JButton();
button.setBackground(Color.WHITE);
button.setName(Integer.toString(i));
button.setPreferredSize(new Dimension(80, 80));
for (int x = 0; x < randompoints.length; x++) {
if (i == randompoints[x]) {
button.setText("O");
}
}
pane.add(button);
}
}
}enter code here
Upvotes: 1
Views: 329
Reputation: 127
A menu bar is something you put in the JFrame
or JDialog
, not in the layout. Is something for the window, not for the inside of the window. You can use a popup menu, but probably is better to use a button or something like that.
I recomend you to read the tutorial: How to Use Menus.
Upvotes: 5