help with JFrame program with java write a program to simulate adding an NBA Tea
ID: 3559718 • Letter: H
Question
help with JFrame program with java
write a program to simulate adding an NBA Team to a Basketball court.
Here is an example of the screenshot when running the program:
Here is what I have so far. help me make it look like the screenshot
public class NBATeam {
private String sTeamName;
private int nWin;
private int nLoss;
private String [] playerArray;
public NBATeam (String s, int w, int l){
sTeamName=s; nWin=w; nLoss=l;
}
public NBATeam (String s){
sTeamName=s; nWin=0; nLoss=0;
}
public String getName(){
return sTeamName;
}
public void setName(String s){
sTeamName=s;
}
public int getWinNum(){
return nWin;
}
public String toString(){
String s= sTeamName + "[ ";
for (int i=0;i s+= "] win #: " + nWin + " los : " +nLoss;
return s;
}
public void winAgame(NBATeam teamB){
nWin++;
teamB.lossAgame();
}
private void winAgame(){
nWin++;
}
private void lossAgame(){
nLoss++;
}
public void lossAgame(NBATeam teamB){
nLoss++;
teamB.winAgame();
}
public void addAPlayer(String newPlayerName){
int newSize=0;
if (playerArray==null) newSize=1;
else newSize=playerArray.length + 1;
String [] tmp=new String[newSize];
if(playerArray!=null) {
for (int i=0; i tmp[i]=playerArray[i];
}
tmp[newSize-1]=newPlayerName;
playerArray=tmp;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NBAPlayoff extends JFrame {
private JTextField txtName;
private JTextField txtAge;
private NBATeam spurs;
private NBAcourtPanel court;
private JLabel lMax, lMin, lAvg, lNum;
public NBAPlayoff(){
spurs=new NBATeam("Spurs");
court=new NBAcourtPanel(spurs);
add(court, BorderLayout.CENTER);
JLabel lMax0=new JLabel("Max Age:");
lMax=new JLabel("");
JLabel lMin0=new JLabel("Min Age:");
lMin=new JLabel("");
JLabel lAvg0=new JLabel("Average Age:");
lAvg=new JLabel("");
JLabel lNum0=new JLabel("Number of Players:");
lNum =new JLabel("");
JPanel rp=new JPanel(new GridLayout(8, 1)); //right panel
rp.add(lNum0);rp.add(lNum);rp.add(lMax0);rp.add(lMax);
rp.add(lMin0);rp.add(lMin);rp.add(lAvg0);rp.add(lAvg);
add(rp, BorderLayout.EAST);
JLabel l1=new JLabel("Player Name:");
txtName= new JTextField();
txtName.setPreferredSize(new Dimension(120,24));
JLabel l2=new JLabel("Player Age:");
txtAge= new JTextField();
txtAge.setPreferredSize(new Dimension(120,24));
JButton jbtAdd=new JButton("Add A Player");
jbtAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int age=Integer.parseInt(txtAge.getText());
spurs.addAPlayer(txtName.getText(), age);
lMax.setText(spurs.getMaxAge()+"");
lMin.setText(spurs.getMinAge()+"");
lAvg.setText(spurs.getAvgAge()+"");
lNum.setText(spurs.getNumOfPlayer()+"");
court.repaint();
}});
JButton jbtClear= new JButton("Clear");
jbtClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtName.setText("");
txtAge.setText("");
}});
JPanel pBot=new JPanel();
pBot.add(l1); pBot.add(txtName); pBot.add(l2);pBot.add(txtAge); pBot.add(jbtAdd);pBot.add(jbtClear);
add(pBot, BorderLayout.SOUTH);
}
public static void main(String[] args) {
NBAPlayoff frame = new NBAPlayoff();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(800, 400);
frame.setVisible(true);
}
}
Explanation / Answer
import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class NBAPlayoff extends JFrame implements
ActionListener
{
// Initializing JButton, JPanel, JRadioButton, JTextField, and the class
// ButtonGroup
private JPanel panel1;
private JTextField name, age;
private JButton ok, clear;
private JLabel name1, age1, l1num, l2max, l3min, l4avg,
l1, l2, l3, l4;
int total = 0, size = 0, avg = 0, max = 0, min = 0;
int[] ageArr = new int[size];
// Setting up GUI
public NBAPlayoff()
{
// Setting up the Title of the Window
super("NBA Player Board");
// Set Size of the Window (WIDTH, LENGTH)
setSize(775, 600);
setVisible(true);
// Exit Property of the Window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ok = new JButton("Add a player");
clear = new JButton("Clear");
name1 = new JLabel("Player Name");
age1 = new JLabel("Player Age");
l1num = new JLabel("Number of players");
l2max = new JLabel("Maximum age");
l3min = new JLabel("Minimum age");
l4avg = new JLabel("Average Age");
name = new JTextField(20);
age = new JTextField(20);
panel1 = new JPanel();
panel1.setLayout(new GridBagLayout());
panel1.add(name1);
panel1.add(name);
panel1.add(age1);
panel1.add(age);
panel1.add(ok);
panel1.add(clear);
Container pane = getContentPane();
setContentPane(pane);
GridBagLayout grid = new GridBagLayout();
pane.setLayout(grid);
pane.add(panel1);
ok.addActionListener(this);
setResizable(true);
ok.addActionListener(new ActionListener()
{
// Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event)
{
size += 1;
ageArr[size] = Integer.parseInt(age.getText());
max = ageArr[0];
for (int i = 0; i < size; i++)
if (ageArr[i] > max)
max = ageArr[i];
min = ageArr[0];
for (int i = 0; i < size; i++)
if (ageArr[i] < min)
min = ageArr[i];
for (int i = 0; i < size; i++)
total += ageArr[i];
avg = total / size;
l1 = new JLabel("" + size);
l2 = new JLabel("" + max);
l3 = new JLabel("" + min);
l4 = new JLabel("" + avg);
}
});
panel1.add(l1);
panel1.add(l2);
panel1.add(l3);
panel1.add(l4);
}
@Override
public void actionPerformed(ActionEvent arg0)
{
// TODO Auto-generated method stub
}
public static void main(String args[])
{
NBAPlayoff n = new NBAPlayoff();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.