Olá, estou tentando fazer um JScrollPane na minha tela mas o problema é que aparece o botão na lateral só que ele não faz nenhuma ação, olhem o meu código:
import javax.swing.; //pacote do JFrame
import java.awt.; //pacote do container
public class Qt1View extends JFrame{
//declarações
public static Container ctnQt1;
//imagens
public static ImageIcon imgQt1;
public static JLabel qd1;
public static JLabel descQt1;
//texto
public static JLabel desc1;
//scroll
public static JScrollPane scroll;
public static JPanel painel;
public static JButton btnScroll;
public static Container ctnScroll;
//construtor
public Qt1View(){
//container
ctnQt1= new Container();
ctnQt1.setLayout(null);
this.add(ctnQt1);
//imagem
qd1= new JLabel();
imgQt1= new ImageIcon("imagens/qt1.jpg");
qd1.setBounds(340,60,1280,720);
qd1.setIcon(imgQt1);
ctnQt1.add(qd1);
//texto
desc1= new JLabel ("Quarto 1"); //aqui mais tarde editaremos com a descrição do quarto
desc1.setBounds(340,800,100,100);
desc1.setForeground(Color.white);
desc1.setFont(new Font("Arial",1, 24));
ctnQt1.add(desc1);
//Painel
painel= new JPanel();
// painel.setSize(600,600);
painel.setPreferredSize(new Dimension(600,600));
ctnQt1.add(painel);
//scroll
scroll= new JScrollPane(scroll);
ctnQt1.add(scroll);
//button
btnScroll= new JButton();
btnScroll.setBounds(10,10,20,20);
painel.add(btnScroll);
this.show();
this.getContentPane().add(painel,BorderLayout.EAST);
this.getContentPane().add(btnScroll,BorderLayout.EAST);
this.getContentPane().setBackground(Color.black);
this.setSize(600,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Que ação você esperava que seu JButton fizesse? Seu botão (btnScroll) não tem nenhum ActionListener, então ele não vai fazer nada mesmo.
Obs: a rolagem com um JScrollPane é automática, se é o que você queria. Mas o conteúdo dele precisa ser maior que o espaço disponível visualmente.
Abraço.
Galera, estou com esse novo código e o scroll aparece, só falta fazer ele sair do Painel para interagir com o Container:
import javax.swing.; //pacote do JFrame
import java.awt.; //pacote do container
public class Qt1View extends JFrame{
//declarações
public static Container ctnQt1;
public static Container ctnPainel;
//imagens
public static ImageIcon imgQt1;
public static JLabel qd1;
public static JLabel descQt1;
//texto
public static JLabel desc1;
//scroll
public static JScrollPane scroll;
public static JPanel painel;
public static JButton btnScroll;
public static Container ctnScroll;
public static JLabel imgRandom;
//construtor
public Qt1View(){
//container
ctnQt1= new Container();
ctnQt1.setLayout(null);
this.add(ctnQt1);
ctnPainel= new Container();
ctnPainel.setLayout(new GridLayout(1,1));
ctnQt1.add(ctnPainel,BorderLayout.EAST);
//ctnQt1.add(ctnPainel);
//imagem
qd1= new JLabel();
imgQt1= new ImageIcon("imagens/qt1.jpg");
qd1.setBounds(340,60,1280,720);
qd1.setIcon(imgQt1);
ctnQt1.add(qd1);
//texto
desc1= new JLabel ("Quarto 1"); //aqui mais tarde editaremos com a descrição do quarto
desc1.setBounds(340,800,100,100);
desc1.setForeground(Color.white);
desc1.setFont(new Font("Arial",1, 24));
ctnQt1.add(desc1);
//Painel
painel= new JPanel();
// painel.setSize(600,600);
painel.setPreferredSize(new Dimension(15,600));
painel.setVisible(true);
ctnPainel.add(painel);
//scroll
scroll= new JScrollPane(painel);
//scroll.setPreferredSize(new Dimension(45,600));
ctnPainel.add(scroll);
//button
//btnScroll= new JButton("scroll");
//btnScroll.setBounds(50,50,100,100);
//painel.add(btnScroll);
this.show();
//this.getContentPane().add(painel,BorderLayout.EAST);
this.getContentPane().add(scroll,BorderLayout.EAST);
//this.getContentPane().add(ctnPainel,BorderLayout.EAST);
this.getContentPane().setBackground(Color.black);
this.setSize(600,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}