【问题标题】:I'm having trouble setting the title of my applet我无法设置小程序的标题
【发布时间】:2014-07-06 15:27:43
【问题描述】:

我想开始我的说法,我已经尝试了大约 2 个小时来找到如何使用 JApplet 为我的小程序命名。我大约两周前才开始使用 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();
    }


  }
}

【问题讨论】:

  • 欢迎来到 Stack Overflow!请清楚地说明您的问题,并给我们一个最小的代码块来演示您遇到的问题。将几百行代码连同您的问题放在评论中,不太可能得到很多答案。

标签: java applet


【解决方案1】:

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

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


  }
}

【讨论】:

  • 感谢您的回复!但是,我这样做了,当它运行时,它返回了一个没有标题的空白小程序。
  • 我将其复制并粘贴到一个新文件中,但我仍然得到一个空白小程序。我运行我的原件没有任何标题,它运行良好。我正在使用 DRJAVA,这和它有什么关系吗?
  • 好的,谢谢您的帮助!我现在要下载 eclipse luna 看看是否会改变它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-21
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多