• Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting the Background color in a panel.

 
Caitlin Gibson
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
When I run my code, there is an area on the JFrame that is *not* set to the correct color.
Any ideas why?
<code>
/**
* @author Caitlin Gibson
* @version 1.0
*
* This is a shipping calculator application. It was designed
* to calculate the cost for a package depending on the 'zone' it
* is being shipped to, it's weight ( pounds or metric ) and the
* size ( length ).
* <P>
* In the event that a specfic field is not within an alloted
* range, the user is notified by the appearance of a message box.
*
*
*/
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.text.NumberFormat;
public class MidTerm extends JFrame
implements ActionListener
{
private JTextField tfZone,
tfWeight,
tfSize,
tfCost;


private JButton bCalc,
bClear,
bExit;

private String zone,
weight,
size,
invoice;
private char zoneLetter;

private int packageWeight,
packageSize;
private double totalCost;
NumberFormat money = null;
public MidTerm()
{
JPanel myPanels = new JPanel();
myPanels.setLayout( new GridLayout( 4, 1 ));
myPanels.add( new JLabel("Zone:"));
myPanels.add( new JLabel("Weight:"));
myPanels.add( new JLabel("Size:"));
myPanels.add( new JLabel("Cost:"));
myPanels.setBackground( Color.green );


JPanel myTextFields = new JPanel();
myTextFields.setLayout( new GridLayout( 4, 1 ));
myTextFields.add( tfZone = new JTextField(10));
myTextFields.add( tfWeight = new JTextField(10));
myTextFields.add( tfSize = new JTextField(10));
myTextFields.add( tfCost = new JTextField(10));
myTextFields.setBackground( Color.green );

JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add( myPanels, BorderLayout.WEST );
p1.add( myTextFields, BorderLayout.CENTER );
p1.setBackground( Color.green );

JPanel p2 = new JPanel();
p2.setLayout( new FlowLayout(FlowLayout.LEFT));
p2.add( bCalc = new JButton("Calculate"));
bCalc.setToolTipText("Calculates total shipping cost");
bCalc.setMnemonic('c');
p2.add( bClear = new JButton("Clear"));
bClear.setToolTipText("Clears all text field data");
bClear.setMnemonic('l');
p2.add( bExit = new JButton("Exit"));
bExit.setToolTipText("Exits the application");
bExit.setMnemonic('e');
p2.setBackground( Color.green );

getContentPane().setLayout( new BorderLayout());
getContentPane().add(p1, BorderLayout.WEST);
getContentPane().add(p2, BorderLayout.SOUTH);


bCalc.addActionListener( this );
bClear.addActionListener( this );
bExit.addActionListener( this );
money = NumberFormat.getCurrencyInstance();

tfCost.setEditable( false );

}
public static void main( String args[] )
{
MidTerm myMidTerm = new MidTerm();

myMidTerm.setTitle("Shipping Calculator");
myMidTerm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMidTerm.pack();
myMidTerm.show();

}

public void actionPerformed( ActionEvent e )
{
String actionCommand = e.getActionCommand();
if( e.getSource() instanceof JButton )
{
if( "Calculate".equals( actionCommand ))
{
totalCost = CalcCost();
tfCost.setText( "$" );
tfCost.setText( String.valueOf( money.format( totalCost )));
}

if( "Clear".equals( actionCommand ))
{
tfZone.setText("");
tfSize.setText("");
tfWeight.setText("");
tfCost.setText( String.valueOf( "" ));
}
if( "Exit".equals( actionCommand ))
System.exit(0);
}


}
public double CalcCost()
{

zone = tfZone.getText().trim();
weight = tfWeight.getText().trim();
size = tfSize.getText().trim();

zoneLetter = zone.charAt(0);
packageWeight = Integer.parseInt( weight );
packageSize = Integer.parseInt( size );

switch( zoneLetter )
{
case 'a' :
case 'A' : totalCost += 10;
break;
case 'b' :
case 'B' : totalCost += 15;
break;
case 'c' :
case 'C' : totalCost += 20;
break;

default : JOptionPane.showMessageDialog( null, "Unknown zone! " +
"Please re-enter");

}

if( packageWeight > 0 && packageWeight <= 10 )
totalCost += 5;
else if( packageWeight > 10 && packageWeight <= 20 )
totalCost += 10;
else if( packageWeight > 20 && packageWeight <= 30 )
totalCost += 15;
else if( packageWeight > 30 && packageWeight <= 40 )
totalCost += 20;
else
{
JOptionPane.showMessageDialog( null, "Your package is too heavy!" );
System.exit(0);
}


if( packageSize > 0 && packageSize <= 10 )
totalCost += 2;
else if( packageSize > 10 && packageSize <= 20 )
totalCost += 3;
else if( packageSize > 20 && packageSize <= 30 )
totalCost += 5;
else if( packageSize > 30 && packageSize <= 40 )
totalCost += 8;
else
{
JOptionPane.showMessageDialog( null, "Your package is too large!" );
System.exit(0);
}

return totalCost;
}
}
</code>
Also, unless there is a value entered in each JTextField, when I click 'Calculate', the command line window behind the app. ( win32 ) fills with various messages. Can I prevent those by throwing an exception?
Thanks,
-Caitlin.
[ December 07, 2002: Message edited by: Caitlin Gibson ]
[ December 07, 2002: Message edited by: Caitlin Gibson ]
 
Ajit Malleri
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of getContentPane().add(p1, BorderLayout.WEST) use getContentPane().add(p1, BorderLayout.CENTER);
This should solve your first problem.
For your second problem, you should first check the value entered inside the text fields and then proceed with the calculations. This way you won't get the exceptions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!