Nhờ mọi người giải thích về framework

Em chưa học java swing mà em có bài tập kết thúc môn về làm giao diện mô phỏng các thuật toán sắp xếp. Nên e nhờ mọi người giải thích giùm em bài code này với ạ, và để làm được code như thế này thì e học những gì ạ? Thời gian hơi ít nên e chỉ học cái chính để làm thôi ạ. Em cảm ơn.

public class FormMain extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JLabel lbTitle;
	private JPanel pnImitiate;
	private JPanel pnArray;
	private JButton btnCreateArray;
	private JButton btnRandom;
	private boolean isIncrease = true;
	private JButton btnSort;
	public int num;
	private JLabel[] lbArrays;
	private int[] array;
	private static FormMain frame;
	private Thread[] threads = new Thread[1000000];
	private int curT = -1;
	private int time = 50;
	private int step = 0;
	private int[] lbI = new int[50];
	private int[] lbJ = new int[50];
	private int[] oriLocat = new int[15];
	private int[] lbL = new int[50];
	private int[] lbR = new int[50];
	private JLabel lbPoint1 = new JLabel();
	private JLabel lbPoint2 = new JLabel();
	private JLabel lbPointM = new JLabel();

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				setLockAndFeel();
				try {
					frame = new FormMain();
					frame.setExtendedState(JFrame.MAXIMIZED_BOTH); // Ch?nh Full m�n h?nh
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public FormMain() {
		setBackground(SystemColor.inactiveCaption);
		setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
		setTitle("Quick Sort by Hieu_Nghia");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1366, 742);
		contentPane = new JPanel();
		contentPane.setBackground(SystemColor.menu);
		contentPane.setBorder(new EmptyBorder(15, 15, 15, 15));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lbTitle = new JLabel("QUICK SORT");
		lbTitle.setBackground(SystemColor.menu);
		lbTitle.setBounds(83, 10, 1347, 28);
		lbTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lbTitle.setFont(new Font("Tahoma", Font.BOLD, 23));
		contentPane.add(lbTitle);

		pnImitiate = new JPanel();
		pnImitiate.setBackground(SystemColor.menu);
		pnImitiate.setBorder(new TitledBorder(
				new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)),
				"Demo quick sort", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		pnImitiate.setBounds(100, 48, 1352, 355);
		contentPane.add(pnImitiate);
		pnImitiate.setLayout(null);

		lbPoint1.setSize(50, 25);
		lbPoint1.setOpaque(true);
		lbPoint1.setLocation(50, 50);
		lbPoint1.setFont(new Font("Helvetica", Font.BOLD, 17));
		lbPoint1.setBackground(SystemColor.menu);
		lbPoint1.setHorizontalAlignment(SwingConstants.CENTER);
		lbPoint1.setVerticalAlignment(SwingConstants.CENTER);
		pnImitiate.add(lbPoint1);
		pnImitiate.add(lbPoint2);
		lbPoint2.setSize(50, 25);
		lbPoint2.setOpaque(true);
		lbPoint2.setLocation(50, 50);
		lbPoint2.setFont(new Font("Helvetica", Font.BOLD, 17));
		lbPoint2.setBackground(SystemColor.menu);
		lbPoint2.setHorizontalAlignment(SwingConstants.CENTER);
		lbPoint2.setVerticalAlignment(SwingConstants.CENTER);
		pnImitiate.add(lbPointM);
		lbPointM.setSize(50, 25);
		lbPointM.setOpaque(true);
		lbPointM.setLocation(50, 50);
		lbPointM.setFont(new Font("Helvetica", Font.BOLD, 17));
		lbPointM.setBackground(SystemColor.menu);
		lbPointM.setHorizontalAlignment(SwingConstants.CENTER);
		lbPointM.setVerticalAlignment(SwingConstants.CENTER);

		pnArray = new JPanel();
		pnArray.setBounds(431, 428, 666, 267);
		contentPane.add(pnArray);
		pnArray.setBackground(SystemColor.menu);
		pnArray.setBorder(new TitledBorder(
				new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)),
				"Ch\u1EE9c n\u0103ng", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));

		btnCreateArray = new JButton("Tạo Mảng");
		btnCreateArray.setBackground(SystemColor.activeCaption);
		btnCreateArray.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				createArrays();
			}
		});
		btnRandom = new JButton("Ngẫu Nhiên");
		btnRandom.setBackground(SystemColor.activeCaption);
		btnRandom.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createRandom();
			}
		});

		btnSort = new JButton("Sắp xếp");
		btnSort.setBackground(SystemColor.activeCaption);
		btnSort.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!isSorted()) {
					setState(3);
					for (int i = 0; i < num; i++)
						lbArrays[i].setForeground(Color.BLUE);
					QuickSort();
				}
			}
		});
		GroupLayout gl_pnArray = new GroupLayout(pnArray);
		gl_pnArray.setHorizontalGroup(gl_pnArray.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_pnArray.createSequentialGroup().addGap(64)
						.addComponent(btnCreateArray, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED, 86, Short.MAX_VALUE)
						.addComponent(btnRandom, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE).addGap(83)
						.addComponent(btnSort, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
						.addGap(61)));
		gl_pnArray.setVerticalGroup(gl_pnArray.createParallelGroup(Alignment.LEADING).addGroup(gl_pnArray
				.createSequentialGroup().addGap(77)
				.addGroup(gl_pnArray.createParallelGroup(Alignment.BASELINE).addGap(70)
						.addComponent(btnCreateArray, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnSort, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnRandom, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE))
				.addContainerGap(136, Short.MAX_VALUE)));

		pnArray.setLayout(gl_pnArray);
		setState(0);
	}

	public static void setLockAndFeel() {
		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (Exception e) {

		}
	}

	public void setState(int state) {
		switch (state) {

		case 1: // created arrays, be waiting to set value arrays.

			btnRandom.setEnabled(true);
			break;

		case 2: // be set values, ready to sort
			btnRandom.setEnabled(true);
			btnSort.setEnabled(true);

			break;
		case 3: // sorting
			btnCreateArray.setEnabled(true);
			btnRandom.setEnabled(false);
			btnSort.setEnabled(false);

			break;

		case 4: // sort done
			btnCreateArray.setEnabled(true);
			btnRandom.setEnabled(true);
			btnSort.setEnabled(true);

			break;
		default:
			btnCreateArray.setEnabled(true);
			btnRandom.setEnabled(false);
			btnSort.setEnabled(false);

		}
	}

	public void createArrays() {

		// delete previous arrays and set number elements of array
		num = 10;

		lbArrays = new JLabel[num];
		array = new int[num];

		for (int i = 0; i < num; i++) {

			// create label, set text "0"
			lbArrays[i] = new JLabel("0");
			array[i] = 0;
			pnImitiate.add(lbArrays[i]);
			lbArrays[i].setText(String.valueOf(array[i]));

			// set size label
			lbArrays[i].setSize(50, 50);
			lbArrays[i].setOpaque(true);
			lbArrays[i].setForeground(Color.blue);

			// set location label
			if (i == 0)
				lbArrays[i].setLocation(((int) ((18 - num) * 0.5) * 70) + 100, 150);
			else
				lbArrays[i].setLocation(lbArrays[i - 1].getX() + 70, 150);

			// set fonts
			lbArrays[i].setFont(new Font("Tahoma", Font.PLAIN, 30));

			// set background color
			lbArrays[i].setBackground(SystemColor.inactiveCaption);
			// set text alignment center
			lbArrays[i].setHorizontalAlignment(SwingConstants.CENTER);
			lbArrays[i].setVerticalAlignment(SwingConstants.CENTER);
		}

		pnImitiate.add(lbPoint1);
		pnImitiate.add(lbPoint2);
		pnImitiate.add(lbPointM);

		pnImitiate.setVisible(true);
		pnImitiate.validate();
		pnImitiate.repaint();
		setState(1);
	}

	public void createRandom() {
		Random rand = new Random();
		for (int i = 0; i < num; i++) {
			int ranNum = rand.nextInt(101) + 0;
			lbArrays[i].setText(String.valueOf(ranNum));
			lbArrays[i].setForeground(Color.BLUE);
			array[i] = ranNum;
		}
		pnImitiate.setVisible(true);
		pnImitiate.validate();
		pnImitiate.repaint();
		setState(2);
	}

	public void Swap(JLabel lb1, JLabel lb2) {
		int x1 = lb1.getX();
		int x2 = lb2.getX();
		curT++;

		int cur = curT;
		threads[cur] = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					if (cur != 0) {
						threads[cur - 1].join();
					}
					while (lb1.getY() > 100) {
						lb1.setLocation(lb1.getX(), lb1.getY() - 10);
						lb2.setLocation(lb2.getX(), lb2.getY() + 10);
						lbPointM.setLocation(x2, lbPointM.getY() + 10);
						Thread.sleep(time);
					}
					while (lb1.getX() < x2) {
						lb1.setLocation(lb1.getX() + 10, lb1.getY());
						lb2.setLocation(lb2.getX() - 10, lb2.getY());
						lbPointM.setLocation(lb2.getX(), 250);
						Thread.sleep(time);
					}
					while (lb1.getY() < 140) {
						lb1.setLocation(lb1.getX(), lb1.getY() + 10);
						lb2.setLocation(lb2.getX(), lb2.getY() - 10);
						lbPointM.setLocation(x1, lbPointM.getY() - 10);
						Thread.sleep(time);
					}
					String txtLb1 = lb1.getText();
					lb1.setText(lb2.getText());
					lb2.setText(txtLb1);
					lb1.setLocation(x1, 150);
					lb2.setLocation(x2, 150);
					lb1.setBackground(SystemColor.inactiveCaption);
					lb2.setBackground(SystemColor.inactiveCaption);
				} catch (Exception e) {
				}
			}
		});
		threads[cur].start();
	}

	public void setlbPoint(JLabel lbPoint, int i, String s) {
		curT++;
		System.out.println(curT);
		int cur = curT;
		threads[cur] = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					if (cur != 0)
						threads[cur - 1].join();
					if (i == -1) {
						lbPoint.setText("");
						return;
					}
					if (s.charAt(0) == 'm') {
						lbPoint.setLocation(lbArrays[i].getX(), 200);
						lbPoint.setText(s);
					} else if (s.charAt(0) == 'k') {
						lbPoint.setLocation(oriLocat[i], 200);
						lbPoint.setText(s + i);
					} else {
						lbPoint.setLocation(lbArrays[i].getX(), 275);
						lbPoint.setText(s + i);
					}
				} catch (Exception e) {
				}
			}
		});
		threads[cur].start();
	}

	public void stopAllThreads() {
		for (int i = 0; i < curT; i++) {
			try {
				threads[i].interrupt();
			} catch (Exception e) {

			}
		}
		curT = -1;
	}

	public void QuickSort() {
		QuickSortAl(0, num - 1);
		QuickSortAnimation();
		step = 0;
	}

	public void Coloring(JLabel lb1, Color c) {
		curT++;
		System.out.println(curT);
		int cur = curT;
		threads[cur] = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					if (cur != 0)
						threads[cur - 1].join();
					lb1.setBackground(c);
					Thread.sleep(time);
				} catch (Exception e) {
				}
			}
		});
		threads[cur].start();
	}

	public void Coloring(int left, int right, Color c) {
		curT++;
		System.out.println(curT);
		int cur = curT;
		threads[cur] = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					if (cur != 0)
						threads[cur - 1].join();
					int i = left;
					while (i <= right) {
						if (i != (left + right) / 2)
							lbArrays[i].setBackground(c);
						i++;
					}
					Thread.sleep(time);
				} catch (Exception e) {
				}
			}
		});
		threads[cur].start();
	}

	public void QuickSortAnimation() {
		int s, i, j;
		for (s = 0; s < step; s++) {
			i = lbI[s];
			j = lbJ[s];
			setlbPoint(lbPoint1, i, "i = ");
			setlbPoint(lbPoint2, j, "j = ");
			if (i != j) {
				Swap(lbArrays[i], lbArrays[j]);
			}
			if (lbL[s + 1] + lbR[s + 1] != lbL[s] + lbR[s]) {
				Coloring(lbArrays[(lbL[s] + lbR[s]) / 2], SystemColor.inactiveCaption);
				Coloring(lbL[s], lbR[s], SystemColor.inactiveCaption);
			}
		}
	}

	public void QuickSortAl(int left, int right) {
		if (isIncrease) {
			int i, j, x;
			x = array[(left + right) / 2];
			i = left;
			j = right;
			while (i < j) {
				while (array[i] < x) {
					i++;
				}
				while (array[j] > x) {
					j--;
				}
				if (i <= j) {
					if (array[i] != array[j]) {
						int temp = array[i];
						array[i] = array[j];
						array[j] = temp;
						if (i != j) {
							lbL[step] = left;
							lbR[step] = right;
							lbI[step] = i;
							lbJ[step] = j;
							step++;
						}
					}
					i++;
					j--;
				}
			}
			if (left < j)
				QuickSortAl(left, j);
			if (i < right)
				QuickSortAl(i, right);
		} else {
			int i, j, x;
			x = array[(left + right) / 2];
			i = left;
			j = right;
			while (i < j) {
				while (array[i] > x) {
					i++;
				}
				while (array[j] < x) {
					j--;
				}
				if (i <= j) {
					if (array[i] != array[j]) {
						int temp = array[i];
						array[i] = array[j];
						array[j] = temp;
						if (i != j) {
							lbL[step] = left;
							lbR[step] = right;
							lbI[step] = i;
							lbJ[step] = j;
							step++;
						}
					}
					i++;
					j--;
				}
			}
			if (left < j)
				QuickSortAl(left, j);
			if (i < right)
				QuickSortAl(i, right);
		}
	}

	public void setArray(int[] array) {

		num = 10;

		lbArrays = new JLabel[num];
		this.array = array;

		for (int i = 0; i < num; i++) {

			// create label, set text "0"
			lbArrays[i] = new JLabel(String.valueOf(array[i]));
			pnImitiate.add(lbArrays[i]);

			// set size label
			lbArrays[i].setSize(50, 50);
			lbArrays[i].setOpaque(true);
			lbArrays[i].setForeground(Color.BLUE);

			// set location label
			if (i == 0)
				lbArrays[i].setLocation(((int) ((18 - num) * 0.5) * 70) + 100, 150);
			else
				lbArrays[i].setLocation(lbArrays[i - 1].getX() + 70, 150);

			// set fonts
			lbArrays[i].setFont(new Font("Tahoma", Font.PLAIN, 30));

			// set background color
			lbArrays[i].setBackground(SystemColor.inactiveCaption);

			// set text alignment center
			lbArrays[i].setHorizontalAlignment(SwingConstants.CENTER);
			lbArrays[i].setVerticalAlignment(SwingConstants.CENTER);
		}
		pnImitiate.setVisible(true);
		pnImitiate.validate();
		pnImitiate.repaint();
		setState(2);
	}

	public boolean isSorted() {
		if (isIncrease) {
			for (int i = 0; i < array.length - 1; i++)
				if (array[i] > array[i + 1])
					return false;
		} else {
			for (int i = 0; i < array.length - 1; i++)
				if (array[i] < array[i + 1])
					return false;
		}
		return true;
	}

}

Chưa học Swing mà bắt làm cái này, đùa à. Hay đề yêu cầu làm trên Console (Sytem.out).

Mới học, làm giao diện bình thường đã khó, đằng này lại mô phỏng thao tác sắp xếp. Dùng đến đa luồng (Multi-thread) nữa chứ.

Bài này bạn tham khảo trên mạng à?

4 Likes

bài này bài tham khảo của khóa trước a. Đề là mô phỏng bằng graphic interface.

Bài này tham khảo của khoá trước.
Khoá trước lại tham khảo của khoá trước nữa.
Khoá trước nữa thì thuê người code giá rẻ cho sinh viên…

Vòng lặp luẩn quẩn :joy:

3 Likes

code này hơn 1500 dòng mà cố e tối giản còn hơn 500 dòng, để có hi vọng làm được đó ạ. Nhưng tìm tài liệu học trên mạng thì họ chỉ hướng dẫn swing cơ bản. Mọi người cho em hỏi học gì để làm hoặc để hiểu code cũng được
:smiley:

không biết gì mà làm cái này là chết chắc.
giờ có nước học căn bản, đọc nhiều code của người khác. chịu khó chạy debug để hiểu được code người ta viết.
không ai giúp được em điều này. Từ em làm theo những bước trên thôi.

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?