Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Feb 7th, 2007, 3:13 AM   #1
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Visuals in Swing

I'm making a Java based survey. For some reason the survey isn't looking right when I change the size of the window.

Basically, buttons, labels and radio boxes will vanish when the window isn't the right size. How would I make it so that the items (label, buttons...) inside the Applet window fit the content of the box more snug?

Here's the default size:


While here's how it looks after messing with the applet size (which is what the user can do).



/*
	Name: Eric the Red
	Program: Computer Programmer Analyst
	Lab #1

	Description:
*/
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class Survey extends JFrame
{
	public Survey()
	{
		super("Health Awareness Survey");
		
		//Place first Question
		JLabel lblSleep = new JLabel("1: On average how many hours of sleep do you get per night?");
		JComboBox cmbSleep = new JComboBox();
		cmbSleep.addItem("3-5 hours");
		cmbSleep.addItem("5-6 hours");
		cmbSleep.addItem("6-7 hours");
		cmbSleep.addItem("7-8 hours");
		cmbSleep.addItem("> 9 hours");
		cmbSleep.setEditable(true);
		cmbSleep.setMaximumRowCount(3);
		
		FlowLayout flowSleep = new FlowLayout(FlowLayout.LEFT, 0,0);
		JPanel pnlSleep = new JPanel(flowSleep);
		pnlSleep.add(lblSleep);
		pnlSleep.add(cmbSleep);
		// adds fourth quest
		
		
		// Second Question
		JLabel lblFood = new JLabel ("2. How many items of junk food do you consume per week?");
		JRadioButton opt0Items = new JRadioButton("0-5 Items");
		JRadioButton opt5Items = new JRadioButton("5-10 Items");
		JRadioButton opt10Items = new JRadioButton("10-15 Items");
		// create a button group for the radio buttons
		ButtonGroup grpItems = new ButtonGroup();
		grpItems.add(opt0Items);
		grpItems.add(opt5Items);
		grpItems.add(opt10Items);
		
		FlowLayout flowItems = new FlowLayout(FlowLayout.LEFT, 0,0);
		JPanel pnlItems = new JPanel(flowItems);
		pnlItems.add(lblFood);
		pnlItems.add(opt0Items);
		pnlItems.add(opt5Items);
		pnlItems.add(opt10Items);
				
		//Third Question
		JCheckBox chkHealthy = new JCheckBox("3. Is the majority of food you eat healthy?", true);
		
		//Fourth Question
		JLabel lblFav = new JLabel("What is your favorite food?");
		JTextField txtFav = new JTextField(25);
		txtFav.setToolTipText("Enter your favorite food");
		
		FlowLayout flowFood = new FlowLayout(FlowLayout.LEFT, 0,0);
		JPanel pnlFav = new JPanel(flowFood);
		pnlFav.add(lblFav);
		pnlFav.add(txtFav);
		// adds fourth question to a panel
		JPanel pnlFields = new JPanel(new GridLayout(0, 1));
		pnlFields.add(pnlFav); // question about favorite food
		pnlFields.add(pnlSleep);
		pnlFields.add(pnlItems);
		//Comment Box + Button
		JLabel lblFeedBack = new JLabel("Before you submit form, please take a moment to enter any feedback you'd like to give us");
		
		lblFeedBack.setText("<html><hr>Before you submit form, please take<br> a moment to to enter any feedback you <br> would like to give us</html>");
		lblFeedBack.setSize(10, 4);
		JTextArea txtFeedBack = new JTextArea(4, 30);
		txtFeedBack.setLineWrap(true);
		txtFeedBack.setWrapStyleWord(true);
		txtFeedBack.setToolTipText("Enter your (Questions/Problems) here");
		JScrollPane scrFeedBack = new JScrollPane(txtFeedBack, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, 
			ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		JPanel pnlAddress = new JPanel(new BorderLayout());
		pnlAddress.add(lblFeedBack, BorderLayout.CENTER);

		JButton cmdSubmit = new JButton("Submit");
		JButton cmdReset = new JButton("Reset");
		JButton cmdExit = new JButton("Exit");
		
		cmdSubmit.setMnemonic(KeyEvent.VK_G);// G instead of S, incase people think 'S = save'
		cmdSubmit.setToolTipText("Click here to submit form");
		
		cmdReset.setMnemonic(KeyEvent.VK_R);
		cmdReset.setToolTipText("Click here to reset form");
		
		cmdExit.setMnemonic(KeyEvent.VK_E);
		cmdExit.setToolTipText("Click here to exit the program.");
		
		JPanel pnlButtons = new JPanel(new GridLayout(1,0));
		pnlButtons.add(cmdSubmit);
		pnlButtons.add(cmdReset);
		pnlButtons.add(cmdExit);

		JPanel pnlFeedBack = new JPanel(new BorderLayout());
		pnlFeedBack.add(pnlButtons, BorderLayout.SOUTH);
		pnlFeedBack.add(scrFeedBack, BorderLayout.CENTER);
		pnlFeedBack.add(lblFeedBack, BorderLayout.NORTH);

		Container pane = this.getContentPane();
		pane.setLayout(new BorderLayout());
		pane.add(pnlFeedBack, BorderLayout.CENTER);
		pane.add(pnlFields, BorderLayout.NORTH);
		
		//final configurations
		this.pack();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
}
__________________
Death smiles at us all. All a man can do is smile back.

Last edited by big_k105; Feb 8th, 2007 at 6:52 PM.
Eric the Red is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
SWING stuff... dsbabyGurl Java 6 Oct 27th, 2006 11:45 PM
Java Swing FocusTraversalPolicy hemanth.balaji Java 0 May 12th, 2006 12:25 AM
Swing || AWT Tutorials Java_König Java 2 Jan 29th, 2006 12:19 AM
swing GUI stuff rsnd Java 4 Aug 10th, 2005 5:39 AM
Swing books? Mjordan2nd Java 0 Mar 2nd, 2005 9:50 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:35 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC