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);
}
}
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.
Re: Are there options to cramming things in the constructor. Here is the code.
Quote:
Originally Posted by
loja11
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.
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:
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.