JScrollPane nie chce się wyświetlić

2015-01-08 19:47
Kokanas
0

Witam

Mam problem z JScrollPane, nie chcę się wyświetlić, dodawałem już na wiele sposobów. Chodzi o to, aby buttonPanel miał suwak.
Z góry dziękuję.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Main implements ActionListener {

    private static final String HOME_CARD = "Home";
    private static final String BLUE_PANEL = "Blue Panel";
    private static final String RED_PANEL = "Red Panel";
    private static final String ORANGE_PANEL = "Orange Panel";

    private JPanel cardHolder;
    private JButton homeButton = new JButton("Home");
    private JButton blueButton = new JButton("Blue Card");
    private JButton redButton = new JButton("Red Panel");
    private JButton orangeButton = new JButton("Orange Panel");
    private JButton test1 = new JButton("Home");
    private JButton test2 = new JButton("Home");
    private JButton test3 = new JButton("Home");
    private JButton test4 = new JButton("Home");
    private JButton test5 = new JButton("Home");
    private JButton test6 = new JButton("Home");
    private JButton test7 = new JButton("Home");

    //JScrollPane scrollpane = new JScrollPane();
      // JScrollPane scrollpane2 = new JScrollPane();

    public JPanel createCardHolderPanel() {
        cardHolder = new JPanel(new CardLayout());
        cardHolder.setBorder(BorderFactory.createTitledBorder("Card Holder Panel"));
        cardHolder.add(createColorPanel(Color.gray), HOME_CARD);
        cardHolder.add(createColorPanel(Color.blue), BLUE_PANEL);
        cardHolder.add(createColorPanel(Color.red), RED_PANEL);
        cardHolder.add(createColorPanel(Color.orange), ORANGE_PANEL);

        return cardHolder;
    }

    private JPanel createColorPanel(Color color) {
        JPanel panel = new JPanel();
        panel.setBackground(color);
        return panel;
    }

    public JPanel createButtonPanel() {
        JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 5, 5));
        buttonPanel.setPreferredSize(new Dimension(100, 288));

        buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));

        JScrollPane scrollPane = new JScrollPane(buttonPanel,   ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setPreferredSize(new Dimension(100, 288));

        buttonPanel.add(homeButton);
        buttonPanel.add(blueButton);
        buttonPanel.add(redButton);
        buttonPanel.add(orangeButton);
        buttonPanel.add(test1);
        buttonPanel.add(test2);
        buttonPanel.add(test3);
        buttonPanel.add(test4);
        buttonPanel.add(test5);
        buttonPanel.add(test6);
        buttonPanel.add(test7);

        homeButton.addActionListener(this);
        blueButton.addActionListener(this);
        redButton.addActionListener(this);
        orangeButton.addActionListener(this);

        return buttonPanel;
    }

    public JPanel createContentPane() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createTitledBorder("Main Content Pane"));
        panel.setBackground(Color.WHITE);

        panel.setPreferredSize(new Dimension(499, 288));
        panel.add(createButtonPanel(), BorderLayout.WEST);
        panel.add(createCardHolderPanel(), BorderLayout.CENTER);

        return panel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
        if (e.getSource() == homeButton) {
            cardLayout.show(cardHolder, HOME_CARD);
        }
        if (e.getSource() == blueButton) {
            cardLayout.show(cardHolder, BLUE_PANEL);
        }
        if (e.getSource() == redButton) {
            cardLayout.show(cardHolder, RED_PANEL);
        }
        if (e.getSource() == orangeButton) {
            cardLayout.show(cardHolder, ORANGE_PANEL);
        }
    }

    public static void createAndShowGUI() {
        JFrame frame = new JFrame("Simple CardLayout Program");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Main main = new Main();
        //frame.setJMenuBar(main.createMenuBar());
        frame.add(main.createContentPane());
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
edytowany 1x, ostatnio: furious programming, 2016-12-13 18:26
Nie używaj prefiksów/postfiksów w tytułach wątków - od podawania nazwy języka/technologii są tagi; - furious programming 2015-01-08 20:42

Pozostało 580 znaków

2015-01-08 21:31
0

Nie widzę w kodzie polecenia wyświetlenia tego JScrollPane. Dodaj ten JScrollPane do okna.


To smutne, że głupcy są tak pewni siebie, a ludzie mądrzy - tak pełni wątpliwości. Bertrand Russell

Pozostało 580 znaków

2015-01-08 21:33
0
public JPanel createContentPane() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createTitledBorder("Main Content Pane"));
        panel.setBackground(Color.WHITE);
        JScrollPane scrollPane = new JScrollPane(createButtonPanel(),   ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setPreferredSize(new Dimension(150, 10));
        panel.setPreferredSize(new Dimension(499, 288));
        panel.add(scrollPane, BorderLayout.WEST);
        panel.add(createCardHolderPanel(), BorderLayout.CENTER);
        return panel;
    }

Pozostało 580 znaków

2015-01-08 22:27
Kokanas
0

garai dzięki wielkie !

Pozostało 580 znaków

Liczba odpowiedzi na stronę

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0