0
我有一個擴展的JFrame和實現的ActionListener,當一類稱爲幀:問題與構造函數調用或JFrame的
if(e.getSource() == addSpell){
ArrayList<Spell> temp = new ArrayList<Spell>();
for(Spell spell: SpellList.getSpellList()){
if(spell.getType().equals(user.getType())){
temp.add(spell);
}
new SpellArraySorter(temp, this);
this.setVisible(false);
}
叫,無論是新SpellArraySorter()被調用6次,或構造函數創建6個jFrames 。
public class SpellArraySorter implements ActionListener{
private JButton help;
private JButton enter;
private JFrame frame;
private JTextField level;
private JTextField levelfill;
private JTextField name;
private JTextField namefill;
private JTextField dmg1;
private JTextField dmg2;
private JTextField dmgfill1;
private JTextField dmgfill2;
private Frame f;
private ArrayList<Spell> changedSpellArray;
public SpellArraySorter(ArrayList<Spell> spellArray, Frame f){
this.f = f;
changedSpellArray = new ArrayList<Spell>(spellArray);
frame = new JFrame();
level = new JTextField();
levelfill = new JTextField();
name = new JTextField();
namefill = new JTextField();
dmg1 = new JTextField();
dmg2 = new JTextField();
dmgfill1 = new JTextField();
dmgfill2 = new JTextField();
help = new JButton("HELP");
enter = new JButton("enter");
frame.setLayout(null);
frame.getContentPane().setBackground(Color.BLACK);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setPreferredSize(new Dimension(300, 300));
frame.pack();
frame.setLocationRelativeTo(null);
level.setBounds(30, 35, 100, 20);
levelfill.setBounds(180, 35, 100, 20);
name.setBounds(30, 90, 100, 20);
namefill.setBounds(180, 90, 100, 20);
dmg1.setBounds(30, 145, 100, 20);
dmg2.setBounds(200, 145, 20, 20);
dmgfill1.setBounds(180, 145, 20, 20);
dmgfill2.setBounds(220, 145, 20, 20);
level.setText("Level of Spell: ");
name.setText("name of Spell: ");
dmg1.setText("Damage of Spell: ");
dmg2.setText("d");
level.setEditable(false);
name.setEditable(false);
dmg1.setEditable(false);
dmg2.setEditable(false);
help.setBounds(30, 215, 100, 20);
enter.setBounds(180, 215, 100, 20);
help.addActionListener(this);
enter.addActionListener(this);
frame.add(level);
frame.add(levelfill);
frame.add(name);
frame.add(namefill);
frame.add(dmg1);
frame.add(dmg2);
frame.add(dmgfill1);
frame.add(dmgfill2);
frame.add(help);
frame.add(enter);
frame.setVisible(true);
}
我不能爲我的生命弄清楚爲什麼,但每次我點擊Frame類的GUI的addSpell JButton的,我有6個JFrames出現類SpellArraySorter的。
我不小心刪除右括號,並錯過了至少三四次的錯誤。謝謝! – Deek3117