【发布时间】:2016-03-03 20:57:36
【问题描述】:
非常感谢您的帮助。
问题
我们有一个幻想类,上面有一个组合框。从组合框中选择一个编队时,框架会使用按钮进行更新。但是因为当我将动作监听器添加到组合框时,它只会让我在main方法中创建我的类时在幻想类的构造函数中进行,我必须将幻想类和小队类交给TeamController 类所以在创建幻想类时,动作监听器不在组合框中,所以它不起作用。任何帮助将不胜感激。
主类
public class Main {
public static void main(String[] args) {
Fantasy f = new Fantasy();
Squad s = new Squad();
TeamController tc = new TeamController(f, s);
TextController txC = new TextController(s);
f.updateController(tc, txC);
f.setVisible(true);
}
}
幻想类
public class Fantasy extends JFrame {
private JPanel goalPanel;
private JPanel defendPanel;
private JPanel midPanel;
private JPanel attPanel;
private JPanel bench;
private JComboBox formation;
private Button myButton;
private TeamController control;
private TextController tControl;
private List < Button > buttons;
public Fantasy() {
super("Fantasy Football");
buttons = new ArrayList < Button > ();
createWidgets();
}
public void createWidgets() {
JPanel formPanel = new JPanel(new GridLayout(1, 0));
goalPanel = new JPanel(new FlowLayout());
defendPanel = new JPanel(new FlowLayout());
midPanel = new JPanel(new FlowLayout());
attPanel = new JPanel(new FlowLayout());
bench = new JPanel(new FlowLayout());
JComboBox formation = new JComboBox();
String[] addFormation = {
"4 - 4 - 2", "4 - 3 - 3", "3 - 5 - 2", "5 - 3 - 2", "3 - 4 - 3", "4 - 5 - 1"
};
for (String newForm: addFormation) {
formation.addItem(newForm);
}
formation.addActionListener(control);
setLayout(new GridLayout(6, 0));
add(formPanel);
formPanel.add(formation);
add(goalPanel);
add(defendPanel);
add(midPanel);
add(attPanel);
add(bench);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public JComboBox getForm() {
return formation;
}
public JPanel getGoal() {
return goalPanel;
}
public JPanel getDef() {
return defendPanel;
}
public JPanel getMid() {
return midPanel;
}
public JPanel getAtt() {
return attPanel;
}
public JPanel getBench() {
return bench;
}
public void createDefender(String text, String ID) {
addButtons(text, ID, defendPanel);
}
public void createMid(String text, String ID) {
addButtons(text, ID, midPanel);
}
public void createAtt(String text, String ID) {
addButtons(text, ID, attPanel);
}
public void addButtons(String text, String ID, JPanel panel) {
myButton = new Button(text, ID);
myButton.getButton().addActionListener(control);
myButton.getText().addTextListener(tControl);
buttons.add(myButton);
panel.add(myButton);
}
public void updateController(TeamController control, TextController tControl) {
this.control = control;
this.tControl = tControl;
}
public List < Button > getButtons() {
return buttons;
}
}
ActionListner - TeamController 类
public class TeamController implements ActionListener {
private Fantasy fantasy;
private Squad squad;
private JComboBox form;
private ArrayList < Button > button;
public TeamController(Fantasy fantasy, Squad squad) {
this.fantasy = fantasy;
this.form = fantasy.getForm();
this.button = (ArrayList < Button > ) fantasy.getButtons();
this.squad = squad;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == form) {
comboChooser((JComboBox) e.getSource());
} else {
fileChooser((Button) e.getSource());
}
}
public void comboChooser(JComboBox form) {
//restart the pane
fantasy.getContentPane().removeAll();
fantasy.createWidgets();
//add the goalie buttons and labels in
fantasy.addButtons("GoalKeeper", squad.getAllGoal().get(0).getID(), fantasy.getGoal());
//2. break it up into each def mid att
//JComboBox format = form;
String[] split = ((String) form.getSelectedItem()).split("-");
int[] splits = new int[3];
for (int i = 0; i < split.length; i++) {
splits[i] = Integer.parseInt(split[i].trim());
}
//3. create number of widgets for each posit in each panel
//4. add leftover to bench
makeDefender(splits[0], squad.getAllDef());
makeMid(splits[1], squad.getAllMid());
makeAttack(splits[2], squad.getAllAtt());
fantasy.pack();
}
public void fileChooser(Button button) {
final JFileChooser fileChooser = new JFileChooser("C:\\Users\\Michael\\Documents\\Java WorkSpace\\ExerciseThree\\Minor Piece of Coursework 3 Resources\\squad");
int getVal = fileChooser.showOpenDialog(button.getButton());
if (getVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
button.getButton().setVisible(false);
button.add(button.updateButton(file), BorderLayout.CENTER);
Player selectedPlayer = squad.getByID(button.getName());
selectedPlayer.setPath(file.getPath());
String playerName = file.getName();
if (playerName.endsWith(".jpg")) {
playerName.substring(0, playerName.length() - 4);
selectedPlayer.setName(playerName);
}
button.getText().setText(playerName);
} else {
System.out.println("Cancelled your choice");
}
}
public void makeDefender(int number, List < Defender > list) {
for (int i = 0; i < number; i++) {
String ID = list.get(i).getID();
fantasy.createDefender("Defender", ID);
}
}
public void makeMid(int number, List < Midfielder > list) {
for (int i = 0; i < number; i++) {
String ID = list.get(i).getID();
fantasy.createMid("Midfielder", ID);
}
}
public void makeAttack(int number, List < Striker > list) {
for (int i = 0; i < number; i++) {
String ID = list.get(i).getID();
fantasy.createAtt("Striker", ID);
}
}
public void updateSquad(Squad squad) {
this.squad = squad;
}
}
【问题讨论】:
-
在你的
Fantasy类中提供一个addActionListener方法,当它的构造函数被调用时,它可以被你的TeamController调用,它注册了JComboBox -
@MadProgrammer 我正在使用严格的 MVC。所以我将无法在我的视图中使用控制器
-
我没有要求你将控制器传递到视图中,我要求你将
ActionListener传递给视图,以便它可以生成执行ActionListener的事件(控制器)可以做出反应,这通常与您最初尝试做的事情没有什么不同。如果您不能在视图中使用控制器,那么private TextController tControl;在Fantasy类中做了什么?我的解决方案会让你摆脱这个 -
Swing 是 MVC 的一种形式(尽管是 M-VC),所以包装另一个 MVC 实现会让您头疼不已。相反,请关注每一层提供的功能,例如,您的视图会生成一个“formation changed”事件,您的控制器需要对此做出反应。控制器实际上并不关心它是如何生成的,只是它可以是,事实上,您的控制器根本不应该关心 UI 或 UI 组件,只需要维护它和视图之间的一些可定义的联系
-
@MadProgrammer 对不起,真的不明白。你想让我在幻想中创建一个新的 actionlistner,如果是,你想让它做什么?是否用于获取选定的组合框值并将其交给团队控制器?
标签: java swing jframe actionlistener jcombobox