Zmiana paneli w CardLayout bez zapisywania

2015-01-09 17:19
Kokanas
0

Witam

Wywołuje nowy widok za pomocą :

CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.show(contentPane,CARD2);

Coś zrobię w tym CARD2, zmienię tło z koloru czarnego na kolor czerwony.
I powiedzmy, że przełączę się na inny widok np. CARD1, a później znowu na ten CARD2.
Co zrobić aby po powrocie do CARD2 był znowu kolor czarny ?
Chodzi o to, aby nie zapisywał mi zmian.

Z góry dziękuję.

zamiana znacznika <code class="c"> na <code class="java"> - @furious programming

edytowany 2x, ostatnio: furious programming, 2016-12-13 18:26

Pozostało 580 znaków

2015-01-09 20:33
0

przed metodą show zawołaj na danym panelu metodę, która będzie przywracała kolor na defaultowy

Pozostało 580 znaków

2015-01-10 19:41
Kokanas
0

garai dzięki za odpowiedź. Pokażę o co mi dokładnie chodzi.
Klikam na daną markę, później na dany model. Jak kliknę znowu na te markę to mam wyświetlony konkretny model, a chciałbym wszystkie.


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

import javax.swing.*;

public class Main extends JPanel implements ActionListener {

    private static final String APPLE_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 static final String CARD_JBUTTON = "Card JButton";

    private JPanel cardHolder;
    private JPanel panel;

    private JButton homeButton = new JButton("NOKIA");
    private JButton blueButton = new JButton("SONY");
    private JButton redButton = new JButton("APPLE");
    private JButton orangeButton = new JButton("SAMSUNG");

    private JButton appleButton;

    public JPanel createCardHolderPanel() {
        cardHolder = new JPanel(new CardLayout());
        cardHolder.setBorder(BorderFactory.createTitledBorder("Model"));

        Model winn = new Model();

        cardHolder.add(winn, CARD_JBUTTON);

        cardHolder.add(ApplePanel(), APPLE_CARD);
        cardHolder.add(createColorPanel(Color.blue), BLUE_PANEL);
        cardHolder.add(createColorPanel(Color.red), RED_PANEL);
        cardHolder.add(createColorPanel(Color.orange), ORANGE_PANEL);
        cardHolder.setPreferredSize(new Dimension(100, 588));

        return cardHolder;

    }

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

    private JPanel ApplePanel() {

        panel = new JPanel((new CardLayout()));

        Icon appleIcon = new ImageIcon(this.getClass().getResource(
                "/img/apple.png"));

        appleButton = new JButton(appleIcon);
        appleButton.setBorderPainted(false);
        appleButton.setContentAreaFilled(false);

        panel.add(appleButton);

        panel.setBackground(Color.BLACK);
        return panel;

    }

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

        buttonPanel.setBorder(BorderFactory.createTitledBorder("Marka"));

        Icon appleIcon = new ImageIcon(this.getClass().getResource(
                "/img/apple.png"));

        appleButton = new JButton(appleIcon);
        appleButton.setBorderPainted(false);
        appleButton.setContentAreaFilled(false);

        homeButton.setPreferredSize(new Dimension(100, 60));

        buttonPanel.add(orangeButton);

        buttonPanel.add(homeButton);
        buttonPanel.add(blueButton);
        buttonPanel.add(redButton);

        buttonPanel.add(appleButton);

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

        return buttonPanel;
    }

    public JPanel createContentPane() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createTitledBorder("KATALOG GSM"));
        panel.setBackground(Color.GRAY);
        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, 668));

        JScrollPane scrollPane2 = new JScrollPane(createCardHolderPanel(),
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane2.setPreferredSize(new Dimension(400, 300));

        panel.add(scrollPane, BorderLayout.WEST);
        panel.add(scrollPane2, BorderLayout.CENTER);
        return panel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        CardLayout cardLayout = (CardLayout) (cardHolder.getLayout());
        if (e.getSource() == homeButton) {
            cardLayout.show(cardHolder, APPLE_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, CARD_JBUTTON);
        }

    }

    public static void createAndShowGUI() {
        JFrame frame = new JFrame("Simple CardLayout Program");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Main main = new Main();

        frame.add(main.createContentPane());

        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        frame.setSize(screenWidth / 2, screenHeight / 2);
        frame.setLocation(screenWidth / 4, screenHeight / 5 + 100);

        frame.setVisible(true);

    }

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

            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

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

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*; // Packages used in this program imported    

public class Model extends JPanel implements ActionListener {

    JPanel cards;
    JPanel widok;
    JButton button1, button2, button3;

    public Model() {
        init2();
    }

    private void init2() {
        // The entry point into your program

        setBackground(Color.BLUE);
        setPreferredSize(new Dimension(440, 411));
        button1 = new JButton("MODEL 1");
        button2 = new JButton("MODEL 2");

        button1.addActionListener(this);
        button2.addActionListener(this);

        // Create the cards

        JPanel card1 = new JPanel();
        card1.setMinimumSize(new Dimension(400, 411));
        card1.setBackground(Color.CYAN);

        card1.add(button1);
        card1.add(button2);

        JPanel card2 = new JPanel();

        JLabel label2 = new JLabel("Opis danego modelu 11111111");
        card2.setPreferredSize((new Dimension(400, 331)));

        card2.add(label2);

        card2.setBackground(Color.WHITE);

        JPanel card3 = new JPanel();

        JLabel label3 = new JLabel("Opis danego modelu 2222222");
        card3.setPreferredSize((new Dimension(400, 331)));

        card3.add(label3);
        card3.setBackground(Color.WHITE);

        // Create the panel that contains the "cards".

        cards = new JPanel(new CardLayout());

        cards.add(card1, "Card 1");
        cards.add(card2, "Card 2");
        cards.add(card3, "Card 3");

        this.add(cards);

        setVisible(true); // Make JFrame visible
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == button1) {

            CardLayout cardLayout = (CardLayout) cards.getLayout();
            // cardLayout.previous(cards);

            cardLayout.show(cards, "Card 2");

            // What do i put here to change to Panel card2 or card3 and so on.
        }

        if (e.getSource() == button2) {

            CardLayout cardLayout = (CardLayout) cards.getLayout();
            // cardLayout.previous(cards);

            cardLayout.show(cards, "Card 3");

            // What do i put here to change to Panel card2 or card3 and so on.
        }

    }

}

Pozostało 580 znaków

2015-01-10 22:57

"trochę" tu namieszałeś...

  1. Model jako pole klasy
    private Model winn;
  2. W metodzie createCardHolderPanel w ten sposób stwórz model
    winn = new Model();
  3. W modelu stwórz metodę
    public void restoreDefaultSettings(){
        CardLayout cardLayout = (CardLayout) cards.getLayout();
        cardLayout.show(cards, "Card 1");
    }
  4. i zawołaj ją przed show
    if (e.getSource() == orangeButton) {
            winn.restoreDefaultsettings();
            cardLayout.show(cardHolder, CARD_JBUTTON);
    }

Pozostało 580 znaków

2015-01-11 00:14
Kokanas
0

garai dziękuję, działa !

Pozostało 580 znaków

Liczba odpowiedzi na stronę

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