Question

I have imported javax.swing.border.*; and the piece of code that has the error is one that was provided by the professor. The error that is produced is

AccountApplet.java:56: cannot find symbol
symbol  : method setBorder(javax.swing.border.TitledBorder)
location: class java.awt.Panel
    p1.setBorder(new TitledBorder("Display Account Information"));
      ^
1 error

The application is just a basic bank withdraw and deposit applet that has a single account with an initial balance of $1000 that is to be processed.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.text.DecimalFormat;

public class AccountApplet extends JApplet implements ActionListener
{
  private JTextField tf_id          = new JTextField(),
                     tf_balance     = new JTextField(),
                     tf_deposit     = new JTextField(),
                     tf_withdraw    = new JTextField();

  private JButton    b_deposit      = new JButton("Deposit"),
                     b_withdraw     = new JButton("Withdraw");

  private JLabel     la_id          = new JLabel("Account ID"),
                     la_balance     = new JLabel("Account Balance"),
                     la_deposit     = new JLabel("Deposit"),
                     la_withdraw    = new JLabel("Withdraw"),
                     la_transaction = new JLabel(" ");

  private Container  c              = getContentPane();

  DecimalFormat f = new DecimalFormat("##.00");
  Account       a = new Account(1234, 1000.00);

  double  amount;

  public void init()
  {
    c.setLayout(new BorderLayout());
    panel1();
    panel2();
    panel3();
    RefreshFields();
  }

  // ----------------------------------------------------------
  // Panel 1 is holds the account id and account balance fields
  // ----------------------------------------------------------
  private void panel1()
  {
    Panel p1   = new Panel(); 
    p1.setLayout(new BorderLayout());
    p1.setBorder(new TitledBorder("Display Account Information")); // Professor's code told to copy and paste were the error is occuring
    p1.setLayout(new GridLayout(2, 2));
    p1.add(la_id);
    p1.add(tf_id);
    p1.add(la_balance);
    p1.add(tf_balance);
    c.add(p1, BorderLayout.WEST); 
    tf_id.setEditable(false);
    tf_balance.setEditable(false);
  }
Was it helpful?

Solution

The method setBorder(Border) is provided by JComponent. A JPanel extends JComponent so it inherits the method.

That code is using the AWT based Panel which does not extend JComponent. Use a JPanel (and use Swing components consistently) instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top