Cannot align buttons on same 'line'
Hi there. I have coded the game, Deal or No Deal and have encountered a last minute problem. I decided to add an Exit button on the GUI so the 'player' can select the Exit button and the program closes down. However, there's a problem with the alignment of the boxes. The first screen loads, with two buttons and a textfield - the textfield is for entering your name, which must be between 1 and 30 characters or the game will not start upon clicking Start Game button. The exit button is supposed to appear directly next to the Start Game button, but for some reason it appears directly below it. Much to my annoyance. I would also like to have the buttons aligned 'centrally' as they are appearing very slightly to the left of center, which doesn't look very good. I think this part of the code is the offending part.
jButton1.setText("Start Game");
jButton1.addActionListener(this);
jButton1.setActionCommand("start");
boxc.add(jButton1);
jButton2.setText("Quit");
jButton2.addActionListener(this);
jButton2.setActionCommand("quit");
boxc.add(jButton2);
add(boxa);
add(Box.createVerticalGlue());
add(Box.createVerticalStrut(strutsize));
add(boxb);
add(Box.createVerticalStrut(strutsize));
add(boxc);
add(Box.createVerticalGlue());
Can anyone shed some light on the alignment problems? Thank you!
[1407 byte] By [
grantg182a] at [2007-11-26 19:05:38]

# 1
I'm not sure exactly what you're trying to do, but I've found that the best way to control the layout is is to use GridBagLayout. Try it if you're not using it already.
# 2
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
# 3
The problem is that the 'Start Game' and 'Quit' buttons do not appear side-by-side, but the Quit button appears directly below the Start Game button. I want both buttons on the 'same line', and central to the GUI, as at the moment they appear slightly to the left. Any ideas how I can solve this? Thank you
package dealornodeal;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** Class configuring the initial screen panel */
public class Panel1 extends JPanel implements ActionListener {
JButton jButton1;
JButton jButton2;
JLabel jLabel1;
JLabel jLabel2;
JTextField inputfield;
/** Initialise panel */
public Panel1() {
Box boxa=new Box(BoxLayout.Y_AXIS);
Box boxb=new Box(BoxLayout.LINE_AXIS);
Box boxc=new Box(BoxLayout.Y_AXIS);
int strutsize=10;
jLabel1=new JLabel(new ImageIcon("E:/Deal Or No Deal/deal/dealuklogo2.JPG"));
jLabel2 = new JLabel();
inputfield = new JTextField(20);
jButton1 = new JButton();
jButton2 = new JButton();
ImageIcon icon=new ImageIcon("E:/Deal Or No Deal/deal/dealuklogo2.JPG");
jLabel1.setIcon(icon);
jLabel1.setText("");
Sounds theme=new Sounds("E:/Deal Or No Deal/Deal/dondtheme.wav",0);
theme.play();
setLayout(new javax.swing.BoxLayout(this, BoxLayout.Y_AXIS));
boxa.add(jLabel1);
jLabel2.setText("Enter your name");
boxb.add(jLabel2);
boxb.add(Box.createHorizontalGlue());
boxb.add(inputfield);
jButton1.setText("Start Game");
jButton1.addActionListener(this);
jButton1.setActionCommand("start");
boxc.add(jButton1);
jButton2.setText("Quit");
jButton2.addActionListener(this);
jButton2.setActionCommand("quit");
boxc.add(jButton2);
add(boxa);
add(Box.createVerticalGlue());
add(Box.createVerticalStrut(strutsize));
add(boxb);
add(Box.createVerticalStrut(strutsize));
add(boxc);
add(Box.createVerticalGlue());
}
Message was edited by:
grantg182
# 4
//boxc.add(jButton1);
//boxc.add(jButton2);
jButton2.setPreferredSize(jButton1.getPreferredSize());
JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER,50,5));
p.add(jButton1);
p.add(jButton2);
boxc.add(p);
# 5
Hi there. I am unsure of the code you've produced. I presume the commented code is to be taken out of the program, a jPanel is to be declared and the code is to be put into the code? Is that correct?
# 6
> The problem is that the 'Start Game' and 'Quit' buttons do not appear side-by-side
The problem is that you still haven't posted a SSCCE.
> Hi there. I am unsure of the code you've produced.
If you posted a SSCCE then you would have the exact code that was changed and you wouldn't have to guess. Now you know how we feel about guessing when you only post partial code.
# 7
Hello again. I am not sure how to post an 'SSCCE', and as the forum doesn't appear to have an Upload function, I can't upload the code, therefore copying and pasting it is - as far I can see - the only way to post the code. I also don't think it's a great idea uploading my whole projects code for someone else to steal, then make me look like a plagiarist.
# 8
> therefore copying and pasting it is - as far I can see - the only way to post the code.
That correct.
> I also don't think it's a great idea uploading my whole projects code
Neither do we. We don't want to see you entire application. If you read the link you would find out that a SSCCE is minimal code to demonstrate your problem.
Why would anybody want to copy code showing a panel with a few components added to it?
# 9
Hello again. Here is the code for the panel which I am having problems with, I
// Copyright Grant Glendinning 2007
package dealornodeal;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** Class configuring the initial screen panel */
public class Panel1 extends JPanel implements ActionListener {
JButton jButton1;
JButton jButton2;
JLabel jLabel1;
JLabel jLabel2;
JTextField inputfield;
/** Initialise panel */
public Panel1() {
Box boxa=new Box(BoxLayout.Y_AXIS);
Box boxb=new Box(BoxLayout.LINE_AXIS);
Box boxc=new Box(BoxLayout.Y_AXIS);
int strutsize=10;
jLabel1=new JLabel(new ImageIcon("E:/Deal Or No Deal/deal/dealuklogo2.JPG"));
jLabel2 = new JLabel();
inputfield = new JTextField(10);
jButton1 = new JButton();
jButton2 = new JButton();
ImageIcon icon=new ImageIcon("E:/Deal Or No Deal/deal/dealuklogo2.JPG");
jLabel1.setIcon(icon);
jLabel1.setText("");
Sounds theme=new Sounds("E:/Deal Or No Deal/Deal/dondtheme.wav",0);
theme.play();
setLayout(new javax.swing.BoxLayout(this, BoxLayout.Y_AXIS));
boxa.add(jLabel1);
jLabel2.setText("Enter your name");
boxb.add(jLabel2);
boxb.add(Box.createHorizontalGlue());
boxb.add(inputfield);
jButton1.setText("Start Game");
jButton1.addActionListener(this);
jButton1.setActionCommand("start");
boxc.add(jButton1);
jButton2.setText("Quit");
jButton2.addActionListener(this);
jButton2.setActionCommand("quit");
boxc.add(jButton2);
add(boxa);
add(Box.createVerticalGlue());
add(Box.createVerticalStrut(strutsize));
add(boxb);
add(Box.createVerticalStrut(strutsize));
add(boxc);
add(Box.createVerticalGlue());
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("start")){
if(!inputfield.getText().equals("")){
// Player's name length check
if(inputfield.getText().length()>20)
DealFrame.coreinstance.setPlayerName(inputfield.getText().substring(0,20));
else
DealFrame.coreinstance.setPlayerName(inputfield.getText());
DealFrame.instance.setPanel(new Panel2());
// Stops music playing upon clicking Start Game with a valid name entered
Sounds theme=new Sounds("E:/Deal Or No Deal/Deal/dondtheme.wav",0);
theme.stopplay();
}
}
if(e.getActionCommand().equals("quit")){
System.exit(0);
}
}
}
# 10
you've posted code, I've copied it, pasted into a .java file, prepared to look at it for you,
hit compile and this is what I get
C:\JavaTest\Testing.java:33: cannot resolve symbol
symbol : class Sounds
location: class Panel1
Sounds theme=new Sounds("E:/Deal Or No Deal/Deal/dondtheme.wav",0);
^
C:\JavaTest\Testing.java:33: cannot resolve symbol
symbol : class Sounds
location: class Panel1
Sounds theme=new Sounds("E:/Deal Or No Deal/Deal/dondtheme.wav",0);
^
C:\JavaTest\Testing.java:72: package DealFrame does not exist
DealFrame.coreinstance.setPlayerName(inputfield.getText().substring(0,20));
^
C:\JavaTest\Testing.java:74: package DealFrame does not exist
DealFrame.coreinstance.setPlayerName(inputfield.getText());
^
C:\JavaTest\Testing.java:76: cannot resolve symbol
symbol : class Panel2
location: class Panel1
DealFrame.instance.setPanel(new Panel2());
^
C:\JavaTest\Testing.java:76: package DealFrame does not exist
DealFrame.instance.setPanel(new Panel2());
^
C:\JavaTest\Testing.java:80: cannot resolve symbol
symbol : class Sounds
location: class Panel1
Sounds theme=new Sounds("E:/Deal Or No Deal/Deal/dondtheme.wav",0);
^
C:\JavaTest\Testing.java:80: cannot resolve symbol
symbol : class Sounds
location: class Panel1
Sounds theme=new Sounds("E:/Deal Or No Deal/Deal/dondtheme.wav",0);
^
8 errors
Process completed.
do you really expect anyone to wade through this bunch of errors, fix 'em,
then recompile and probably get another bunch of different errors?
(Most people have better things to do with their time)
why is it so hard to post something we can copy/paste/compile/run/see/fix
and you get your problem solved very quickly.
anyway, did you try the changes from reply #4
# 11
Hi there. I can only presume the errors you are receiving are because I have another java file which initialises the sounds. Hence why all the java files are part of 'package dealornodeal;'. As I said before, I didn't understand which parts of the code I had to take out and which parts needed replacing, so I have not yet made the changes as I'm not sure where to put the code you've given. Thank you
There also are no errors given when I compile and run the program, the only problem is the positioning of the jbutton1 and jbutton2.
Message was edited by:
grantg182
# 12
> I'm not sure where to put the code you've given.
replace this section of code
boxc.add(jButton1);
jButton2.setText("Quit");
jButton2.addActionListener(this);
jButton2.setActionCommand("quit");
boxc.add(jButton2);
add(boxa);
with this
//boxc.add(jButton1);
jButton2.setText("Quit");
jButton2.addActionListener(this);
jButton2.setActionCommand("quit");
//boxc.add(jButton2);
jButton2.setPreferredSize(jButton1.getPreferredSize());
JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER,50,5));
p.add(jButton1);
p.add(jButton2);
boxc.add(p);
add(boxa);
# 13
Hi there. I have done as you suggested and it has made the image disappear so now all that is shown is "Enter your name", the textfield and the Start Game and Quit buttons in the correct places, but the image is gone? How can I get the image back while keeping the other components in place, as I am only aware of BoxLayout, and Strut/Glue interfaces. Thank you
Message was edited by:
grantg182
# 14
perhaps the default alignment of the added panel has changed the positioning
of the label with the image.
try adding the indicated line
JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER,50,5));
p.setAlignmentX(0.0f);//<--
# 15
Hello again. I have added the code you advised and it has made the width of the screen wider, the textfield width increased and the two buttons moved along a bit too. Perhaps it is something to do with the fact I am using the ImageIcon method for the picture? Thanks
# 16
Hi there. I wondered if anyone else has any ideas at all as to why the layout managers seem to be conflicting with each other and causing the buttons to align the way I wanted, except the picture has disappeared? Thank you
# 17
No idea what you are talking about.I've been waiting 4 days for a SSCCE.
# 18
If you can tell me EXACTLY what this SSCCE is then I'll consider doing it - I must mention again, I am NOT willing to paste my entire project code so that some miserable ******* can come along and steal my work and get the credit for it. I am here in need of help as every scenario I have tried has failed and I have turned to the 'experts' to see if anyone could lend me a hand in solving the problem. Thank you
Please note, the code for the 'problem' Panel has already been copied onto this site.
# 19
> If you can tell me EXACTLY what this SSCCE is
I've told you twice. Michael has told you once.
> Please note, the code for the 'problem' Panel has already been copied onto this site.
Then write a main() method that creates a JFrame and adds the panel to the frame and you've got yourself a SSCCE. A SSCCE "demonstrates" the problem. That means we can compile and execute your code and see the behaviour your are describing. If we so desire when can then attempt to fix the problem by using our own debugging techniques. Looking at your code is not a demonstration.