1

カード レイアウトがあります。最初のカードはメニューです。

2枚目のカードを選択し、何らかのアクションを実行します。ボタンをクリックして JTextField を追加するとします。メニュー カードに戻ってから 2 番目のカードに戻ると、最初に追加した JTextField が残っています。

2 番目のカードは、アクセスするたびに最初に作成した状態にして、ボタンを付けてテキストフィールドを付けないようにしたいと考えています。

4

2 に答える 2

1

これが最終的に整理されたバージョンです。カードを削除するには、変更を加えた後、見て、いつものように と を使用してrevalidate()くださいrepaint():-)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ApplicationBase extends JFrame
{
    private JPanel centerPanel;
    private int topPanelCount = 0;

    private String[] cardNames = {
                                                        "Login Window",
                                                        "TextField Creation"
                                                   };

    private TextFieldCreation tfc;
    private LoginWindow lw;

    private JButton nextButton;
    private JButton removeButton;

    private ActionListener actionListener = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            if (ae.getSource() == nextButton)
            {   
                    CardLayout cardLayout = (CardLayout) centerPanel.getLayout();
                    cardLayout.next(centerPanel);
            }
            else if (ae.getSource() == removeButton)
            {
                    centerPanel.remove(tfc);
                    centerPanel.revalidate();
                    centerPanel.repaint();
                    tfc = new TextFieldCreation();
                    tfc.createAndDisplayGUI();  
                    centerPanel.add(tfc, cardNames[1]);
            }
        }
    };

    private void createAndDisplayGUI()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        centerPanel = new JPanel();
        centerPanel.setLayout(new CardLayout());

        lw = new LoginWindow();
        lw.createAndDisplayGUI();
        centerPanel.add(lw, cardNames[0]);
        tfc = new TextFieldCreation();
        tfc.createAndDisplayGUI();
        centerPanel.add(tfc, cardNames[1]);

        JPanel bottomPanel = new JPanel();
        removeButton = new JButton("REMOVE");
        nextButton = new JButton("NEXT");       
        removeButton.addActionListener(actionListener);
        nextButton.addActionListener(actionListener);

        bottomPanel.add(removeButton);
        bottomPanel.add(nextButton);

        add(centerPanel, BorderLayout.CENTER);
        add(bottomPanel, BorderLayout.PAGE_END);

        pack();
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ApplicationBase().createAndDisplayGUI();
            }
        });
    }
}

class TextFieldCreation extends JPanel
{
    private JButton createButton;
    private int count = 0;

    public void createAndDisplayGUI()
    {
        final JPanel topPanel = new JPanel();
        topPanel.setLayout(new GridLayout(0, 2));

        createButton = new JButton("CREATE TEXTFIELD");
        createButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                JTextField tfield = new JTextField();
                tfield.setActionCommand("JTextField" + count);

                topPanel.add(tfield);
                topPanel.revalidate();
                topPanel.repaint();
            }
        });

        setLayout(new BorderLayout(5, 5));
        add(topPanel, BorderLayout.CENTER);
        add(createButton, BorderLayout.PAGE_END);
    }
}

class LoginWindow extends JPanel
{
    private JPanel topPanel;
    private JPanel middlePanel;
    private JPanel bottomPanel;

    public void createAndDisplayGUI()
    {
        topPanel = new JPanel();

        JLabel userLabel = new JLabel("USERNAME : ", JLabel.CENTER);
        JTextField userField = new JTextField(20);
        topPanel.add(userLabel);
        topPanel.add(userField);

        middlePanel = new JPanel();

        JLabel passLabel = new JLabel("PASSWORD : ", JLabel.CENTER);
        JTextField passField = new JTextField(20);
        middlePanel.add(passLabel);
        middlePanel.add(passField);

        bottomPanel = new JPanel();

        JButton loginButton = new JButton("LGOIN");
        bottomPanel.add(loginButton);

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        add(topPanel);
        add(middlePanel);
        add(bottomPanel);
    }
}

カードのメイドを削除したいだけの場合はLatest Edit、次のコードを試してください。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ApplicationBase extends JFrame
{
    private JPanel centerPanel;
    private int topPanelCount = 0;

    private String[] cardNames = {
                                                        "Login Window",
                                                        "TextField Creation"
                                                   };

    private TextFieldCreation tfc;
    private LoginWindow lw;

    private JButton nextButton;
    private JButton removeButton;

    private ActionListener actionListener = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            if (ae.getSource() == nextButton)
            {   
                    CardLayout cardLayout = (CardLayout) centerPanel.getLayout();
                    cardLayout.next(centerPanel);
            }
            else if (ae.getSource() == removeButton)
            {
                    TextFieldCreation.topPanel.remove(TextFieldCreation.tfield);
                    TextFieldCreation.topPanel.revalidate();
                    TextFieldCreation.topPanel.repaint();
            }
        }
    };

    private void createAndDisplayGUI()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        centerPanel = new JPanel();
        centerPanel.setLayout(new CardLayout());

        lw = new LoginWindow();
        lw.createAndDisplayGUI();
        centerPanel.add(lw, cardNames[0]);
        tfc = new TextFieldCreation();
        tfc.createAndDisplayGUI();
        centerPanel.add(tfc, cardNames[1]);

        JPanel bottomPanel = new JPanel();
        removeButton = new JButton("REMOVE");
        nextButton = new JButton("NEXT");       
        removeButton.addActionListener(actionListener);
        nextButton.addActionListener(actionListener);

        bottomPanel.add(removeButton);
        bottomPanel.add(nextButton);

        add(centerPanel, BorderLayout.CENTER);
        add(bottomPanel, BorderLayout.PAGE_END);

        pack();
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ApplicationBase().createAndDisplayGUI();
            }
        });
    }
}

class TextFieldCreation extends JPanel
{
    private JButton createButton;
    private int count = 0;
    public static JTextField tfield;
    public static JPanel topPanel;

    public void createAndDisplayGUI()
    {
        topPanel = new JPanel();
        topPanel.setLayout(new GridLayout(0, 2));

        createButton = new JButton("CREATE TEXTFIELD");
        createButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                tfield = new JTextField();
                tfield.setActionCommand("JTextField" + count);

                topPanel.add(tfield);
                topPanel.revalidate();
                topPanel.repaint();
            }
        });

        setLayout(new BorderLayout(5, 5));
        add(topPanel, BorderLayout.CENTER);
        add(createButton, BorderLayout.PAGE_END);
    }
}

class LoginWindow extends JPanel
{
    private JPanel topPanel;
    private JPanel middlePanel;
    private JPanel bottomPanel;

    public void createAndDisplayGUI()
    {
        topPanel = new JPanel();

        JLabel userLabel = new JLabel("USERNAME : ", JLabel.CENTER);
        JTextField userField = new JTextField(20);
        topPanel.add(userLabel);
        topPanel.add(userField);

        middlePanel = new JPanel();

        JLabel passLabel = new JLabel("PASSWORD : ", JLabel.CENTER);
        JTextField passField = new JTextField(20);
        middlePanel.add(passLabel);
        middlePanel.add(passField);

        bottomPanel = new JPanel();

        JButton loginButton = new JButton("LGOIN");
        bottomPanel.add(loginButton);

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        add(topPanel);
        add(middlePanel);
        add(bottomPanel);
    }
}
于 2012-03-19T17:36:34.027 に答える
1

リセットしようとしているパネルに、「最初に構築されたときの状態」に戻すコードがあることを確認してください。次に、カードを変更するイベントを処理するときに、そのコードを呼び出して、カードを表示する前の元の状態に戻します。

于 2012-03-16T18:35:09.340 に答える