Замена местами кнопок в ArrayList JavaSwing
Формулировка задачи:
Всем здравствуйте. делаю игру пятнашки на java Swing.меняю в ArrayList. местами кнопки.Почему не меняются кнопки местами при отображении приложения?
Весь код игры:
- Создаю ArrayList<JButton>
- туда заношу все созданные кнопки со значениями.
- ArrayList<JButton> добавляю в JPanel
- и все это во фрейм. д
- обавляю слушатель.
- запускаю
public void actionPerformed(ActionEvent e) {
event = (JButton) e.getSource();
System.out.println(event.equals(button1));
if (event.equals(button1)) {
int x = button.indexOf(button1);
int y = button.indexOf(button0); //меняю в ArrayList. местами кнопки
button.set(x, button0);
button.set(y, button1);
x = button.indexOf(button1);
y = button.indexOf(button0);
}
}package barley_break_GUI;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Barley_Break_GUI_Start extends JPanel implements
Barley_Break_Constance {
static Barley_Break_GUI_Start start = new Barley_Break_GUI_Start();
JFrame frame = new JFrame();
ArrayList<JButton> button = new ArrayList<JButton>(16);
JPanel panelbutton = new JPanel();
int width = 0;
int height = 0;
public Barley_Break_GUI_Start() {
Barley_Break_GUI_Engine engine = new Barley_Break_GUI_Engine(start,
button);
ActionListener action = new Barley_Break_GUI_Engine(start, button);
button1.addActionListener(action);
}
public static void main(String[] args) {
start.createFrame();
}
public void createFrame() {
start.createDimension();
frame.setSize(width, height);
frame.setTitle("Barley_Break");
Image image = new ImageIcon("icon.gif").getImage();
frame.setIconImage(image);
start.createan();
frame.getContentPane().add(panelbutton, BorderLayout.CENTER);
frame.setLocationByPlatform(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void createDimension() {
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension dim = kit.getScreenSize();
width = dim.width / 4;
height = dim.height / 4;
}
public void createan() {
button.add(button0);
button.get(0).setEnabled(false);
button.add(button1);
button.add(button2);
button.add(button3);
button.add(button4);
button.add(button5);
button.add(button6);
button.add(button7);
button.add(button8);
button.add(button9);
button.add(button10);
button.add(button11);
button.add(button12);
button.add(button13);
button.add(button14);
button.add(button15);
panelbutton.setLayout(new GridLayout(4, 4));
for (int i = 0; i < button.size(); i++) {
panelbutton.add(button.get(i));
}
}
}
package barley_break_GUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
public class Barley_Break_GUI_Engine implements Barley_Break_Constance,
ActionListener {
Barley_Break_GUI_Start start;
ArrayList<JButton> button;
JButton event;
public Barley_Break_GUI_Engine(Barley_Break_GUI_Start start,
ArrayList<JButton> button) {
this.start = start;
this.button = button;
}
@Override
public void actionPerformed(ActionEvent e) {
event = (JButton) e.getSource();
System.out.println(event.equals(button1));
if (event.equals(button1)) {
int x = button.indexOf(button1);
int y = button.indexOf(button0);
button.set(x, button0);
button.set(y, button1);
x = button.indexOf(button1);
y = button.indexOf(button0);
}
}
}
package barley_break_GUI;
import javax.swing.JButton;
public interface Barley_Break_Constance {
JButton button0 = new JButton(" ");
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton button10 = new JButton("10");
JButton button11 = new JButton("11");
JButton button12 = new JButton("12");
JButton button13 = new JButton("13");
JButton button14 = new JButton("14");
JButton button15 = new JButton("15");
} Решение задачи: «Замена местами кнопок в ArrayList JavaSwing»
textual
Листинг программы
package barley_break_GUI;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.util.List;
public class Barley_Break_GUI_Start implements ActionListener {
static Barley_Break_GUI_Start start = new Barley_Break_GUI_Start();
JFrame frame = new JFrame();
List<JButton> buttonList;
JButton button0;
int width = 0;
int height = 0;
public static void main(String[] args) {
start.createFrame();
}
public void createFrame() {
start.createDimension();
frame.setSize(width, height);
frame.setResizable(false);
frame.setTitle("Barley_Break");
Image image = new ImageIcon("icon.gif").getImage();
frame.setIconImage(image);
frame.setLocationByPlatform(true);
start.createButton();
start.addButtonInFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void createDimension() {
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension dim = kit.getScreenSize();
width = dim.width / 4;
height = dim.height / 4;
}
public void createButton() {
buttonList = new ArrayList<JButton>(16);
button0 = new JButton(" ");
button0.setEnabled(false);
for (int i = 1; i < 16; i++) {
JButton button = new JButton(Integer.toString(i));
button.addActionListener(this);
buttonList.add(button);
}
Collections.shuffle(buttonList);
buttonList.add(0, button0);
}
public void addButtonInFrame() {
frame.getContentPane().setLayout(new GridLayout(4, 4));
for (JButton jButton : buttonList) {
frame.getContentPane().add(jButton);
}
frame.getContentPane().doLayout();
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source instanceof JButton) {
JButton sourceButton = (JButton) source;
int indexSource = buttonList.indexOf(sourceButton);
int indexButton0 = buttonList.indexOf(button0);
if (indexSource == (indexButton0 + 1)) {
buttonList.set(indexSource - 1, sourceButton);
buttonList.set(indexSource, button0);
} else if (indexSource == (indexButton0 - 1)) {
buttonList.set(indexSource + 1, sourceButton);
buttonList.set(indexSource, button0);
} else if (indexSource == (indexButton0 - 4)) {
buttonList.set(indexSource + 4, sourceButton);
buttonList.set(indexSource, button0);
} else if (indexSource == (indexButton0 + 4)) {
buttonList.set(indexSource - 4, sourceButton);
buttonList.set(indexSource, button0);
}
}
start.addButtonInFrame();
}
}