2014-07-06 47 views
0

我想開始說我已經嘗試了大約2個小時來找到如何使用JApplet標題我的小程序。大約兩週前我開始使用GUI。這是我的代碼。我在init()中遇到了問題;任何幫助將是偉大的,謝謝! - 坦納爾我無法設置我的小程序的標題

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

歡迎來到Stack Overflow!請清楚地陳述您的問題,並給我們提供一小段代碼,說明您遇到的問題。在部分評論中刪除幾百行代碼並將其與您的問題進行評論並不太可能爲您提供很多答案。 – user3553031

回答

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


    } 
} 
+0

謝謝你的回覆!但是,我這樣做,當它運行時,它返回了一個沒有標題的空白小程序。 – user3809062

+0

我複製並粘貼到一個新的文件,我仍然有一個空白的小程序。我跑了我的原始沒有任何標題,它運行良好。我正在使用DRJAVA,這與它有什麼關係? – user3809062

+0

好的,謝謝你的幫助!我現在要去下載eclipse luna,看看它是否會改變它。 – user3809062

相關問題