为什么方法 paintComponent 不起作用?爪哇

问题描述 投票:0回答:0

我正在编码选择排序可视化。所以,我遇到了一个问题:当我更改面板时(当我在主菜单中并按下按钮进行可视化时)我希望我的数组被绘制在面板上,但它没有发生。 我不知道在更换面板或绘制阵列时出现什么问题 我是java的新手,所以我希望你能帮助我! 代码:

Main.java

import project.MainMenu;

public class Main {
    public static void main(String[] args) {
        new MainMenu();
    }
}

MainMenu.java

package project;

import java.awt.*;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class MainMenu extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel panel;
    private JFrame frame;
    private JLabel label;
    private JButton vizualiseButton;
    private JLabel description;
    Dimension screen;

    public MainMenu() {
        frame = new JFrame();
        panel = new JPanel();

        GridLayout layout = new GridLayout(0, 1);
        layout.setVgap(10);
        panel.setLayout(layout);
        panel.setBorder(new EmptyBorder(10,10,10,10));

        label = new JLabel("Сортировка выбором");
        label.setFont(label.getFont().deriveFont(36.0f));
        label.setHorizontalAlignment(getX());
        label.setPreferredSize(getPreferredSize());

        description = new JLabel("");
        description.setFont(label.getFont().deriveFont(20.0f));
        description.setPreferredSize(getPreferredSize());
        description.setText("<html><span>Описание какое-то сортировки</span></html>");

        vizualiseButton = new JButton("Посмотреть визуализацию");


        vizualiseButton.addMouseListener(new java.awt.event.MouseAdapter() {
            boolean clicked = false;

            public void mouseEntered(java.awt.event.MouseEvent evt) {
                vizualiseButton.setBackground(new Color(155, 239, 245));
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                if (!clicked)
                    vizualiseButton.setBackground(new Color(255,255,255));
            }
            /// Changing panels. This might be an issue
            public void mousePressed(java.awt.event.MouseEvent evt) {
                clicked = true;
                frame.add(new Sorting());
                panel.setVisible(false);
            }
        });

        JPanel buttonPanel = new JPanel();
        buttonPanel.setSize(50,40);
        buttonPanel.add(vizualiseButton, BorderLayout.CENTER);

        panel.add(label, BorderLayout.NORTH);
        panel.add(description, BorderLayout.CENTER);
        panel.add(buttonPanel, BorderLayout.SOUTH);

        screen = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLayout(new BorderLayout());
        frame.add(panel, BorderLayout.CENTER);
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Сортировка выбором");
        frame.setVisible(true);
    }
}

Sorting.java

package project;

import javax.swing.*;


public class Sorting extends JPanel {
    private JPanel buttonPanel;
    private JButton addNum;
    private JButton startVisualizing;
    private JButton resetArray;
    private int x = 15;
    private int[] arr;
    private int y = 15;

    public Sorting(){
        arr = new int[]{1,2,3};
        /// array visualization
        add(new ArrayVis(arr,x,y));
        x += 10 * arr.length;
        setVisible(true);
    }

    public void selectionSort() {
        for (int i = 0; i < arr.length; i++) {
            int min = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[min]) {
                    min = j;
                }
            }
            int helper = arr[i];
            arr[i] = arr[min];
            arr[min] = helper;
        }
    }
}

ArrayVis.java

package project;

import javax.swing.*;
import java.awt.*;

public class ArrayVis extends JPanel {
    private int[] arr;
    private int x;
    private int y;

    public ArrayVis(int[] arr,int x, int y){
        this.arr = arr;
        this.x = x;
        this.y = y;
        setVisible(true);
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        for (int i = 0;i<arr.length;i++){
            g.drawString(Integer.toString(arr[i]),x,y);
            x += 10;
            g.fillRect(20,20,30,30);
        }
    }
}

java swing awt
© www.soinside.com 2019 - 2024. All rights reserved.