我在设置applet标题时遇到问题

时间:2014-07-06 07:07:17

标签: java applet

我想开始说我已经尝试了大约2个小时来找到如何使用JApplet标记我的applet。我刚刚开始使用GUI大约两周前。这是我的代码。我在init()中遇到了麻烦;任何帮助都会很棒,谢谢!-Tanner

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


public class blooddrive_average extends JApplet implements ActionListener
{ 
  JLabel Dep1=new JLabel("Department 1");
  JTextField atext1=new JTextField(5);
  JLabel Dep2=new JLabel("Department 2");
  JTextField atext2=new JTextField(5);
  JLabel Dep3=new JLabel("Department 3");
  JTextField atext3=new JTextField(5);
  JLabel Dep4=new JLabel("Department 4");
  JTextField atext4=new JTextField(5);
  JLabel Dep5=new JLabel("Department 5");
  JTextField atext5=new JTextField(5); 
  JButton but1=new JButton("Average"); //button 1
  JButton but2=new JButton("Clear Fields"); //button 2

  Font resfont=new Font("Ravie", Font.BOLD,20);
  JLabel average=new JLabel(); //for the action listener  
  Container c;
  FlowLayout flow= new FlowLayout();


  public void init()
  {
    JFrame window=new JFrame();
    window.setTitle("A");   //************ this is where i'm having trouble declaring    the applets title
    c=getContentPane();
    c.setLayout(flow);
    c.setBackground(Color.white);

    Dep1.setForeground(Color.red);
    c.add(Dep1);
    atext1.setForeground(Color.blue);
    c.add(atext1);
    Dep2.setForeground(Color.red);
    c.add(Dep2);    
    atext2.setForeground(Color.blue);
    c.add(atext2);
    Dep3.setForeground(Color.red);
    c.add(Dep3);  
    atext3.setForeground(Color.blue);
    c.add(atext3);
    Dep4.setForeground(Color.red);
    c.add(Dep4);    
    atext4.setForeground(Color.blue);
    c.add(atext4);
    Dep5.setForeground(Color.red);
    c.add(Dep5);
    atext5.setForeground(Color.blue);
    c.add(atext5); 

    but1.setForeground(Color.red);
    c.add(but1);
    but2.setForeground(Color.red);
    c.add(but2);

    average.setFont(resfont);
    average.setForeground(Color.blue);
    c.add(average);

    but1.addActionListener(this);
    but2.addActionListener(this);
  }

  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource() == but1)
    {
      String text1=atext1.getText();
      double number1=Double.parseDouble(text1);      
      String text2=atext2.getText();
      double number2=Double.parseDouble(text2);      
      String text3=atext3.getText();
      double number3=Double.parseDouble(text3);      
      String text4=atext4.getText();
      double number4=Double.parseDouble(text4);      
      String text5=atext5.getText();
      double number5=Double.parseDouble(text5);      
      double average1=((number1+number2+number3+number4+number5)/5);
      average.setText("The average pints of blood donated\n" +  " by the five departments is: "+average1);
      atext1.setEnabled(false);
      atext2.setEnabled(false);
      atext3.setEnabled(false);
      atext4.setEnabled(false);
      atext5.setEnabled(false);
    }
    else if(e.getSource() == but2)
    {

      average.setVisible(false);
      JLabel Dep1=new JLabel("Department 1");
      atext1.setText("");
      JLabel Dep2=new JLabel("Department 2");
      atext2.setText("");
      JLabel Dep3=new JLabel("Department 3");
      atext3.setText("");
      JLabel Dep4=new JLabel("Department 4");
      atext4.setText("");
      JLabel Dep5=new JLabel("Department 5");
      atext5.setText("");
      atext1.setEnabled(true);
      atext2.setEnabled(true);
      atext3.setEnabled(true);
      atext4.setEnabled(true);
      atext5.setEnabled(true);
      atext1.requestFocus();
    }


  }
}

1 个答案:

答案 0 :(得分:0)

刚刚测试过它对我有用。这是我使用的代码:

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


public class blooddrive_average extends JApplet implements ActionListener
{ 
  JLabel Dep1=new JLabel("Department 1");
  JTextField atext1=new JTextField(5);
  JLabel Dep2=new JLabel("Department 2");
  JTextField atext2=new JTextField(5);
  JLabel Dep3=new JLabel("Department 3");
  JTextField atext3=new JTextField(5);
  JLabel Dep4=new JLabel("Department 4");
  JTextField atext4=new JTextField(5);
  JLabel Dep5=new JLabel("Department 5");
  JTextField atext5=new JTextField(5); 
  JButton but1=new JButton("Average"); //button 1
  JButton but2=new JButton("Clear Fields"); //button 2

  Font resfont=new Font("Ravie", Font.BOLD,20);
  JLabel average=new JLabel(); //for the action listener  
  Container c;
  FlowLayout flow= new FlowLayout();


  public void init()
  {
    JFrame window=new JFrame();
    Frame c1 = (Frame)this.getParent().getParent();
    c1.setTitle("Hello World");

    c=getContentPane();
    c.setLayout(flow);
    c.setBackground(Color.white);

    Dep1.setForeground(Color.red);
    c.add(Dep1);
    atext1.setForeground(Color.blue);
    c.add(atext1);
    Dep2.setForeground(Color.red);
    c.add(Dep2);    
    atext2.setForeground(Color.blue);
    c.add(atext2);
    Dep3.setForeground(Color.red);
    c.add(Dep3);  
    atext3.setForeground(Color.blue);
    c.add(atext3);
    Dep4.setForeground(Color.red);
    c.add(Dep4);    
    atext4.setForeground(Color.blue);
    c.add(atext4);
    Dep5.setForeground(Color.red);
    c.add(Dep5);
    atext5.setForeground(Color.blue);
    c.add(atext5); 

    but1.setForeground(Color.red);
    c.add(but1);
    but2.setForeground(Color.red);
    c.add(but2);

    average.setFont(resfont);
    average.setForeground(Color.blue);
    c.add(average);

    but1.addActionListener(this);
    but2.addActionListener(this);
  }

  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource() == but1)
    {
      String text1=atext1.getText();
      double number1=Double.parseDouble(text1);      
      String text2=atext2.getText();
      double number2=Double.parseDouble(text2);      
      String text3=atext3.getText();
      double number3=Double.parseDouble(text3);      
      String text4=atext4.getText();
      double number4=Double.parseDouble(text4);      
      String text5=atext5.getText();
      double number5=Double.parseDouble(text5);      
      double average1=((number1+number2+number3+number4+number5)/5);
      average.setText("The average pints of blood donated\n" +  " by the five departments is: "+average1);
      atext1.setEnabled(false);
      atext2.setEnabled(false);
      atext3.setEnabled(false);
      atext4.setEnabled(false);
      atext5.setEnabled(false);
    }
    else if(e.getSource() == but2)
    {

      average.setVisible(false);
      JLabel Dep1=new JLabel("Department 1");
      atext1.setText("");
      JLabel Dep2=new JLabel("Department 2");
      atext2.setText("");
      JLabel Dep3=new JLabel("Department 3");
      atext3.setText("");
      JLabel Dep4=new JLabel("Department 4");
      atext4.setText("");
      JLabel Dep5=new JLabel("Department 5");
      atext5.setText("");
      atext1.setEnabled(true);
      atext2.setEnabled(true);
      atext3.setEnabled(true);
      atext4.setEnabled(true);
      atext5.setEnabled(true);
      atext1.requestFocus();
    }


  }
}