0

Necesito ayuda, Mi problema es que tengo un Panel con BorderLayout con paneles en NORTH, SOUTH y CENTER, en el CENTER hay otro Panel con WrapLayout con un JScrollPane, a este le agrego botones y al momento de deslizar estos componentes se pintan sobre los paneles, y queda un efecto super feo, me preguntaba ¿Cual era la forma de eliminar ese comportamiento que le deja el JSrollPane? y ¿Porque sucede?

Cuando utilizo el Touchpad para deslizar es muy notable y se deforma todo, pero cuando arrastro haciendo click en el scroll es solo un parpadeo hasta que lo suelto.

Codigo que tiene problema:

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.beans.Transient;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Test extends JFrame {

    private static final long serialVersionUID = 1L;

    private String title = "Window";
    private int width = 920, height = 700;
        
    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        setTitle(title);
        setSize(width, height);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    
        add(new General());
    
        setVisible(true);
    }

private class General extends JPanel {

    private static final long serialVersionUID = 1L;

    public General() {
        setLayout(new BorderLayout());
    
        add(new Top(), BorderLayout.NORTH);
        add(new JScrollPane(new Center()), BorderLayout.CENTER);
        add(new Bottom(), BorderLayout.SOUTH);
    }    

private class Top extends JPanel {

    private static final long serialVersionUID = 1L;

    public Top() {
        setBackground(Color.RED);
        setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20));
    
    }

    @Override
    @Transient
    public Dimension getPreferredSize() {
        return new Dimension(super.getPreferredSize().width, 50);
    }

}

private class Center extends JPanel {

    private static final long serialVersionUID = 1L;

    public Center() {
        setLayout(new WrapLayout(WrapLayout.LEFT, 20, 20));
        setBackground(Color.PINK);
    
        addArticles(100);
    }

    private void addArticles(int number) {
        for (int i = 0; i < number; i++) {
            add(getTemplateArticle("#"+i));
        } 
    }

    private Button getTemplateArticle(String text) {
        Button button = new Button(text);
        button.setBackground(Color.MAGENTA);
        button.setPreferredSize(new Dimension(200, 200));
        return button;
    }

}

private class Bottom extends JPanel {

    private static final long serialVersionUID = 1L;

    public Bottom() {
        setBackground(Color.BLUE);
    }

    @Override
    @Transient
    public Dimension getPreferredSize() {
        return new Dimension(super.getPreferredSize().width, 20);
    }

}

}

}

Estas son imágenes de como se pintan mal los botones al usar el Scroll:

introducir la descripción de la imagen aquí

introducir la descripción de la imagen aquí

2
  • 1
    Deberias en lo posible evitar mezclar componentes AWT (los botones) con Swing en una misma UI, simplemente porque no se llevan bien; en vez de Buttonusa JButton ;)
    – JohnnyTB
    el 13 may. 2021 a las 0:13
  • Muchas gracias esto soluciono mi problema, no me había dado cuenta que había puesto Button
    – JFrame
    el 13 may. 2021 a las 0:21

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.