Java Can';t使jcombox更新JFrame
如果你能帮我一点忙,我将不胜感激 问题 我们有一个带组合框的幻想班。从组合框中选择一个队形时,框架会用按钮更新。但是,因为当我将action listener添加到组合框中时,它只允许我在main方法中创建类时在fantasy类的构造函数中执行,所以我必须将fantasy类和squad类交给Java Can';t使jcombox更新JFrame,java,swing,jframe,actionlistener,jcombobox,Java,Swing,Jframe,Actionlistener,Jcombobox,如果你能帮我一点忙,我将不胜感激 问题 我们有一个带组合框的幻想班。从组合框中选择一个队形时,框架会用按钮更新。但是,因为当我将action listener添加到组合框中时,它只允许我在main方法中创建类时在fantasy类的构造函数中执行,所以我必须将fantasy类和squad类交给TeamController类,因此在创建fantasy类时,action listner不在组合框中,所以它不起作用了。任何帮助都将不胜感激 主类 幻想班 public类扩展JFrame{ 私人JPanel
TeamController
类,因此在创建fantasy类时,action listner不在组合框中,所以它不起作用了。任何帮助都将不胜感激
主类
幻想班
public类扩展JFrame{
私人JPanel goalPanel;
私人JPanel辩护小组;
私人JPanel中面板;
私人JPanel小组;
私人JPanel长椅;
私人JComboBox组;
私人按钮myButton;
私人团队控制器控制;
私有文本控制器;
私有列表<按钮>按钮;
公众幻想(){
超级(“梦幻足球”);
按钮=新阵列列表<按钮>();
createWidgets();
}
公共void createWidgets(){
JPanel formPanel=新的JPanel(新的网格布局(1,0));
goalPanel=newJPanel(newflowLayout());
DefendenPanel=newJPanel(newFlowLayout());
midPanel=newjpanel(newflowlayout());
attPanel=新的JPanel(新的FlowLayout());
工作台=新的JPanel(新的FlowLayout());
JComboBox formation=新JComboBox();
字符串[]addFormation={
"4 - 4 - 2", "4 - 3 - 3", "3 - 5 - 2", "5 - 3 - 2", "3 - 4 - 3", "4 - 5 - 1"
};
for(字符串新形式:addFormation){
形成。附加项(新形式);
}
形成。addActionListener(控制);
setLayout(新的GridLayout(6,0));
添加(formPanel);
formPanel.add(formation);
添加(goalPanel);
增加(专家组);
添加(中间面板);
添加(附件面板);
增加(工作台);
包装();
setDefaultCloseOperation(关闭时退出);
}
公共JComboxGetForm(){
回流形成;
}
公共JPanel getGoal(){
回传门;
}
公共JPanel getDef(){
返回面板;
}
公共JPanel getMid(){
返回中面板;
}
公共JPanel getAtt(){
返回面板;
}
公共JPanel getBench(){
返回台;
}
public void createDefender(字符串文本,字符串ID){
添加按钮(文本、ID、面板);
}
public void createMid(字符串文本,字符串ID){
添加按钮(文本、ID、中间面板);
}
public void createAtt(字符串文本,字符串ID){
添加按钮(文本、ID、附件面板);
}
公共无效添加按钮(字符串文本、字符串ID、JPanel面板){
myButton=新按钮(文本,ID);
myButton.getButton().addActionListener(控件);
myButton.getText().addTextListener(tControl);
按钮。添加(myButton);
面板。添加(myButton);
}
public void updateController(TeamController控件、TextController控件){
这个控制=控制;
this.tControl=tControl;
}
公共列表<按钮>获取按钮(){
返回按钮;
}
}
ActionListner-TeamController类
公共类TeamController实现ActionListener{
私人幻想;
私人小队,;
私人JComboBox表格;
私有数组列表<按钮>按钮;
公共队控制员(幻想队、幻想队){
这个。幻想=幻想;
this.form=fantasy.getForm();
this.button=(ArrayList
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;
}
}
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;
}
}
public interface FantasyView {
public String getFormation();
public void setFormation(String formation); // Maybe throw an IllegalArgumentException or use an object or enum
// Other information which the might be useful to return
public void addFormationChangedObserver(ChangeListener listener);
public void removeFormationChangedObserver(ChangeListener listener);
public JComponent getView();
}
public interface SquadModel {
// Some getters and setters
// Some observers so the controller and get notified by changes and
// update the view accordingly...
}
public interface TeamController {
public FantasyView getView();
public SquadModel getModel();
}
public class DefaultTeamController implements TeamController {
private FantasyView view;
private SquadModel model;
public DefaultTeamController(FantasyView view, SquadModel model) {
this.view = view;
this.model = model;
view.addFormationChangedObserver(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
SquadModel model = getModel();
// update model accordingly
}
});
// Add observers to model...
}
@Override
public FantasyView getView() {
return view;
}
@Override
public SquadModel getModel() {
return model;
}
}
JComboBox formation = new JComboBox();
//...
formation.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fireFormationChanged();
}
});
//...
protected void fireFormationChanged() {
// changeListeners is a simple List of ChangeListener
// this is instance of FantasyView
ChangeEvent evt = new ChangeEvent(this);
for (ChangeListener listener : changeListeners) {
listener.stateChanged(evt);
}
}