Results 1 to 4 of 4
  1. #1
    loja11 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default Are there options to cramming things in the constructor. Here is the code.

    import java.awt.*;
    import javax.swing.*;


    public class Visitor extends JFrame {

    JPanel basePane, leftPane, rightPane;
    JLabel cardTitle, firstName, lastName, nationality;
    JLabel entryDate, departDate, stayLength, visitPurpose;

    JTextField forFname, forLname, forNationality;
    JTextField forEntryDate, forDepartDate;

    JComboBox forStayLength, forPurposeChooser;

    String strFname, strLname, strNat;
    String[] stay = {"less than 5 days" ,"one week", "about 10 days", " about two weeks",
    "more than two weeks"};
    String[] purpose = {"Tourist", "Business", "Conference", "Visit Family",
    "transit"};

    public Visitor() {

    basePane = new JPanel();
    basePane.setLayout(new BorderLayout());
    basePane.setBackground(Color.red);

    cardTitle = new JLabel("WOW COUNTRY TOURIST OFFICE");
    cardTitle.setFont(new Font("Consolas", Font.ITALIC, 20));
    cardTitle.setForeground(Color.blue);
    cardTitle.setHorizontalAlignment(JLabel.CENTER);
    cardTitle.setOpaque(true);
    cardTitle.setBackground(Color.white);
    cardTitle.setPreferredSize(new Dimension(500,35));

    firstName = new JLabel("First Name");
    lastName = new JLabel("Last Name");
    nationality = new JLabel("Nationality");

    entryDate = new JLabel("Date of Entry");
    departDate = new JLabel("Date of Departure");
    stayLength = new JLabel("How long will you be staying?");
    visitPurpose = new JLabel("What is the purpose of your visit?");


    forFname = new JTextField(20);
    forLname = new JTextField(20);
    forNationality = new JTextField(20);

    forEntryDate = new JTextField(10);
    forDepartDate = new JTextField(10);
    forStayLength = new JComboBox(stay);
    forPurposeChooser = new JComboBox(purpose);


    leftPane = new JPanel();
    leftPane.setLayout(new BoxLayout(leftPane, BoxLayout.Y_AXIS));
    leftPane.setPreferredSize(new Dimension(200, 250));
    leftPane.setBackground(Color.GREEN);
    leftPane.add(firstName);
    leftPane.add(forFname);
    leftPane.add(lastName);
    leftPane.add(forLname);
    leftPane.add(nationality);
    leftPane.add(forNationality);


    rightPane = new JPanel();
    rightPane.setLayout(new BoxLayout(rightPane, BoxLayout.Y_AXIS));
    rightPane.setPreferredSize(new Dimension(250, 250));
    rightPane.setBackground(Color.yellow);
    rightPane.add(entryDate);
    rightPane.add(forEntryDate);
    rightPane.add(departDate);
    rightPane.add(forDepartDate);
    rightPane.add(stayLength);
    rightPane.add(forStayLength);
    rightPane.add(visitPurpose);
    rightPane.add(forPurposeChooser);


    basePane.add(cardTitle, BorderLayout.NORTH);
    basePane.add(leftPane, BorderLayout.WEST);
    basePane.add(rightPane, BorderLayout.EAST);

    this.add(basePane);

    }

    public static void main(String[] args){
    Visitor form = new Visitor();
    form.setTitle("Visitor Info Format");
    form.setSize(500, 400);
    form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

    form.setVisible(true);
    }

    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Are there options to cramming things in the constructor. Here is the code.

    Sure there are options. You could write an initializer block, or call a function. But is there a way around writing code? No, at least not yet.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    loja11 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default Re: Are there options to cramming things in the constructor. Here is the code.

    Quote Originally Posted by loja11 View Post
    import java.awt.*;
    import javax.swing.*;


    public class Visitor extends JFrame {

    JPanel basePane, leftPane, rightPane;
    JLabel cardTitle, firstName, lastName, nationality;
    JLabel entryDate, departDate, stayLength, visitPurpose;

    JTextField forFname, forLname, forNationality;
    JTextField forEntryDate, forDepartDate;

    JComboBox forStayLength, forPurposeChooser;

    String strFname, strLname, strNat;
    String[] stay = {"less than 5 days" ,"one week", "about 10 days", " about two weeks",
    "more than two weeks"};
    String[] purpose = {"Tourist", "Business", "Conference", "Visit Family",
    "transit"};

    public Visitor() {

    basePane = new JPanel();
    basePane.setLayout(new BorderLayout());
    basePane.setBackground(Color.red);

    cardTitle = new JLabel("WOW COUNTRY TOURIST OFFICE");
    cardTitle.setFont(new Font("Consolas", Font.ITALIC, 20));
    cardTitle.setForeground(Color.blue);
    cardTitle.setHorizontalAlignment(JLabel.CENTER);
    cardTitle.setOpaque(true);
    cardTitle.setBackground(Color.white);
    cardTitle.setPreferredSize(new Dimension(500,35));

    firstName = new JLabel("First Name");
    lastName = new JLabel("Last Name");
    nationality = new JLabel("Nationality");

    entryDate = new JLabel("Date of Entry");
    departDate = new JLabel("Date of Departure");
    stayLength = new JLabel("How long will you be staying?");
    visitPurpose = new JLabel("What is the purpose of your visit?");


    forFname = new JTextField(20);
    forLname = new JTextField(20);
    forNationality = new JTextField(20);

    forEntryDate = new JTextField(10);
    forDepartDate = new JTextField(10);
    forStayLength = new JComboBox(stay);
    forPurposeChooser = new JComboBox(purpose);


    leftPane = new JPanel();
    leftPane.setLayout(new BoxLayout(leftPane, BoxLayout.Y_AXIS));
    leftPane.setPreferredSize(new Dimension(200, 250));
    leftPane.setBackground(Color.GREEN);
    leftPane.add(firstName);
    leftPane.add(forFname);
    leftPane.add(lastName);
    leftPane.add(forLname);
    leftPane.add(nationality);
    leftPane.add(forNationality);


    rightPane = new JPanel();
    rightPane.setLayout(new BoxLayout(rightPane, BoxLayout.Y_AXIS));
    rightPane.setPreferredSize(new Dimension(250, 250));
    rightPane.setBackground(Color.yellow);
    rightPane.add(entryDate);
    rightPane.add(forEntryDate);
    rightPane.add(departDate);
    rightPane.add(forDepartDate);
    rightPane.add(stayLength);
    rightPane.add(forStayLength);
    rightPane.add(visitPurpose);
    rightPane.add(forPurposeChooser);


    basePane.add(cardTitle, BorderLayout.NORTH);
    basePane.add(leftPane, BorderLayout.WEST);
    basePane.add(rightPane, BorderLayout.EAST);

    this.add(basePane);

    }

    public static void main(String[] args){
    Visitor form = new Visitor();
    form.setTitle("Visitor Info Format");
    form.setSize(500, 400);
    form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

    form.setVisible(true);
    }

    }
    Thank you for the reponse. What i wanted to ask is, does it look ok the way i put the code in the constructor. I think it would be good to put some of it somewhere and refer to it. Since it does not have significant functionality yet (only forms) i thought better to put it in the constructor.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Are there options to cramming things in the constructor. Here is the code.

    I usually have methods for the individual JPanels.
    So the constructor looks something like:
    Java Code:
    public MyGUI() {
    ...
        frame.add(createBasePanel());
    ...
    }
    private JPanel createBasePanel() {
        basePane = new JPanel();
        basePane.setLayout(new BorderLayout());
        basePane.setBackground(Color.red);
        cardTitle = new JLabel("WOW COUNTRY TOURIST OFFICE");
    ... other cardTitle bits
        basePane.add(cardTitle, BorderLayout.NORTH);
        basePane.add(createLeftPanel(), BorderLayout.WEST);
        basePane.add(createRightPanel(), BorderLayout.EAST);
        return basePane;
    }
    .. etc etc
    It just breaks down the layout so, if you need to change the stuff in (say) the leftPanel then you can look at the method that makes that part of the gui without being distracted by any of the other stuff.

Similar Threads

  1. Replies: 4
    Last Post: 07-19-2011, 06:33 PM
  2. adding code to constructor
    By twillders in forum New To Java
    Replies: 5
    Last Post: 12-13-2010, 09:29 PM
  3. Help with this code, overload the constructor
    By zoe in forum New To Java
    Replies: 12
    Last Post: 11-03-2008, 03:55 AM
  4. Replies: 0
    Last Post: 03-29-2008, 01:38 PM
  5. Replies: 0
    Last Post: 12-19-2007, 10:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •