I am making a basic soundboard in java and i want it so that the every 2 buttons are on a different line like:
(button) (button)
(button) (button)
This is my code as of now
JPanel p = new JPanel();
JButton one = new JButton(sound1);
JButton two = new JButton(sound2);
JButton three = new JButton(sound3);
JButton four = new JButton(sound4);
JButton five = new JButton(sound5);
p.add(one);
p.add(two);
p.add(three);
p.add(four);
p.add(five);
int n = JOptionPane.showConfirmDialog(null, p, "Test", JOptionPane.OK_CANCEL_OPTION, -1);
What would be the easiest way to do it? If i have to switch to a JFrame let me know i wont mind if that is the only option.
Just use GridLayout in your JPanel:
JPanel p = new JPanel(new GridLayout(0, 2));
First argument is the number of rows, second one is the number of columns. If you specify number of rows as 0 you are telling the layout manager that you just want to have two columns and don't know how many rows you're going to need - the rows will be created dynamically.
You can use the GridLayout, it presents components in a table. like below, 3 is the number of rows, 2 is the number of columns.
p.setLayout(new GridLayout(3, 2));
p.add(one);
p.add(two);
p.add(three);
p.add(four);
p.add(five);
Related
I have a very big problem. I need to do vertical menu in the center of window. What could be easier? What I did:
I create JFrame and set BorderLayout to it:
JFrame jfr = new JFrame("Frame");
Then I create 4 buttons:
JButton b1 = new JButton("b1");
JButton b2 = new JButton("b2");
JButton b3 = new JButton("b3");
JButton b4 = new JButton("b4");
I created panel and add all buttons to panel:
JPanel jpan = new JPanel();
jpan.setLayout(new BoxLayout(jpan, BoxLayout.Y_AXIS));
jpan.add(b1);
jpan.add(b2);
jpan.add(b3);
jpan.add(b4);
Aligned all buttons
b1.setAlignmentX(JComponent.CENTER_ALIGNMENT);
b2.setAlignmentX(JComponent.CENTER_ALIGNMENT);
b3.setAlignmentX(JComponent.CENTER_ALIGNMENT);
b4.setAlignmentX(JComponent.CENTER_ALIGNMENT);
And add panel to JFrame
jfr.add(jpan, BorderLayout.CENTER);
Please help me to understand this layouts!
Only say like this: "You should use this, when you use this layout"
And now main question: How can I change size of buttons?
There are a number of easy ways to change the size of buttons:
Give the buttons an icon of a different size.
Make the font of the buttons a different size.
Set more / less space between the button contents (the icon and text) and the border of the button.
Give the buttons more / less text.
Given the last is quite arbitrary, here is a demonstration of the first 3 techniques:
I am trying to make a 2x2 grid layout that has a JLabel on the top left, and three buttons on the other three spaces. When I do this, I get the unexpected result of one big button (filling up the entire JDialog) that says "Do you want to push me". I don't know why this result shows up, please help, Thanks!
public void sinceyoupressedthecoolbutton() {
JDialog replacementwindow = new JDialog(); //Like a window
JButton best = new JButton("best");
JButton first = new JButton("FIRST");
JButton second = new JButton("Second");
replacementwindow.setLayout(new GridLayout(2,3,0,0)); //Row, column, distance horizontally, distance vertical
JPanel panel = new JPanel();
replacementwindow.add(panel); //adding the JPanel itself
replacementwindow.add(first);
replacementwindow.add(second);
replacementwindow.add(best);
replacementwindow.setSize(500, 500);
replacementwindow.setTitle("NEW WINDOW!");
replacementwindow.setVisible(true);
}
It's because you set the layout of your JButton, and not of your JDialog
Change
label.setLayout(new GridLayout(2,2,0,0));
to
YES.setLayout(new GridLayout(2,2,0,0));
Also, your variable called label is a JButton, you probably want to change that.
Don't add components to a button. You add components to a panel.
So the basic code should be:
JDialog dialog = new JDialog(...);
JPanel panel = new JPanel( new GridLayout(...) );
panel.add(label);
panel.add(button1);
...
dialog.add(panel);
Also, variable names should NOT start with an upper case character! "Yes" does not follow Java standards. The other variables do. Be consistent!
Here I have a simple buildPanel method for the input area.
I try to use GridLayout(7,1), but the first 4 JTextField seem to fill up the entire space of its block, and that looks quite ugly.
Is there any way to change them back to its normal size?
private void buildInputPenal()
{
dateField = new JTextField(10); //dateField should be changed to something else later
fNameField = new JTextField(15);
lNameField = new JTextField(15);
pledgeField = new JTextField(10);
charityRB1 = new JRadioButton(Charity[0]);
charityRB2 = new JRadioButton(Charity[1]);
charityRB3 = new JRadioButton(Charity[2]);
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(charityRB1);
radioButtonGroup.add(charityRB2);
radioButtonGroup.add(charityRB3);
charityRB1.addActionListener(new RadioButtonListener());
charityRB2.addActionListener(new RadioButtonListener());
charityRB3.addActionListener(new RadioButtonListener());
inputPanel = new JPanel(new GridLayout(7,1));
inputPanel.add(dateField);
inputPanel.add(fNameField);
inputPanel.add(lNameField);
inputPanel.add(pledgeField);
inputPanel.add(charityRB1);
inputPanel.add(charityRB2);
inputPanel.add(charityRB3);
}
Don't use GridLayout. GridLayout creates an even set of cells for each component (that is they get the same amount of space).
Instead, consider using something GridBagLayout instead
See How to Use GridLayout and How to Use GridBagLayout for more details
So i'm creating an array of buttons that is supposed to display and 8,8 grid, instead it displays very small buttons spreading across the window (31 buttons in a row for two rows then two more buttons on the third). If I replace:
gamePanel1.add(buttons[a][b]);
with:
frame.add(buttons[a][b]);
... it display correctly but when initialising the array, I have to resize the window to see the buttons as it does not fit to contents.
Here is the code to create the buttons:
contentPane.setLayout(new BorderLayout());
JPanel gamePanel1 = new JPanel();
buttons = new JButton[boardsize][boardsize];
mineBoard = new int[9][9];
for (int a = 0; a < boardsize; a++)
for (int b = 0; b < boardsize; b++) {
buttons[a][b] = new JButton("");
buttons[a][b].setBounds(30+gridsize*a,30+gridsize*b,gridsize,gridsize);
gamePanel1.add(buttons[a][b]);
buttons[a][b].addMouseListener(new MouseListener(a,b));
setx(a);
sety(b);
settried(false);
setmine(false);
}
contentPane.add(gamePanel1, BorderLayout.CENTER);
Can anyone tell me how I might fix this or show me how with this code I may use a different layout - i tried grid layout for the buttons but could not get it working at all.
First create a Panel as:
JPanel panel=new JPanel();
Then set the layout as
panel.setLayout(new GridLayout(8,8));
Then using a for loop create and add the buttons and the buttons will be displayed in eight by eight grid. Thanks.
I'm creating a simple Minesweeper game in Java. Size 9x9.
I create an array of JPanels and an array of buttons; I add each button to its respective JPanel. then i add the JPanels to the JFrame.
How do i distinguish between each button on the action event?
Here's some of my code:
int gridx = 9;
int gridy = 9;
JButton[] buttons = new JButton[gridx*gridy];
JPanel[] jpanels = new JPanel[gridx*gridy];
public Minesweeper(){
super("Minesweeper");
setLayout(new GridLayout(9,9));
JPanel panel = new JPanel();
int i = 0;
for(i = 0; i<gridx*gridy; i++){
jpanels[i] = new JPanel();
buttons[i] = new JButton();
buttons[i].addActionListener(buttonEvent);
jpanels[i].setLayout(new GridLayout(1,1));
jpanels[i].add(buttons[i]);
add(jpanels[i]);
}
//buttons[67].setEnabled(false);
setSize(300,300);
setVisible(true);
}
The only way i can think about doing this is adding text to the button like so:
buttons[i] = new JButton(i);
Then calling getActionCommand() but i dont want text to show up on the button. Any other ideas?
You can use AbstractButton#setActionCommand.
In your loop:
buttons[i].setActionCommand(i+"");
Then you'll get i back when you use getActionCommand
Note I did mention in a comment on another answer that I would create a new class Mine which extends JButton which I believe to be a better and more complete solution. This however gets the job done rather quickly.