I'm working on a program in which I am using the BorderLayout Manager. However, I'm having a difficult time changing the size of the window. I want to make it longer and more narrow, with only one or two text fields per row and want to make the text field where there user enters in their information shorter to only allow for up to a 3 digit number.
Here is my code:
[
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class CustomerVisit extends JFrame
{ // Begin public class
private DataPanel dataPanel; // A panel to hold labels and text fields
private JPanel buttonPanel; // A panel for the buttons
private JButton calcButton; // Button to calculate everything
private JButton resetButton; // Button to reset everything
private final int WINDOW_WIDTH = 400; // Setting the window width
private final int WINDOW_HEIGHT = 350; // Setting the window height
// Create the constructor
public CustomerVisit()
{ // Begin public CustomerVisit()
// Display the title
setTitle("Customer Visit");
// Set the size of the window
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// Specify what happens when the close button is clicked
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a DataPanel object
dataPanel = new DataPanel();
// Build the panel that contains the buttons
buildButtonPanel();
// Add a BorderLayout manager to the content pane
setLayout(new BorderLayout());
// Add the panel to the content pane
add(dataPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
// Pack and display the window
pack();
setVisible(true);
} // End public CustomerVisit()
// The buildButtonPanel method creates a panel containing buttons
private void buildButtonPanel()
{ // Begin private void buildButtonPanel()
// Create a button to Calculate the expenses
calcButton = new JButton("Calculate Charges");
// Add an action listener to the button
calcButton.addActionListener(new CalcButtonListener());
// Create a button to reset everything
resetButton = new JButton("Reset");
// Add an action listener to the button
resetButton.addActionListener(new ResetButtonListener());
// Put the buttons in their own panel
buttonPanel = new JPanel();
buttonPanel.add(resetButton);
buttonPanel.add(calcButton);
} // End private void buildButtonPanel()
// CalcButtonListener is an action listener class for calcButton component
private class CalcButtonListener implements ActionListener
{ // Begin private class CalcButtonListener implements ActionListener
/**
actionPerformed method
@param e An ActionEvent object
*/
public void actionPerformed(ActionEvent e)
{ // Begin public void actionPerformed(ActionEvent e)
double OIL_CHANGE = 26.00; // Constant for oil change- $26.00
double LUBE_JOB = 18.00; // Constant for lube job- $18.00
double RAD_FLUSH = 30.00; // Constant for radiator flush- $30.00
double TRANS_FLUSH = 80.00; // Constant for transmission flush- $80.00
double INSPECTION = 15.00; // Constant for inspection- $15.00
double MUFFLER_REPL = 100.00; // Constant for muffler replacement- $100.00
double TIRE_ROTATE = 20.00; // Constant for tire rotation- $20.00
double HOURLY_RATE = 20.00; // Constant for nonroutine service hourly rate- $20.00
double miscParts; // Variable for nonroutine parts
double totalMiscParts; // Variable for total cost of misc parts
double totalOilChange; // Variable for total cost of oil changes
double totalLubeJob; // Variable for total cost of lube jobs
double totalRadFlush; // Variable for total cost of radiator flushes
double totalTransFlush; // Variable for total cost of transmission flushes
double totalInspection; // Variable for total cost of inspections
double totalMufflerRepl; // Variable for total cost of muffler replacements
double totalTireRotate; // Variable for total cost of tire rotations
double totalHourlyRate; // Variable for total cost of hourly rate
double totalNonroutine; // Variable for total cost of nonroutine services provided
double totalCost; // Variable for total cost of services
String msg; // To hold a message
// Create a DecimalFormat object to format output
DecimalFormat dollar = new DecimalFormat("#,###.##");
// Calculate the cost of oil changes
totalOilChange = OIL_CHANGE * dataPanel.getOilChangeQuantity();
// Calculate the cost of lube jobs
totalLubeJob = LUBE_JOB * dataPanel.getLubeJobQuantity();
// Calculate the cost of radiator flushes
totalRadFlush = RAD_FLUSH * dataPanel.getRadFlushQuantity();
// Calculate the cost of transmission flushes
totalTransFlush = TRANS_FLUSH * dataPanel.getTransFlushQuantity();
// Calcluate the cost of inspections
totalInspection = INSPECTION * dataPanel.getInspectionQuantity();
// Calculate the cost of muffler replacements
totalMufflerRepl = MUFFLER_REPL * dataPanel.getMufflerReplQuantity();
// Calcluate the cost of tire rotations
totalTireRotate = TIRE_ROTATE * dataPanel.getTireRotateQuantity();
// Calculate the non-routine services performed
totalHourlyRate = HOURLY_RATE * dataPanel.getHourlyRateQuantity();
totalMiscParts = dataPanel.getMiscPartsCost() * dataPanel.getMiscPartsQuantity();
totalNonroutine = totalHourlyRate + totalMiscParts;
// Calculate the total cost of the visit
totalCost = totalOilChange + totalLubeJob + totalRadFlush + totalTransFlush + totalInspection + totalMufflerRepl + totalTireRotate + totalNonroutine;
// Create the message to display
msg = "Total cost of customer visit: $" +
dollar.format(totalCost);
// Display the message
JOptionPane.showMessageDialog(null, msg);
} // End public void actionPerformed(ActionEvent e)
} // End private class CalcButtonListener implements ActionListener
// ResetButtonListener is an action listener class for the resetButton component
private class ResetButtonListener implements ActionListener
{ // Begin private class ResetButtonListener implements ActionListener
/**
actionPerformed method
@param e An ActionEvent object
*/
public void actionPerformed(ActionEvent e)
{ // Begin public void actionPerformed(ActionEvent e)
dataPanel.reset();
} // End public void actionPerformed(ActionEvent e)
} // End private class ResetButtonListener implements ActionListener
/**
The main method creates in instance of the Customer Visit class,
causing it to display its window.
*/
public static void main(String[] args)
{ // Begin public static void main(String[] args)
CustomerVisit cv = new CustomerVisit();
} // End public static void main(String[] args)
} // End public class CustomerVisit extend JFrame
]