Skip to main content

Java swing class program displaying wrong

3 replies [Last post]
cgodfrey987
Offline
Joined: 2012-11-30
Points: 0

my code isn't displaying right. It seems it is displaying my questionPanel horizontally instead of vertically. I have it set for a gridlayout of 7 rows and 2 cols but it is displaying straight across horizontally. I can't show it all on this posting. So I will post what I think is relevant and then if you want the whole file, please just let me know!! Thanks so much in advance!!

package travelExpenses;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TravelExpenses extends JFrame
{
private final double RENTALCAR = 40;
private final double PARKING = 20;
private final double FOOD = 40;
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenu editMenu;
private JMenuItem exit;
private JMenuItem cut;
private JMenuItem copy;
private JMenuItem paste;
private JLabel numDays;
private JLabel airfareCost;
private JLabel rentalCarCost;
private JLabel parkingFees;
private JLabel classFee;
private JLabel lodgingFee;
private JLabel foodCost;
private JTextField numDaysF, airfareCostF, rentalCarCostF, parkingFeesF, classFeeF;
private JTextField lodgingFeeF, foodCostF;
private JPanel questionPanel;
private JPanel buttonPanel;
private JButton calcButton;
private JButton exitButton;

public TravelExpenses()
{
super("Travel Expenses");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2,1));
buildMenuBar();
buildQuestionPanel();
buildButtonPanel();

add(questionPanel);
add(buttonPanel);
pack();
setVisible(true);
}

public void buildMenuBar()
{
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
exit = new JMenuItem("Exit");
exit.setMnemonic(KeyEvent.VK_E);
exit.addActionListener(new ExitButtonListener());
fileMenu.add(exit);
editMenu = new JMenu("Edit");
editMenu.setMnemonic(KeyEvent.VK_E);
cut = new JMenuItem("Cut");
cut.setMnemonic(KeyEvent.VK_X);
copy = new JMenuItem("Copy");
copy.setMnemonic(KeyEvent.VK_C);
paste = new JMenuItem("Paste");
paste.setMnemonic(KeyEvent.VK_P);
editMenu.add(cut);
editMenu.add(copy);
editMenu.add(paste);
menuBar.add(fileMenu);
menuBar.add(editMenu);
setJMenuBar(menuBar);
}

public void buildQuestionPanel()
{
questionPanel = new JPanel();
setLayout(new GridLayout(7,2));
numDays = new JLabel("Days on Trip");
numDaysF = new JTextField(6);
airfareCost = new JLabel("Airfare Cost");
airfareCostF = new JTextField(6);
rentalCarCost = new JLabel("Rental Car Cost");
rentalCarCostF = new JTextField(6);
parkingFees = new JLabel("Parking Cost");
parkingFeesF = new JTextField(6);
classFee = new JLabel("Conference / Training Cost");
classFeeF = new JTextField(6);
lodgingFee = new JLabel("Lodging Cost");
lodgingFeeF = new JTextField(6);
foodCost = new JLabel("Food Cost");
foodCostF = new JTextField(6);

questionPanel.add(numDays);
questionPanel.add(numDaysF);
questionPanel.add(airfareCost);
questionPanel.add(airfareCostF);
questionPanel.add(rentalCarCost);
questionPanel.add(rentalCarCostF);
questionPanel.add(parkingFees);
questionPanel.add(parkingFeesF);
questionPanel.add(classFee);
questionPanel.add(classFeeF);
questionPanel.add(lodgingFee);
questionPanel.add(lodgingFeeF);
questionPanel.add(foodCost);
questionPanel.add(foodCostF);
}

public void buildButtonPanel()
{
calcButton = new JButton("Calculate Total");
exitButton = new JButton("Exit");
buttonPanel = new JPanel();
calcButton.addActionListener(new CalcButtonListener());
buttonPanel.add(calcButton);
buttonPanel.add(exitButton);
}

private class CalcButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double days, airfare, rentalCar, parking, classFee, lodging, food;
double total, allowableTotal, overage;
double rentalOverage, parkingOverage, foodOverage;
double rentalAllowance, parkingAllowance, foodAllowance;
String str;

str = numDays.getText();
days = Double.parseDouble(str);
str = airfareCostF.getText();
airfare = Double.parseDouble(str);
str = rentalCarCostF.getText();
rentalCar = Double.parseDouble(str);
str = parkingFeesF.getText();
parking = Double.parseDouble(str);
str = classFeeF.getText();
classFee = Double.parseDouble(str);
str = lodgingFeeF.getText();
lodging = Double.parseDouble(str);
str = foodCostF.getText();
food = Double.parseDouble(str);
rentalAllowance = RENTALCAR * days;
parkingAllowance = PARKING * days;
foodAllowance = FOOD * days;
total = airfare + rentalCar + parking + classFee + lodging + food;

if (rentalCar > rentalAllowance)
rentalOverage = rentalCar - rentalAllowance;
else
rentalOverage = 0;
if (parking > parkingAllowance)
parkingOverage = parking - parkingAllowance;
else
parkingOverage = 0;
if (food > foodAllowance)
foodOverage = food - foodAllowance;
else
foodOverage = 0;

overage = rentalOverage + parkingOverage + foodOverage;
allowableTotal = total - overage;

JOptionPane.showMessageDialog(null, "Total Charges: $" + total + "\n\n" +
"Non-Reimburstable Charges: $" + overage + "\n\n" +
"Your reimburstable amount: $" + allowableTotal,
"Total", JOptionPane.PLAIN_MESSAGE);
}
}

private class ExitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);

}
}

public static void main(String[] args)
{
new TravelExpenses();
}

}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bachi_1983
Offline
Joined: 2012-11-26
Points: 0

Hi cgodfray,

I have seen your code and modified a little.All you have to do is setLayout for question panel only.

public void buildQuestionPanel()
{
questionPanel = new JPanel();
questionPanel.setLayout(new GridLayout(7,2));
numDays = new JLabel("Days on Trip");
numDaysF = new JTextField(6);
airfareCost = new JLabel("Airfare Cost");
airfareCostF = new JTextField(6);
rentalCarCost = new JLabel("Rental Car Cost");
rentalCarCostF = new JTextField(6);
parkingFees = new JLabel("Parking Cost");
parkingFeesF = new JTextField(6);
classFee = new JLabel("Conference / Training Cost");
classFeeF = new JTextField(6);
lodgingFee = new JLabel("Lodging Cost");
lodgingFeeF = new JTextField(6);
foodCost = new JLabel("Food Cost");
foodCostF = new JTextField(6);

questionPanel.add(numDays);
questionPanel.add(numDaysF);
questionPanel.add(airfareCost);
questionPanel.add(airfareCostF);
questionPanel.add(rentalCarCost);
questionPanel.add(rentalCarCostF);
questionPanel.add(parkingFees);
questionPanel.add(parkingFeesF);
questionPanel.add(classFee);
questionPanel.add(classFeeF);
questionPanel.add(lodgingFee);
questionPanel.add(lodgingFeeF);
questionPanel.add(foodCost);
questionPanel.add(foodCostF);
}

I hope this is enough to solve your problem, if solved post a reply here or in my blog.
You can see the output as shown below...

Output

Regards
bachi
Myblog

cgodfrey987
Offline
Joined: 2012-11-30
Points: 0

Thanks bachi! Also, do you know what I need to do to make there be more space around the window. Liks so the labels aren't right against the edge of the border, etc? And make the window bigger in general?! Thanks for your help!

bachi_1983
Offline
Joined: 2012-11-26
Points: 0

Hi,

I think you should change your layout of Question panel.I don't know what exacly suits your requirement.But when you choose grid layout with x,y parameters it makes x rows and y columns.You can choose the correct layout for your panel.

Regards
bachi