Boa tarde,
não sou um profundo conhecedor de interfaces gráficas e layout ,e como preciso desenvolver uma aplicação usando swing estou apanhanho um pouco.
Vamos lá :
Eu tenho um JFrame, tenho um panel ( ficarei trocando esse panel usando CardLayout). Esse painel é responsavel por armazenar todos os componentes dessa tela.
Dentro desse painel eu terei um JScrolPane, que por sua vez irá conter um JImagePanel do projeto towel com uma imagem bem grande (por isso o uso do JScrolPane).
O meu problema é que, eu consigo adicionar o panel dentro do JScrolPane porém, as barras de rolagem nao aparecem e eu nao consigo “navegar” pela imagem. Alguem mais experiente consegue ver oq estou fazendo de errado?
segue o código
Construtor do JFrame :
//Construtor do frame
this.cardLayout = new CardLayout();
this.setLayout(null);
this.setResizable(false);
this.setMinimumSize(new Dimension(1280, 720));
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
//panel de background
this.backgroundPanel = new JPanel(this.cardLayout);
this.setContentPane(backgroundPanel);
//panel inicial
JImagePanel panelInicial = new JImagePanel(loadImage("resources/LogoPNG.png"));
panelInicial.setFillType(JImagePanel.FillType.CENTER);
panelInicial.setBackground(Color.WHITE);
panelInicial.setLayout(null);
panelInicial.setMinimumSize(new Dimension(panelInicial.getImage().getWidth(), panelInicial.getImage().getHeight()));
//panel de novo projeto
JPanel newProject = this.createPanelNewProject();
this.backgroundPanel.add(panelInicial, CardEnum.CARD_VAZIO.getCard());
this.backgroundPanel.add(newProject, CardEnum.PAINT.getCard());
Criação do Scroll e do ImagePanel
/**
* Cria o Painel de Visualização de imagem
*/
private JScrollPane createImagePanel() {
BufferedImage image = loadImage("resources/imagem.png");
this.imagePanel = new JImagePanel(image);
imagePanel.setFillType(JImagePanel.FillType.CENTER);
imagePanel.setBounds(300, 250, image.getWidth(), image.getHeight());
//centralizar o painel no scrollPane
imagePanel.setVisible(true);
imagePanel.setLayout(null);
imagePanel.repaint();
JScrollPane scrol = new JScrollPane(imagePanel);
scrol.setBounds(190, 35, 500, 620);
scrol.setVisible(true);
return scrol;
}
Esse é o painel principal do frame ,que ira conter os componentes
/**
* Cria o painel de novo projeto
*/
private JPanel createPanelNewProject() {
JPanel panel = new JPanel();
panel.setBackground(new Color(245, 245, 245));
panel.setBorder(BorderFactory.createTitledBorder(null, "Novo Projeto",
TitledBorder.LEFT, TitledBorder.CENTER, null, new Color(0, 0, 0)));
panel.setLayout(null);
//painel da imagem
JScrollPane projectPanel = this.createImagePanel();
panel.add(projectPanel);
panel.repaint();
panel.setVisible(true);
return panel;
}
Obrigado a todos.