我正在尝试在Java中创建一个GUI,它在三个文本字段中输入信息,并在按下按钮后显示运动员在文本区域中的信息(名字,姓氏和运动)。我能够使用所有Jlabels,Jtextfields,JtextArea和Jbutton创建JFrame,但我无法弄清楚如何获取输入的文本并将其放入正确的运动员格式以及添加上面的某种对话框如果填写了所有文本字段,则提交“运动员添加”;如果文本字段未填满,则“请填写所有类别”。我将发布下面的代码,任何帮助/指导将不胜感激。
这是Setup类:
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 :(得分:0)
改变这个:
button1.addActionListener(new ButtonListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
对此:
button1.addActionListener(new ButtonListener());
当前编码actionPerformed(...)方法的方法是覆盖ButtonListener中的方法。