Reputation: 3
public class Fantasy extends JFrame {
int di;
int mi;
int si;
public Fantasy() {
setTitle("Fantasy Football");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setSize(700,700);
setResizable(false);
String[] positions = {"4-4-2", "4-3-3", "3-5-2", "5-3-2", "3-4-3", "4-5-1"};
JComboBox select = new JComboBox(positions);
add(select, BorderLayout.NORTH);
select.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
select.getItemAt(select.getSelectedIndex());
Pattern p = Pattern.compile("(\\d)\\-(\\d)\\-(\\d)");
Matcher m = p.matcher(select.toString());
m.find();
di = Integer.parseInt(m.group(1));
mi = Integer.parseInt(m.group(2));
si = Integer.parseInt(m.group(3));
}
});
JPanel ptch = new JPanel();
add(ptch, BorderLayout.CENTER);
JPanel goal = new JPanel();
goal.setLayout(new FlowLayout());
goal.add(createGoalieHolder());
JPanel dfnc = new JPanel();
dfnc.setLayout(new FlowLayout());
dfnc.add(createDefenceHolder());
JPanel midf = new JPanel();
midf.setLayout(new FlowLayout());
midf.add(createMidfHolder());
JPanel strk = new JPanel();
strk.setLayout(new FlowLayout());
strk.add(createStrkHolder());
JPanel bnch = new JPanel();
bnch.setLayout(new FlowLayout());
bnch.add(createBenchHolder());
ptch.add(goal);
ptch.add(dfnc);
ptch.add(midf);
ptch.add(strk);
add(bnch);
setVisible(true);
}
private JPanel createGoalieHolder() {
JPanel gh = new JPanel(new GridLayout(2,1));
JTextField gk = new JTextField("Goalkeeper");
JButton g = new JButton("+");
gh.add(gk,g);
return gh;
}
private JPanel createDefenceHolder() {
JPanel dh = new JPanel(new GridLayout(2, 1));
JTextField df = new JTextField("Defender");
JButton d = new JButton("+");
for (int a = 0; a < di; a++) {
dh.add(df,d);
}
return dh;
}
private JPanel createMidfHolder() {
JPanel mh = new JPanel(new GridLayout(2, 1));
JTextField md = new JTextField("Midfielder");
JButton m = new JButton("+");
for (int b = 0; b < mi; b++) {
mh.add(md,m);
}
return mh;
}
private JPanel createStrkHolder() {
JPanel sh = new JPanel(new GridLayout(2, 1));
JTextField sk = new JTextField("Striker");
JButton s = new JButton("+");
for (int c = 0; c < si; c++) {
sh.add(sk,s);
}
return sh;
}
private JPanel createBenchHolder() {
JPanel gkh = new JPanel(new GridLayout(2,1));
JTextField gkt = new JTextField("Goalkeeper");
JButton gkb = new JButton("+");
gkh.add(gkt,gkb);
for(int ad = 0; ad < (5-di); ad++) {
JPanel dfh = new JPanel(new GridLayout(2,1));
JTextField dft = new JTextField("Defender");
JButton dfb = new JButton("+");
dfh.add(dft,dfb);
return dfh;
}
for(int bd = 0; bd < (5-mi); bd++) {
JPanel mfh = new JPanel(new GridLayout(2,1));
JTextField mft = new JTextField("Midfielder");
JButton mfb = new JButton("+");
mfh.add(mft,mfb);
return mfh;
}
for(int cd = 0; cd < (3-si); cd++) {
JPanel sih = new JPanel(new GridLayout(2,1));
JTextField sit = new JTextField("Striker");
JButton sib = new JButton("+");
sih.add(sit,sib);
return sih;
}
return gkh;
}
}
So, the createHolder methods are supposed to create JPanels with JButtons and JTextFields. Functionality will be added later. However at this point, I cannot make anything really appear on my JFrame, other than a single JTextField with the word "Defender" appear on it. I am not sure how else to proceed.
Upvotes: 0
Views: 39
Reputation: 347204
So, basically, you do this...
add(ptch, BorderLayout.CENTER);
//...
add(bnch);
Which is pretty much the same as doing this...
add(ptch, BorderLayout.CENTER);
//...
add(bnch, BorderLayout.CENTER);
or this...
//add(ptch, BorderLayout.CENTER);
//...
add(bnch, BorderLayout.CENTER);
for all intense purposes.
BorderLayout
will only manage a single component at any of the given positions, meaning that only the bnch
is actually been laid out.
Without knowing exactly what you're trying to do, it's difficult you know what exactly to suggest, but you could start by trying
add(ptch, BorderLayout.CENTER);
//...
add(bnch, BorderLayout.SOUTH);
which should help.
Next...
private JPanel createGoalieHolder() {
JPanel gh = new JPanel(new GridLayout(2, 1));
JTextField gk = new JTextField("Goalkeeper");
JButton g = new JButton("+");
gh.add(gk, g);
return gh;
}
The problem here is gh.add(gk, g);
, the second parameter is typically the constraint property for the layout, instead, you should be doing something more like...
private JPanel createGoalieHolder() {
JPanel gh = new JPanel(new GridLayout(2, 1));
JTextField gk = new JTextField("Goalkeeper");
JButton g = new JButton("+");
gh.add(gk);
gh.add(g);
return gh;
}
and that pretty much goes for all your other create methods
You might like to spend some more time having a look at Laying Out Components Within a Container, How to Use BorderLayout and How to Use GridLayout for more details
Another problem you're going to have is...
private JPanel createDefenceHolder() {
JPanel dh = new JPanel(new GridLayout(2, 1));
JTextField df = new JTextField("Defender");
JButton d = new JButton("+");
for (int a = 0; a < di; a++) {
dh.add(df);
dh.add(d);
}
return dh;
}
(I've corrected for the add
method) Components can only reside within a single parent, so the above should be more like
private JPanel createDefenceHolder() {
JPanel dh = new JPanel(new GridLayout(2, 1));
for (int a = 0; a < di; a++) {
JTextField df = new JTextField("Defender");
JButton d = new JButton("+");
dh.add(df);
dh.add(d);
}
return dh;
}
but, when you call this method, di
is 0
and any changes you make to di
won't affect the UI, unless you recall this method and update the UI with the new panel
Upvotes: 3