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;
}
}
因此,createHolder方法应该用JButtons和JTextFields创建JPanels。稍后将添加功能。但是在这一点上,除了单个JTextField上出现“Defender”字样之外,我无法在JFrame上显示任何内容。我不知道该怎么办。
答案 0 :(得分:3)
所以,基本上,你这样做......
add(ptch, BorderLayout.CENTER);
//...
add(bnch);
这与这样做几乎相同......
add(ptch, BorderLayout.CENTER);
//...
add(bnch, BorderLayout.CENTER);
或者这......
//add(ptch, BorderLayout.CENTER);
//...
add(bnch, BorderLayout.CENTER);
用于所有激烈目的。
BorderLayout
只管理任何给定位置的单个组件,这意味着实际上只有bnch
被布局。
如果不确切知道自己要做什么,很难知道具体建议是什么,但你可以先尝试
add(ptch, BorderLayout.CENTER);
//...
add(bnch, BorderLayout.SOUTH);
应该有所帮助。
下一步...
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;
}
这里的问题是gh.add(gk, g);
,第二个参数通常是布局的约束属性,相反,你应该做的更像......
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;
}
这几乎适用于所有其他创建方法
您可能希望花一些时间查看Laying Out Components Within a Container,How to Use BorderLayout和How to Use GridLayout了解更多详情
你将遇到的另一个问题是......
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;
}
(我已经针对add
方法进行了更正)组件只能位于单个父级中,因此上面应该更像
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;
}
但是,当您调用此方法时,di
为0
,您对di
所做的任何更改都不会影响用户界面,除非您回想一下此方法并使用新面板