Serj Mavis
Posts: 8
Nickname: peb
Registered: Oct, 2005
|
|
Re: Setting Locations
|
Posted: Oct 24, 2005 12:42 AM
|
|
I tried that but it would'nt seem to work. This is my first time i've used Swing, so if any one could help me get my menu bar and texts alligned, It would be appreciated. I want to make the toolbar alligned to the left, and have the "aff flight" jlabel just below that.
(sorry about the sloppy coding)..I hope its not too unreadable.
import java.awt.*; import java.awt.Frame; import java.awt.event.*; import javax.swing.*; import java.io.*; import javax.swing.undo.*; import javax.swing.*; import javax.swing.border.*;
public class TravelBookingSystemFrame extends JFrame { JPanel contentPane; BorderLayout borderLayout = new BorderLayout(); JToolBar toolBar = new JToolBar(); JButton button1 = new JButton(); JButton button2 = new JButton(); JButton button3 = new JButton(); JButton button4 = new JButton(); JButton button5 = new JButton(); JButton button6 = new JButton(); ImageIcon image1; ImageIcon image2; ImageIcon image3; ImageIcon image4; ImageIcon image5; ImageIcon image6; // define the menu related objects JMenuBar jMenuBar = new JMenuBar(); JMenu jMenuFile = new JMenu(); JMenuItem jMenuItemOpen = new JMenuItem(); JMenuItem jMenuItemSave = new JMenuItem(); JMenuItem jMenuItemSaveAs= new JMenuItem(); JMenuItem jMenuItemCopy = new JMenuItem(); JMenuItem jMenuItemExit = new JMenuItem(); JMenu jMenuEdit = new JMenu(); JMenuItem jMenuItemFont = new JMenuItem(); JMenu jMenuView = new JMenu(); JMenu jMenuHelp = new JMenu(); JMenuItem jMenuItemAbout = new JMenuItem(); JFileChooser chooser = new JFileChooser(); JLabel statusLine = new JLabel(); JTextArea textArea; JScrollPane textScroller; String currentFile = ""; /** * Constructor for objects of class TravelBookingSystemFrame * */ public TravelBookingSystemFrame() { contentPane = (JPanel)this.getContentPane(); contentPane.setLayout(borderLayout); this.setSize(new Dimension(800, 500)); this.setTitle("Travel Booking System"); // set up the tool bar buttons try { image1 = new ImageIcon(TravelBookingSystemFrame.class.getResource("addFlight.gif")); button1.setIcon(image1); } catch(Exception e) { button1.setText("Add a Flight"); } try { image2 = new ImageIcon(TravelBookingSystemFrame.class.getResource("addMotel.gif")); button2.setIcon(image2); } catch(Exception e) { button2.setText("Add a Motel"); } try { image3 = new ImageIcon(TravelBookingSystemFrame.class.getResource("addCar.gif")); button3.setIcon(image3); } catch(Exception e) { button3.setText("Add a Car"); } try { image4 = new ImageIcon(TravelBookingSystemFrame.class.getResource("addDayTrip.gif")); button4.setIcon(image4); } catch(Exception e) { button4.setText("Add a DayTrip"); } try { image5 = new ImageIcon(TravelBookingSystemFrame.class.getResource("addResturant.gif")); button5.setIcon(image5); } catch(Exception e) { button5.setText("Add a Resturant"); } try { image6 = new ImageIcon(TravelBookingSystemFrame.class.getResource("epd.gif")); button6.setIcon(image6); } catch(Exception e) { button6.setText("Enter Personal Details"); } // set the tool tips for the tool bar buttons button1.setToolTipText("Adds a flight booking"); button2.setToolTipText("Adds a motel booking"); button3.setToolTipText("Adds a car booking"); button4.setToolTipText("Adds a day trip booking");; button5.setToolTipText("Adds a resturant booking"); button6.setToolTipText("Enter your personal details"); // setup the tool bar toolBar.add(button1); toolBar.add(button2); toolBar.add(button3); toolBar.add(button4); toolBar.add(button5); toolBar.add(button6); // now create the menu bar // The file menu jMenuFile.setText("File"); jMenuItemOpen.setText("Open"); jMenuItemSave.setText("Save"); jMenuItemSaveAs.setText("Save As..."); jMenuItemCopy.setText("copy"); jMenuItemExit.setText("Exit"); jMenuFile.add(jMenuItemOpen); jMenuFile.add(jMenuItemSave); jMenuFile.add(jMenuItemSaveAs); jMenuFile.add(jMenuItemCopy); jMenuFile.add(jMenuItemExit); // The Edit Menu jMenuEdit.setText("Edit"); jMenuItemFont.setText("Font"); jMenuEdit.add(jMenuItemFont); // The View Menu jMenuView.setText("View"); // The Help menu jMenuHelp.setText("Help"); jMenuItemAbout.setText("About"); jMenuHelp.add(jMenuItemAbout); // add menu object to menu bar jMenuBar.add(jMenuFile); jMenuBar.add(jMenuEdit); jMenuBar.add(jMenuView); jMenuBar.add(jMenuHelp); // set the menu bar to jMenuBar this.setJMenuBar(jMenuBar); contentPane.add(toolBar, BorderLayout.NORTH); contentPane.add(statusLine, BorderLayout.SOUTH); //Sets Content Pane. Insets insets=contentPane.getInsets(); //JLabel - Add a flight. //New FlowLayout. getContentPane().setLayout(new FlowLayout()); JLabel label = new JLabel("Add a Flight"); label.setFont(new Font("Helvetica", Font.PLAIN, 14)); getContentPane().add(label); setLocationRelativeTo(null); setVisible(true); //Sets label location. label.setBounds(20+ insets.left, 55+ insets.top,200, 20);//JLabel. //Makes a new container. Container contentPane=getContentPane(); contentPane.setLayout(null); //JLabel - Flight Option getContentPane().setLayout(new FlowLayout()); label = new JLabel("Flight Option"); label.setFont(new Font("Helvetica", Font.PLAIN, 12)); getContentPane().add(label); setLocationRelativeTo(null); setVisible(true); //Sets label location. label.setBounds(20+ insets.left, 85+ insets.top,200, 20);//JLabel. //Makes a new container. contentPane.setLayout(null); //Makes the two radio buttons. JRadioButton returnTrip=new JRadioButton("Return"); JRadioButton oneWay=new JRadioButton("One Way"); //Sets up the Radio Buttons. ButtonGroup typebutton=new ButtonGroup(); typebutton.add(returnTrip); typebutton.add(oneWay); //Adds Radio Buttons. contentPane.add(returnTrip); contentPane.add(oneWay); //Sets Radio buttons locations. returnTrip.setBounds(120+ insets.left, 115+ insets.top,200, 20);//Radio button. oneWay.setBounds(30+ insets.left, 115+ insets.top,200, 20);//Radio button. } }
|
|