Content in JPanel won't appear - java

There is no errors but when I Run it the content that I added into the JPanel won't appear, only the one not inside the JPanel appear.
import javax.swing.*;
import java.awt.*;
public class SimpleGUI extends JFrame
{
public static void main(String arg[])
{
SimpleGUI f = new SimpleGUI("GUI components");
f.setSize(600,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
SimpleGUI(String s)
{
setTitle(s);
setLayout(new GridLayout(3,2));
JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!");
JButton bt = new JButton("OK");
JLabel lb = new JLabel ("Enter your name:");
JTextField tf = new JTextField("<type name here>");
JLabel lb2 = new JLabel ("Enter age:");
JTextField tf2= new JTextField(10);
tf2.setHorizontalAlignment(JTextField.RIGHT);
JCheckBox cb = new JCheckBox("Bold",true);
JRadioButton rb1 = new JRadioButton("Red");
JTextArea ta = new JTextArea(5,20);
JList list = new JList(new Object[] {"Block A", "Block B"});
JComboBox jcb = new JComboBox(new Object[] {"Hello", "Bye"});
ImageIcon ic = new ImageIcon("music.gif");
JButton newbt = new JButton("Play",ic);
newbt.setVerticalTextPosition(JButton.TOP);
newbt.setHorizontalTextPosition(JButton.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(lb, BorderLayout.WEST);
p1.add(tf, BorderLayout.CENTER);
p1.add(cb, BorderLayout.EAST);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(lb2, BorderLayout.WEST);
p2.add(tf2, BorderLayout.CENTER);
p2.add(rb1, BorderLayout.EAST);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(jcb);
add(ta);
add(list);
p3.add(newbt, BorderLayout.NORTH);
add(msg);
p3.add(bt, BorderLayout.SOUTH);
}
}

I've updated your code. Have a look at this version:
import javax.swing.*;
import java.awt.*;
public class SimpleGUI extends JFrame {
public static void main(String arg[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
SimpleGUI f = new SimpleGUI("GUI components");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
public SimpleGUI(String s) {
setTitle(s);
setLayout(new GridLayout(3, 2));
JLabel msg = new JLabel("FINAL EXAM IS JUST AROUND THE CORNER!");
JButton bt = new JButton("OK");
JLabel lb = new JLabel("Enter your name:");
JTextField tf = new JTextField("<type name here>");
JLabel lb2 = new JLabel("Enter age:");
JTextField tf2 = new JTextField(10);
tf2.setHorizontalAlignment(JTextField.RIGHT);
JCheckBox cb = new JCheckBox("Bold", true);
JRadioButton rb1 = new JRadioButton("Red");
JTextArea ta = new JTextArea(5, 20);
JList list = new JList(new Object[]{"Block A", "Block B"});
JComboBox jcb = new JComboBox(new Object[]{"Hello", "Bye"});
ImageIcon ic = new ImageIcon("music.gif");
JButton newbt = new JButton("Play", ic);
newbt.setVerticalTextPosition(JButton.TOP);
newbt.setHorizontalTextPosition(JButton.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add(lb, BorderLayout.WEST);
p1.add(tf, BorderLayout.CENTER);
p1.add(cb, BorderLayout.EAST);
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(lb2, BorderLayout.WEST);
p2.add(tf2, BorderLayout.CENTER);
p2.add(rb1, BorderLayout.EAST);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(jcb);
add(ta);
add(list);
p3.add(newbt, BorderLayout.NORTH);
add(msg);
p3.add(bt, BorderLayout.SOUTH);
/**
* Need to add the following lines
*/
this.add(p1);
this.add(p2);
this.add(p3);
this.pack();
this.setVisible(true);
}
}
A couple of pointers:
You need to add your components to your JFrame for them to actually show up.
Any updates to the user interface must happen on the event dispatch thread. Consequently you would notice that I've added a SwingUtilites.invokeLater() to the main. Have a look at this article to understand "Threading with Swing"

Where you you add your panels to the frame? Also, forgotten my java "rules and regulations": do you need to call "super()"?

Related

Place label for text box above the box and not to the side

As stated in the title i need to move the label for the text box to be above the box and not to the side. attached i have a picutres of what i mean. what i have vs what i want i have tried searching for it but i cannot seem to find the answer im looking for/not exactly sure what to look up. I have tried using JFrame but it made a separate window unless i need to make the entire GUI a JFrame for me to get the result i want?
Also the actionPerformed method has things but it is irrelevant to the question but displays correctly still.
import java.awt.event.\*;
import javax.swing.\*;
import java.text.DecimalFormat;
public class Project4 extends JFrame implements ActionListener {
private JTextArea taArea = new JTextArea("", 30, 20);
ButtonGroup group = new ButtonGroup();
JTextField name = new JTextField(20);
boolean ch = false;
boolean pep = false;
boolean sup = false;
boolean veg = false;
DecimalFormat df = new DecimalFormat("##.00");
double cost = 0.0;
public Project4() {
initUI();
}
public final void initUI() {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
getContentPane().add(panel1, "North");
getContentPane().add(panel2, "West");
getContentPane().add(panel3, "Center");
getContentPane().add(panel4, "East");
panel4.setLayout(new BoxLayout(panel4, BoxLayout.Y_AXIS));
getContentPane().add(panel5, "South");
JButton button = new JButton("Place Order");
button.addActionListener(this);
panel5.add(button);
JButton button2 = new JButton("Clear");
button2.addActionListener(this);
panel5.add(button2);
panel3.add(taArea);
JCheckBox checkBox1 = new JCheckBox("Cheese Pizza") ;
checkBox1.addActionListener(this);
panel4.add(checkBox1);
JCheckBox checkBox2 = new JCheckBox("Pepperoni Pizza");
checkBox2.addActionListener(this);
panel4.add(checkBox2);
JCheckBox checkBox3 = new JCheckBox("Supreme Pizza");
checkBox3.addActionListener(this);
panel4.add(checkBox3);
JCheckBox checkBox4 = new JCheckBox("Vegetarian Pizza");
checkBox4.addActionListener(this);
panel4.add(checkBox4);
JRadioButton radioButton1 = new JRadioButton("Pick Up");
group.add(radioButton1);
radioButton1.addActionListener(this);
panel1.add(radioButton1);
JRadioButton radioButton2 = new JRadioButton("Delivery");
group.add(radioButton2);
radioButton2.addActionListener(this);
panel1.add(radioButton2);
JLabel name_label = new JLabel("Name on Order");
name.addActionListener(this);
panel5.add(name_label);
panel5.add(name);
setSize(600, 300);
setTitle("Pizza to Order");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent action) {
}
public static void main(String[] args) {
Project4 ex = new Project4();
ex.setVisible(true);
}
}
You can use a nested JPanel with another layout in order to achieve that. I would go with BorderLayout here. You can also other layouts that allow vertical orientation. Visiting the visual guide to Layout Managers will help you spot them.
JLabel name_label = new JLabel("Name on Order");
name.addActionListener(this);
JPanel verticalNestedPanel = new JPanel(new BorderLayout());
verticalNestedPanel.add(name_label, BorderLayout.PAGE_START);
verticalNestedPanel.add(name, BorderLayout.PAGE_END);
panel5.add(verticalNestedPanel);

How do I output the text input and combobox on JList (java)

import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JList;
public class PetUI extends JFrame implements ActionListener{
JButton btnAdd = new JButton("Add");
JButton btnCancel = new JButton("Cancel");
JTextField txtbox1 = new JTextField();
JTextField txtbox2 = new JTextField();
public PetUI(){
super("Pet Shop");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
//inputs
panel1.setLayout(new GridLayout(3,4));
add(panel1,BorderLayout.NORTH);
panel1.add(new JLabel("Pet Type: "));
String [] animals = {"Cat","Dog","Rabbit"};
JComboBox petlist = new JComboBox(animals);
panel1.add(petlist);
panel1.add(new JLabel("Breed: "));
panel1.add(txtbox1);
panel1.add(new JLabel("Name: "));
panel1.add(txtbox2);
//buttons
add(panel2,BorderLayout.CENTER);
panel2.add(btnAdd);
btnAdd.addActionListener(this);
panel2.add(btnCancel);
setVisible(true);
setSize(300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
}
public static void main(String [] args){
new PetUI();
}
}
How do I do this? Output it to a JList.
My teacher didn't taught this to us.
And I tried to search it online but it didn't worked out.
I've been through so many YT tutorials already and it didn't worked out.
Welcome to SO. Here is something very basic to get you started:
public class PetUI extends JFrame implements ActionListener{
JButton btnAdd = new JButton("Add");
JButton btnCancel = new JButton("Cancel");
JTextField txtbox1 = new JTextField();
JTextField txtbox2 = new JTextField();
JList<String> list;
DefaultListModel<String> listModel;
JComboBox<String> petList;
public PetUI(){
super("Pet Shop");
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
//inputs
panel1.setLayout(new GridLayout(3,4));
add(panel1,BorderLayout.NORTH);
panel1.add(new JLabel("Pet Type: "));
String [] animals = {"Cat","Dog","Rabbit"};
petList = new JComboBox<>(animals);
panel1.add(petList);
panel1.add(new JLabel("Breed: "));
panel1.add(txtbox1);
panel1.add(new JLabel("Name: "));
panel1.add(txtbox2);
//buttons
add(panel2,BorderLayout.CENTER);
panel2.add(btnAdd);
btnAdd.addActionListener(this);
panel2.add(btnCancel);
//JList
listModel = new DefaultListModel<>();
list = new JList<>(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(-1);
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout(FlowLayout.LEFT));
panel3.add(list);
add(panel3,BorderLayout.SOUTH);
setVisible(true);
setSize(300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
#Override
public void actionPerformed(ActionEvent e){
StringBuilder sb = new StringBuilder((String)petList.getSelectedItem());
sb.append(" ") //add space
.append(txtbox1.getText())
.append(" ")
.append(txtbox2.getText());
listModel.addElement(sb.toString());
}
public static void main(String [] args){
new PetUI();
}
}

Java GUI JScrollBar How to set the length?

I have the following GUI codded up but I would like to increase the length of the scroll bar on the right side.
Any Idea How to do this?
// test class that implements the gui stuff.
public class testing
{
//variables
private JFrame f = new JFrame("GUI TEST");
private JPanel p = new JPanel();
private JPanel p2 = new JPanel();
private JPanel p3 = new JPanel();
private JPanel p4 = new JPanel();
private JPanel p5 = new JPanel();
private JPanel p6 = new JPanel();
private JPanel p7 = new JPanel();
private JPanel p8 = new JPanel();
private JPanel p9 = new JPanel();
private JPanel p10 = new JPanel();
private JPanel p11 = new JPanel();
private JButton b1 = new JButton("Button");
private JTextField tf1 = new JTextField(" ");
private JTextField tf2 = new JTextField(" ");
private JTextField tf3 = new JTextField(" ");
private JTextArea ta1 = new JTextArea(10,45);
private JLabel label1 = new JLabel("Label 1");
private JLabel label2 = new JLabel("Label 2");
private JLabel label3 = new JLabel("Label 3");
private JLabel label4 = new JLabel("Label 4");
private JScrollBar sb1 = new JScrollBar();
//class constructor
public testing()
{
gui();
}
public void gui()
{
//change length of scroll bar
f.setVisible(true);
f.setSize(600,300);
p.add(label1);
p.add(tf1);
p2.add(label2);
p2.add(tf2);
p3.add(label3);
p3.add(tf3);
p4.add(sb1);
p4.add(label4);
p5.add(ta1);
p6.add(b1);
p4.setBackground(Color.GRAY);
p9.setBackground(Color.GRAY);
p10.setBackground(Color.GRAY);
p11.setBackground(Color.GRAY);
p9.add(p);
p9.add(p2);
p9.add(p3);
p10.add(p5);
p11.add(p6);
//adds panels to frames
f.add(p4, BorderLayout.EAST);
f.add(p9, BorderLayout.NORTH);
f.add(p10, BorderLayout.CENTER);
f.add(p11, BorderLayout.PAGE_END);
}
public static void main(String[] args)
{
new testing();
}
Ordinarilly, you'd simply add your JTextArea to a JScrollPane, which handles the resizing behavior for you.
f.add(new JScrollPane(ta1), BorderLayout.CENTER);
For demonstration purposes, you can override the getPreferredSize() method of the JScrollBar to see the effect.
private JScrollBar sb1 = new JScrollBar(){
#Override
public Dimension getPreferredSize() {
return new Dimension(
super.getPreferredSize().width, ta1.getPreferredSize().height);
}
};
In addition,
Swing GUI objects should be constructed and manipulated only on the event dispatch thread.
Use the appropriate constructor to establish the desired initial size of text components.
Use an appropriate layout to get the desired resizing behavior.
As tested:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Testing {
//variables
private JFrame f = new JFrame("GUI TEST");
private JPanel p = new JPanel();
private JPanel p2 = new JPanel();
private JPanel p3 = new JPanel();
private JPanel p4 = new JPanel();
private JPanel p5 = new JPanel();
private JPanel p6 = new JPanel();
private JPanel p9 = new JPanel();
private JPanel p10 = new JPanel();
private JPanel p11 = new JPanel();
private JButton b1 = new JButton("Button");
private JTextField tf1 = new JTextField(12);
private JTextField tf2 = new JTextField(12);
private JTextField tf3 = new JTextField(12);
private JTextArea ta1 = new JTextArea(10, 45);
private JLabel label1 = new JLabel("Label 1");
private JLabel label2 = new JLabel("Label 2");
private JLabel label3 = new JLabel("Label 3");
private JLabel label4 = new JLabel("Label 4");
private JScrollBar sb1 = new JScrollBar(){
#Override
public Dimension getPreferredSize() {
return new Dimension(super.getPreferredSize().width, ta1.getPreferredSize().height);
}
};
//class constructor
public Testing() {
gui();
}
public void gui() {
p.add(label1);
p.add(tf1);
p2.add(label2);
p2.add(tf2);
p3.add(label3);
p3.add(tf3);
p4.add(sb1);
p4.add(label4);
p5.add(ta1);
p6.add(b1);
p4.setBackground(Color.GRAY);
p9.setBackground(Color.GRAY);
p10.setBackground(Color.GRAY);
p11.setBackground(Color.GRAY);
p9.add(p);
p9.add(p2);
p9.add(p3);
p10.add(p5);
p11.add(p6);
//adds panels to frames
f.add(p4, BorderLayout.EAST);
f.add(p9, BorderLayout.NORTH);
f.add(p10, BorderLayout.CENTER);
f.add(p11, BorderLayout.PAGE_END);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new Testing();
});
}
}

Jpanel is not appearing in JFrame

So I am trying to get a JFrame to display a JPanel that has 5 other JPanels in it. I dont have any syntax errors and all that displays is a very small screen. I have been at this all day and have yet to find a solution.
public class addressPanel extends JPanel {
private JTextField nameT;
private JTextField addressT;
private JTextField cityT;
private JTextField stateT;
private JTextField zipCodeT;
private JTextField phoneNumberT;
private JLabel Title;
private JLabel addressTitle;
private JLabel nameL;
private JLabel addressL;
private JLabel stateL;
private JLabel cityL;
private JLabel zipCodeL;
private JLabel phoneNumberL;
private JLabel orderType;
private JRadioButton takeOut;
private JRadioButton delivery;
private JButton clear;
private JButton submit;
private JPanel addressTextPanel;
private JPanel addressLabelPanel;
private JPanel orderTypePanel;
private JPanel titlePanel;
private JPanel buttonsPanel;
public JPanel addressTextPanel() {
nameT = new JTextField(1);
addressT = new JTextField(2);
cityT = new JTextField(3);
stateT = new JTextField(4);
zipCodeT = new JTextField(5);
phoneNumberT = new JTextField(6);
Font font = new Font(Font.SERIF, Font.PLAIN, 24);
nameT.setFont(font);
addressT.setFont(font);
cityT.setFont(font);
stateT.setFont(font);
zipCodeT.setFont(font);
phoneNumberT.setFont(font);
JPanel addressTextPanel = new JPanel();
addressTextPanel.setPreferredSize(new Dimension(125, 250));
addressTextPanel.setLayout(new BoxLayout(addressTextPanel, BoxLayout.Y_AXIS));
addressTextPanel.add(nameT);
addressTextPanel.add(addressT);
addressTextPanel.add(cityT);
addressTextPanel.add(stateT);
addressTextPanel.add(zipCodeT);
addressTextPanel.add(phoneNumberT);
return addressTextPanel;
}
public JPanel addressLabelPanel() {
nameL = new JLabel("Name:");
addressL = new JLabel("Address:");
cityL = new JLabel("City:");
zipCodeL = new JLabel("Zip Code:");
stateL = new JLabel("State:");
phoneNumberL = new JLabel("Phone Number:");
nameL.setFont(nameL.getFont().deriveFont(24.0f));
addressL.setFont(addressL.getFont().deriveFont(24.0f));
cityL.setFont(cityL.getFont().deriveFont(24.0f));
zipCodeL.setFont(zipCodeL.getFont().deriveFont(24.0f));
stateL.setFont(stateL.getFont().deriveFont(24.0f));
phoneNumberL.setFont(phoneNumberL.getFont().deriveFont(24.0f));
JPanel addressLabelPanel = new JPanel();
addressLabelPanel.setPreferredSize(new Dimension(125, 250));
addressLabelPanel.setLayout(new BoxLayout(addressLabelPanel, BoxLayout.Y_AXIS));
addressLabelPanel.add(nameL);
addressLabelPanel.add(addressL);
addressLabelPanel.add(cityL);
addressLabelPanel.add(stateL);
addressLabelPanel.add(zipCodeL);
addressLabelPanel.add(phoneNumberL);
return addressLabelPanel;
}
public JPanel orderTypePanel() {
orderType = new JLabel("Order Type:");
takeOut = new JRadioButton("Take Out");
delivery = new JRadioButton("Delivery");
orderType.setFont(takeOut.getFont().deriveFont(24.0f));
takeOut.setFont(takeOut.getFont().deriveFont(24.0f));
delivery.setFont(delivery.getFont().deriveFont(24.0f));
JPanel orderTypePanel = new JPanel();
orderTypePanel.setPreferredSize(new Dimension(250, 125));
orderTypePanel.setLayout(new BoxLayout(orderTypePanel, BoxLayout.Y_AXIS));
orderTypePanel.add(orderType);
orderTypePanel.add(takeOut);
orderTypePanel.add(delivery);
return orderTypePanel;
}
public JPanel titlePanel() {
Title = new JLabel("Pizza Order Form");
addressTitle = new JLabel("Address");
Title.setFont(Title.getFont().deriveFont(36.0f));
addressTitle.setFont(addressTitle.getFont().deriveFont(36.0f));
JPanel titlePanel = new JPanel();
titlePanel.setPreferredSize(new Dimension(500, 100));
titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.Y_AXIS));
titlePanel.add(Title);
titlePanel.add(addressTitle);
return titlePanel;
}
public JPanel buttonsPanel() {
clear = new JButton("Clear");
submit = new JButton("Submit");
clear.setFont(clear.getFont().deriveFont(24.0f));
submit.setFont(submit.getFont().deriveFont(24.0f));
JPanel buttonsPanel = new JPanel();
buttonsPanel.setPreferredSize(new Dimension(500, 100));
buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
buttonsPanel.add(clear);
buttonsPanel.add(submit);
return buttonsPanel;
}
public addressPanel() {
JPanel addressParent = new JPanel(new BorderLayout());
addressParent.add(new titlePanel(), BorderLayout.NORTH);
addressParent.add(new orderTypePanel(), BorderLayout.WEST);
addressParent.add(new addressLabelPanel(), BorderLayout.CENTER);
addressParent.add(new addressTextPanel(), BorderLayout.EAST);
addressParent.add(new buttonsPanel(), BorderLayout.SOUTH);
}
public static void main(String[] args) {
// Create Main Panel
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.getContentPane().add(new addressPanel());
// Color background = new Color(238,233,191);
// frame.getContentPane().setBackground(background);
frame.pack();
frame.setVisible(true);
}
}
Read your code. The program creates a JFrame. It creates an instance of addressPanel (which should be named AddressPanel). ANd it adds this addressPanel instance to the frame conent pane.
Now what is added to the addressPanel? Nothing:
public addressPanel()
{
JPanel addressParent = new JPanel(new BorderLayout());
addressParent.add (new titlePanel(), BorderLayout.NORTH);
addressParent.add (new orderTypePanel(), BorderLayout.WEST);
addressParent.add (new addressLabelPanel(), BorderLayout.CENTER);
addressParent.add (new addressTextPanel(), BorderLayout.EAST);
addressParent.add (new buttonsPanel(), BorderLayout.SOUTH);
}
The constructor of addressPanel creates another panel (addressParent), adds plenty of things to this addressParent panel, but doesn't add anything to this, the addressPanel. So the addressPanel is empty.
Please respect the Java naming conventions to make your code readable. Classes start with an uppercase letter.

One of my Panels is not showing

What's wrong with my code? The panel named 'leftPanel' is not showing up when i run the program whereas 'flightPanel
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TravelAgentSystem {
public static void main(String[] args){
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new BorderLayout());
mainFrame.setTitle("Main Menu");
mainFrame.setVisible(true);
mainFrame.setSize(500,500);
mainFrame.setLocationRelativeTo(null);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel flightPanel = new JPanel();
flightPanel.setLayout(new GridLayout(2,2));
JButton timeb, priceb, hotelb, exitb;
timeb = new JButton("Time");
priceb = new JButton ("Price");
hotelb = new JButton ("Hotels Menu");
exitb = new JButton ("Exit Program");
class buttonHandler implements ActionListener{
public void actionPerformed(ActionEvent event) {
JButton clickedButton = (JButton)event.getSource();
String buttonText = clickedButton.getText();
if (buttonText.equals("Exit Program")) {System.exit(0);}
}
}
buttonHandler handler = new buttonHandler();
exitb.addActionListener(handler);
flightPanel.add(timeb);
flightPanel.add(priceb);
flightPanel.add(hotelb);
flightPanel.add(exitb);
mainFrame.add(flightPanel, BorderLayout.CENTER);
JPanel travelPanel = new JPanel();
travelPanel.setLayout(new GridLayout(2,2));
travelPanel.setVisible(true);
JPanel timePanel = new JPanel();
timePanel = new JPanel();
timePanel.setLayout(new GridLayout(2,1));
timePanel.setVisible(true);
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new GridLayout(2,1));
leftPanel.setVisible(true);
JLabel Lfrom, Lto, LDeparture;
Lfrom = new JLabel("From");
Lto = new JLabel("To");
LDeparture = new JLabel("Departure Date (DD/MM/YY)");
String[] fromOptions = {"East Midlands","Birmingham","Heathrow","Manchester"};
String[] toOptions = {"New York", "Dahab", "Rome", "Sydney", "Tokyo"};
JComboBox fromDest = new JComboBox(fromOptions);
JComboBox toDest = new JComboBox(toOptions);
JPanel datePanel = new JPanel();
datePanel.setLayout(new FlowLayout()); // So i can fix size of dateField
JTextField dateField = new JTextField();
dateField.setPreferredSize(new Dimension(100,20));
datePanel.add(dateField);
travelPanel.add(Lfrom);
travelPanel.add(fromDest);
travelPanel.add(Lto);
travelPanel.add(toDest);
timePanel.add(LDeparture);
timePanel.add(datePanel);
leftPanel.add(travelPanel);
leftPanel.add(timePanel);
mainFrame.add(leftPanel, BorderLayout.EAST);
}
}
Call mainFrame.setVisible(true); in last. I.e,
mainFrame.add(leftPanel, BorderLayout.EAST);
mainFrame.setVisible(true);
Here's what I get :

Categories