The logic is done, but my LAYOUT looks Awful!!! help?

You guys have been great in helping me get through my little trivia game project and I believe I have ALL the logical processing in place, but the final and saddest MOST BLARING problem now is this DISGUSTING Layout.

Here is the link:

http://www.flickr.com/photos/7270990@N06/414222370/

(PLEASE DONT LAUGH TOO HARD) -- I have been saving this part till last.

BUT do you guys have any easy layout suggestions? I looked around for awhile and all the layouts seem very complicated to get the look I would like. I just want to be able to have more control and make it look better. I thought of the border layout but couldnt get more than one element in each section.

I would like the top10 list to be straight down -- more space and even alignment between the buttons and questions....blah blah blah.

I'm sure some of you have heard this 1000 times... can you give me some hints?

Message was edited by:

tvance929

[966 byte] By [tvance929a] at [2007-11-26 20:50:51]
# 1
Hi there,Can you send your code, so at least I know how you create your GUI ?
Thunder_Blasta at 2007-7-10 2:15:37 > top of Java-index,Java Essentials,Java Programming...
# 2
Tips: You can use a JPanel with different layout in your master panel. Example: Use JPanel with gridlayout to pack your top 10 list, then add that panel into your master panel.
Icycoola at 2007-7-10 2:15:37 > top of Java-index,Java Essentials,Java Programming...
# 3
Or if you really want to create your GUI with your own location setting, try to use SwingLayout. But yes, it will make you do so many coding to set the position of each component.
Thunder_Blasta at 2007-7-10 2:15:37 > top of Java-index,Java Essentials,Java Programming...
# 4
> SwingLayoutThat's not in the J2SE, is it?
DrLaszloJamfa at 2007-7-10 2:15:37 > top of Java-index,Java Essentials,Java Programming...
# 5
Eeh sorry.What I mean is SpringLayout...Sorry....
Thunder_Blasta at 2007-7-10 2:15:37 > top of Java-index,Java Essentials,Java Programming...
# 6

I would suggest a flexible layout manager like GridBagLayout. I personally use GridBagLayout for most of the complex GUI's i create. The only concern in GridBagLayout is assigning values to the GridBagConstraints which does demand some thinking.

I have rarely used SpringLayout but it seemed to a very flexible layout manger.

Incase you are not going to resize your window then you can use a null layout manger. Using null layout manger is not a very good thing to do and any day i would suggest you using a layout manger. But as you have trouble using a layout manager you can think of using a null layout manager. Incase you would want to use null layout manger set the window not to resize and be very careful with the size of the components and also where you place them in a container.

qUesT_foR_knOwLeDgea at 2007-7-10 2:15:37 > top of Java-index,Java Essentials,Java Programming...
# 7

Spring Layout will put the location depending on the other Component's position.

For example :

Between SOUTH side of Component A and NORTH side of Component B = 5

I think this is the best way if you have no plan to resize your window.

Anyway, it's about personal opinion.

Thunder_Blasta at 2007-7-10 2:15:37 > top of Java-index,Java Essentials,Java Programming...
# 8

Thanks all! I just read your posts... I'm putting the first part of my code but PLEASE realize, this is my first big (for me) program -- I'm a newbie, I know it is a little scattered and probably not commented like it should be... so grace and mercy would be appreciated, but so would some coding advice! NOT TOO MENTION my layout issue problem.

=======================

import java.text.*; //for formatting

import javax.swing.*; //imports swing classes

import java.awt.event.*; //imports event listeners

import java.awt.Container; // Container methods

import java.awt.*; //layout manager

import java.io.*; // File readers

import java.util.*; // Tokenizer

import java.util.Arrays;

public class TriviaGame extends JFrame {

private JFrame mainFrame; //main container variable

private JButton answerButton; // Calculate button variable -

private JButton exitButton; // Exit button variable

private JTextField answerField; // Length text field variable

private JTextField scoreField; // Score field variable

private JTextField qmissedField; // Questions missed field

private JLabel[] toptenLabel; // Top Ten Labels (array so we can print them all as seperate labels)

private JLabel questionLabel; // The Questions label

private JLabel qmissedLabel; //Questions Missed label

private JLabel scoreLabel; //Score label

private JLabel answerLabel;

private JLabel totalPointLabel, answerReport;

private JLabel TopTenScores, top1, top2, top3, top4, top5, top6, top7, top8, top9, top10; //Top Ten Scores Labels

private String questionsFile = "questions.txt";

private String toptenFile = "topten.txt";

private String inLine, newScore, playerName;

private StringTokenizer st;

private int i, e, score, questionsMissed, randomQuestion;

private int[] qpointsInt, topScoresInt;

private double random;

private String[] answer, fileCustomer, question, qpoints, questionAsked, topScores, topNames, topTenScores;

/** Creates a new instance of TriviaGame */

public TriviaGame() {

mainFrame = new JFrame("Todd's Trivia"); //Defining container with label

//Buttons on interface

answerButton = new JButton("Submit"); //Creating Calc button

exitButton = new JButton("Exit"); // Creating Exit Button

qmissedLabel = new JLabel("Questions Missed:"); //Creating width label

scoreLabel = new JLabel("Score:"); //depth label

answerLabel = new JLabel("Answer");

answerReport = new JLabel("");

TopTenScores = new JLabel("TOP TEN SCORES!");

//text fields for labels

answerField = new JTextField(5);

scoreField = new JTextField(5);

qmissedField = new JTextField(5);

//These strings are for the data that will be read in from the external file (customers.txt)

question = new String[10];

answer = new String[10];

qpoints = new String[10];

qpointsInt = new int[10];

questionAsked = new String[10];

topScoresInt = new int[10]; //Int'ing the top ten scores from the file

topScores = new String[10];

topNames = new String[10];

topTenScores = new String[10]; //Concatenated scores and names for using Array.sort() if it will work!

HighScore[] top = new HighScore[10]; //High Score object that will properly sort the scores

questionsMissed = 0; //Questions Missed variable...duh!

//Assign ALL questions asked with the marker of "n" for NO - this variable will tell us if a question has been asked yet or not.

// Need to change this iteration as we add more questions!!!

for (e=0; e<10; e++)

{

questionAsked[e] = "n";

}

i = 0; //The line counter for reading from file

score = 0; //Score!

//TOP TEN SCORES MECHANISM - implementing the HighScore object!

try //the reader stream must have a means for throwing errors if problem with opening stream

{

BufferedReader br = new BufferedReader (new FileReader(toptenFile)); //Opens stream to read data from file

//The following few lines pull the data from the file line by line and create a seperate variable for each piece of data

while ((inLine = br.readLine())!= null)

{

st = new StringTokenizer (inLine);

topScores = st.nextToken();

topNames = st.nextToken();

topScoresInt=Integer.parseInt(topScores);

top = new HighScore(topScoresInt,topNames);

i++;

}

br.close(); //closing the stream

Arrays.sort(top);

i = 0; //Resetting this variable for the next read.

}

catch(IOException e) //Catches an IOExceptions and gives appropriate response.

{

JOptionPane.showMessageDialog(null, "Top Ten File NOT FOUND - exiting program", "File Not Found!",

JOptionPane.INFORMATION_MESSAGE);

}

top1 = new JLabel("1. " + top[9]);

top2 = new JLabel("2. " + top[8]);

top3 = new JLabel("3. " + top[7]);

top4 = new JLabel("4. " + top[6]);

top5 = new JLabel("5. " + top[5]);

top6 = new JLabel("6. " + top[4]);

top7 = new JLabel("7. " + top[3]);

top8 = new JLabel("4. " + top[2]);

top9 = new JLabel("5. " + top[1]);

top10 = new JLabel("6. " + top[0]);

try //the reader stream must have a means for throwing errors if problem with opening stream

{

BufferedReader br = new BufferedReader (new FileReader(questionsFile)); //Opens stream to read data from file

//The following few lines pull the data from the file line by line and create a seperate variable for each piece of data

random = Math.round(Math.random()*4);

randomQuestion = (int)random;

while ((inLine = br.readLine())!= null)

{

if (i == randomQuestion)

{

st = new StringTokenizer (inLine, "^");

question = st.nextToken();

answer = st.nextToken();

qpoints = st.nextToken();

qpointsInt=Integer.parseInt(qpoints);

questionLabel = new JLabel(question); //This creates a SINGLE question that has been chose randomly

totalPointLabel = new JLabel(qpoints); //This is to tell us what question it is.

questionAsked="y";

break;

}

i++;

}

br.close(); //closing the stream

}

catch(IOException e) //Catches an IOExceptions and gives appropriate response.

{

JOptionPane.showMessageDialog(null, "Question File NOT FOUND - exiting program", "File Not Found!",

JOptionPane.INFORMATION_MESSAGE);

}

Container c = mainFrame.getContentPane(); // Get the content pane

c.setLayout(new FlowLayout(FlowLayout.CENTER)); // Setting Layout

//adding all components to layout in order of appearence

c.add(questionLabel);

c.add(answerField);

c.add(answerLabel);

c.add(answerButton);

c.add(scoreField);

scoreField.setText("0");

c.add(scoreLabel);

c.add(qmissedLabel);

c.add(qmissedField);

c.add(answerButton);

c.add(exitButton);

c.add(totalPointLabel);

c.add(answerReport);

c.add(TopTenScores);

c.add(top1); c.add(top2); c.add(top3); c.add(top4); c.add(top5);

c.add(top6); c.add(top7); c.add(top8); c.add(top9); c.add(top10);

Message was edited by:

tvance929

tvance929a at 2007-7-10 2:15:37 > top of Java-index,Java Essentials,Java Programming...
# 9
How do I control the Layout of the Master pane if I do what you say?It sounds great but I dont know how to add JPanel's into the master pane and how to align them.
tvance929a at 2007-7-10 2:15:38 > top of Java-index,Java Essentials,Java Programming...
# 10

> How do I control the Layout of the Master pane if I

> do what you say?

>

> It sounds great but I dont know how to add JPanel's

> into the master pane and how to align them.

Sorry, is this question for the SpringLayout or for null layout ?

Anyway, I have suggestion to make your code nicer :

* Combine the creating of variable to be one line

e.g.

String string1;

String string2;

become :

String string1, string2;

I still see some of your Jxxxxx object still created 1 per line. Just to make your code shorter :)

Thunder_Blasta at 2007-7-10 2:15:38 > top of Java-index,Java Essentials,Java Programming...
# 11

Thanks thunder! I listed the CODE because you had asked me too so you could look at it and see how I am setting up the layout -- any suggestions?

THE Next post about the Layout of the Master pane was in response to an earlier post by someone else who suggested that I use Multiple layouts inside the Master pane -- they suggested I use the GridLayout for my top score portion and place that in a seperate JPanel inside of the Master panel ...

THAT sounded like a real good solution but I dont know how to manage seperate JPANELs inside of a Master panel.

tvance929a at 2007-7-10 2:15:38 > top of Java-index,Java Essentials,Java Programming...