找不到符号setBorder()方法java

时间:2014-05-15 01:07:47

标签: java swing jpanel awt border

我导入了javax.swing.border。*;并且有错误的代码是教授提供的代码。产生的错误是

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

该应用程序只是一个基本的银行提款和存款小程序,它有一个账户,初始余额为1000美元,需要处理。

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);
  }

1 个答案:

答案 0 :(得分:2)

方法setBorder(Border)JComponent提供。 JPanel扩展JComponent,因此它会继承该方法。

该代码使用基于AWT的Panel 不扩展JComponent 。使用JPanel(并始终使用Swing组件)。