View Single Post
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