2016-10-03 59 views
0

我正在嘗試在Java中製作一個GUI,它接受三個文本字段中輸入的信息並在按下按鈕後在文本區域中顯示運動員的信息(名字,姓氏和運動) 。我能夠用Jlabels,Jtextfields,JtextArea和Jbutton創建JFrame,但我無法弄清楚如何將輸入的文本輸入到正確的運動員格式以及添加上面的某種對話框如果所有文本框都已填充,則提交'添加運動員';如果文本框未全部填充,則提交'請填寫所有類別'。我將發佈我下面的代碼,任何幫助/指導將不勝感激。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方法的方法。