2016-10-03 18 views
0

私は3つのテキストフィールドに入力された情報を取り込み、ボタンを押した後にテキストエリアにアスリートの情報(名字、名字、スポーツ)を表示するGUIをJavaで作成しようとしています。私はすべてのJlabels、Jtextfields、JtextArea、およびJbuttonでJFrameを作成することができましたが、入力したテキストを適切なアスリートフォーマットに配置する方法と、上記のダイアログボックスを追加する方法を理解することができませんテキストフィールドがすべて満たされていない場合は 'athlete added'を提出し、テキストフィールドがすべて満たされていない場合は 'すべてのカテゴリを記入してください'とします。私は以下のコードを掲載します。どんなヘルプ/ガイダンスも高く評価されます。ここでjava GUIアクションが実行され、ButtonListenerが

は、セットアップクラスである:ここで

import javax.swing.*; 
import java.util.*; 

public class Setup extends JApplet 
{ 

    private int APPLET_WIDTH = 500, APPLET_HEIGHT = 400; 

    private JTabbedPane tPane; 
    private CreatePanel createPanel; 
    private CountPanel countPanel; 
    private Vector athleteList; 

    //The method init initializes the Applet with a Pane with two tabs 
    public void init() 
    { 
    //list of athletes to be used in every panel 
    athleteList = new Vector(); 

    //register panel uses the list of athletes 
    countPanel = new CountPanel(athleteList); 

    createPanel = new CreatePanel(athleteList, countPanel); 

    //create a tabbed pane with two tabs 
    tPane = new JTabbedPane(); 
    tPane.addTab("Athlete Creation", createPanel); 
    tPane.addTab("Medal Count", countPanel); 

    getContentPane().add(tPane); 
    setSize (APPLET_WIDTH, APPLET_HEIGHT); //set Applet size 
    } 
} 

はアスリートクラスです:

public class Athlete 
{ 
    private String firstName, lastName; 
    private String sport; 
    private int gold, silver, bronze; 

    //Constructor to initialize all member variables 
    public Athlete() 
    { 
     firstName = "?"; 
     lastName = "?"; 
     sport = "?"; 
     gold = 0; 
     silver = 0; 
     bronze = 0; 
    } 

    //Accessor methods 
    public String getFirstName() 
    { 
     return firstName; 
    } 

    public String getLastName() 
    { 
     return lastName; 
    } 

    public String getSport() 
    { 
     return sport; 
    } 

    //Mutator methods 
    public void setFirstName(String first) 
    { 
    firstName = first; 
    } 

    public void setLastName(String last) 
    { 
    lastName = last; 
    } 

    public void setSport(String someSport) 
    { 
    sport = someSport; 
    } 

    //Methods to increase the count of medals 
    public void increaseGold() 
    { 
    gold++; 
    } 

    public void increaseSilver() 
    { 
    silver++; 
    } 

    public void increaseBronze() 
    { 
    bronze++; 
    } 

    //toString() method returns a string containg information of an athlete 
    public String toString() 
    { 
     String result = "Name:\t" + lastName + "," + firstName + "\n" 
        + "Sport:\t" + sport + "\n" 
        + "Medal Count:\n" 
        + "Gold: " + gold + "\n" 
        + "Silver: " + silver + "\n" 
        + "Bronze: " + bronze + "\n\n"; 
     return result; 
    } 
    } 

そして、ここでは、私はアスリートの作成パネルでの実験をしてきたものです:

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

public class CreatePanel extends JPanel 
{ 
    private Vector athleteList; 
    private CountPanel cPanel; 
    private int count; 
    private JTextField textField; 
    private JTextField textField_1; 
    private JTextField textField_2; 

//Constructor initializes components and organize them using certain layouts 
public CreatePanel(Vector athleteList, CountPanel cPanel) 
    { 
    count = 0; 

    this.setAthleteList(athleteList); 
    this.cPanel = cPanel; 
    setLayout(null); 

    JTextArea textArea = new JTextArea("No Athlete"); 
    textArea.setBounds(252, 16, 183, 268); 
    add(textArea); 

    JLabel lblFirstName = new JLabel("First Name"); 
    lblFirstName.setBounds(15, 100, 89, 20); 
    add(lblFirstName); 

    JLabel lblLastName = new JLabel("Last Name"); 
    lblLastName.setBounds(15, 135, 89, 20); 
    add(lblLastName); 

    JLabel lblSport = new JLabel("Sport"); 
    lblSport.setBounds(15, 170, 69, 20); 
    add(lblSport); 

    textField = new JTextField(); 
    textField.setBounds(120, 100, 125, 25); 
    add(textField); 
    textField.setColumns(10); 

    textField_1 = new JTextField(); 
    textField_1.setBounds(120, 135, 125, 25); 
    add(textField_1); 
    textField_1.setColumns(10); 

    textField_2 = new JTextField(); 
    textField_2.setBounds(120, 170, 125, 25); 
    add(textField_2); 
    textField_2.setColumns(10); 

    JButton button1 = new JButton("Create an Athlete"); 
    button1.addActionListener(new ButtonListener() { 
     public void actionPerformed(ActionEvent arg0) { 
     } 
    }); 
    button1.setBounds(48, 206, 155, 29); 
    add(button1); 

    } 

//ButtonListener is a listener class that listens to 
//see if the button "Create an Athlete" is pushed. 
//When the event occurs, it adds an athlete using the information 
//entered by a user. 
//It also performs error checking. 
private class ButtonListener implements ActionListener 
    { 
    public void actionPerformed(ActionEvent event) 
    { 

     //TO BE COMPLETED 
     //p1 = ((Object) event).getAthlete(); 

     textArea.setText(textArea.getText() + "/n" 
       + textField.getText() 
       + textField_1.getText() 
       + textField_2.getText()); 

     count++; 
     athleteList.setSize(count); 

     String firsttf = textField.getText(); 
     String lasttf = textField_1.getText(); 
     String sporttf = textField_2.getText(); 

     //Athlete.setFirstName(firsttf); 

     JDialog success = new JDialog(success, "athlete added."); 
     success.setBounds(15, 50, 80, 20); 

     JDialog fail = new JDialog(fail, "Please enter all fields."); 
     fail.setBounds(15, 50, 100, 20); 

     success.setVisible(false); 
     fail.setVisible(false); 

     if (firsttf.length() >= 1 && lasttf.length() >= 1 && sporttf.length() >= 1){ 
      success.setVisible(true); 
      //x = new Athlete(firsttf, lasttf, sporttf); 
     } 

     else if (firsttf.length() < 1 || lasttf.length() < 1 || sporttf.length() < 1){ 
      fail.setVisible(true); 
     } 



    } //end of actionPerformed method 
} //end of ButtonListener class 

private void setAthleteList(Vector athleteList2) { 
    // TODO Auto-generated method stub 

} 
} //end of CreatePanel class 

答えて

0

変更:

これに
button1.addActionListener(new ButtonListener() { 
     public void actionPerformed(ActionEvent arg0) { 
     } 
    }); 

button1.addActionListener(new ButtonListener()); 

あなたがのactionPerformed(...)メソッドはButtonListenerのメソッドをオーバーライドしている現在、コード化されてきた方法です。

関連する問題