java - 向导不重绘/重新验证 jframe

标签 java swing

我想在 java 中创建一个类似于应用程序的向导。我来到了一个点,我用 3 种不同的布局绘制了一个框架,每个布局都包含一个标签和一个“下一步”按钮,但是当我单击第一个框架上的按钮时,我看到的是一个空框架。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;



public class MainFrame extends JFrame implements ActionListener{
    private BorderLayout layout;
    public MainFrame() {
        super("MyWizz");
        setSize(500, 500);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLocationRelativeTo(null);
    }
    protected ImageIcon createImageIcon(String path,
                                               String description) {
        java.net.URL imgURL = getClass().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
    public void DrawFirstPage(){
        layout = new BorderLayout();
        setLayout(layout);       
        ImageIcon icon = createImageIcon("/images/icon.png","desc");
        add(new JLabel("text",icon,JLabel.CENTER),BorderLayout.CENTER);
        JButton bn = new JButton("next");
        add(bn,BorderLayout.PAGE_END);
        setVisible(true);
        bn.setActionCommand("goto2");
        bn.addActionListener(this);
        revalidate();
    }
    public void DrawBrowsePage(){
        System.out.println(getContentPane().getComponentCount());
        getContentPane().removeAll();

        System.out.println(getContentPane().getComponentCount());
        repaint();
        revalidate();
        layout = new BorderLayout();
        JButton bn = new JButton("next");
        add(new JLabel("text",JLabel.CENTER),BorderLayout.CENTER);
        add(bn,BorderLayout.PAGE_END);
        bn.setActionCommand("goto3");
        bn.addActionListener(this);
        setLayout(layout);
        setVisible(true);
        System.out.println(getContentPane().getComponentCount());
        repaint();
        revalidate();

    }


    public void DrawLoadingPage(){
        getContentPane().removeAll();
        repaint();
        revalidate();
        layout = new BorderLayout();
        setLayout(layout);
        setVisible(true);
        JButton bn = new JButton("next");
        add(new JLabel("text",JLabel.CENTER),BorderLayout.CENTER);
        add(bn,BorderLayout.PAGE_END);
        repaint();
        revalidate();
    }
    public void actionPerformed(ActionEvent e) {
        if("goto2".equals(e.getActionCommand())) DrawBrowsePage();
        if("goto3".equals(e.getActionCommand())) DrawLoadingPage();
    }
}

从那几个 System.out.println 函数中,我可以看到 DrawBrowsePage() 执行,旧元素被删除并添加了新元素。非常感谢您能提供的任何帮助。

常见问题解答:不可以。我不能使用 jwizz。

最佳答案

您可以简单地使用 CardLayout 而不是 removeadd 然后 revalidate()/repaint() .

我的意思的简短解释:

只需创建 JPanel 的堆栈或列表(使用集合或数组),每个代表不同的 View 。单击下一步按钮时,只需使用 cardLayout.show(container, "StringNameOfThePanelYouWantToShow") 显示下一个 JPanel。更多信息可以在 CardLayout API 上找到,求助一个小例子,看看它的功能:

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

/* Here we are first declaring our class that will act as the
 * base for other panels or in other terms the base for CardLayout.
 */

public class CardLayoutTest {

    private static final String CARD_JBUTTON =  "Card JButton";
    private static final String CARD_JTEXTFIELD = "Card JTextField";    
    private static final String CARD_JRADIOBUTTON = "Card JRadioButton";

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Card Layout Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        // This JPanel is the base for CardLayout for other JPanels.
        final JPanel contentPane = new JPanel();
        contentPane.setLayout(new CardLayout(20, 20));

        /* Here we be making objects of the Window Series classes
         * so that, each one of them can be added to the JPanel 
         * having CardLayout. 
         */
        Window1 win1 = new Window1();
        contentPane.add(win1, CARD_JBUTTON);
        Window2 win2 = new Window2();
        contentPane.add(win2, CARD_JTEXTFIELD);
        Window3 win3 = new Window3();
        contentPane.add(win3, CARD_JRADIOBUTTON);

        /* We need two JButtons to go to the next Card
         * or come back to the previous Card, as and when
         * desired by the User.
         */
        JPanel buttonPanel = new JPanel(); 
        final JButton previousButton = new JButton("PREVIOUS");
        previousButton.setBackground(Color.BLACK);
        previousButton.setForeground(Color.WHITE);
        final JButton nextButton = new JButton("NEXT");
        nextButton.setBackground(Color.RED);
        nextButton.setForeground(Color.WHITE);
        buttonPanel.add(previousButton);
        buttonPanel.add(nextButton);

        /* Adding the ActionListeners to the JButton,
         * so that the user can see the next Card or
         * come back to the previous Card, as desired.
         */
        previousButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                cardLayout.previous(contentPane);
            }
        });
        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                cardLayout.next(contentPane);   
            }
        });

        // Adding the contentPane (JPanel) and buttonPanel to JFrame.
        frame.add(contentPane, BorderLayout.CENTER);
        frame.add(buttonPanel, BorderLayout.PAGE_END);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }
} 

class Window1 extends JPanel {

    /*
     * Here this is our first Card of CardLayout, which will
     * be added to the contentPane object of JPanel, which
     * has the LayoutManager set to CardLayout.
     * This card consists of Two JButtons.
     */  
    private ActionListener action;

    public Window1() {
        init();
    }

    private void init() {
        final JButton clickButton = new JButton("CLICK ME");
        final JButton dontClickButton = new JButton("DON\'T CLICK ME");     

        action = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (ae.getSource() == clickButton) {
                    JOptionPane.showMessageDialog(null, "Hello there dude!"
                                                , "Right Button", JOptionPane.INFORMATION_MESSAGE);
                } else if (ae.getSource() == dontClickButton) {
                    JOptionPane.showMessageDialog(null, "I told you not to click me!"
                                                        , "Wrong Button", JOptionPane.PLAIN_MESSAGE);
                }
            }
        };

        clickButton.addActionListener(action);
        dontClickButton.addActionListener(action);

        add(clickButton);
        add(dontClickButton);
    }
}

class Window2 extends JPanel implements ActionListener {

    /*
     * Here this is our second Card of CardLayout, which will
     * be added to the contentPane object of JPanel, which
     * has the LayoutManager set to CardLayout.
     * This card consists of a JLabel and a  JTextField
     * with GridLayout.
     */  

    private JTextField textField;

    public Window2() {
        init();
    }

    private void init() {
        setLayout(new GridLayout(1, 2));
        JLabel userLabel = new JLabel("Your Name : ");
        textField = new JTextField();
        textField.addActionListener(this);

        add(userLabel);
        add(textField);
    }

    @Override
    public void actionPerformed(ActionEvent e) {            
        if (textField.getDocument().getLength() > 0)
            JOptionPane.showMessageDialog(null, "Your Name is : " + textField.getText()
                                                                            , "User\'s Name : ", JOptionPane.QUESTION_MESSAGE);
    }
}

class Window3 extends JPanel {

    /*
     * Here this is our third Card of CardLayout, which will
     * be added to the contentPane object of JPanel, which
     * has the LayoutManager set to CardLayout.
     * This card consists of Two JLabels and two JCheckBox
     * with GridLayout.
     */  
    private ActionListener state;

    public Window3() {
        init();
    }

    public void init() {
        setLayout(new GridLayout(2, 2));
        JLabel maleLabel = new JLabel("MALE", JLabel.CENTER);
        final JCheckBox maleBox = new JCheckBox();
        JLabel femaleLabel = new JLabel("FEMALE", JLabel.CENTER);
        final JCheckBox femaleBox = new JCheckBox();

        state = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (maleBox == (JCheckBox) ae.getSource()) {
                    femaleBox.setSelected(false);
                    JOptionPane.showMessageDialog(null, "Congrats you are a Male"
                                                , "Gender : ", JOptionPane.INFORMATION_MESSAGE);                            
                } else if (femaleBox == (JCheckBox) ae.getSource()) {
                    maleBox.setSelected(false);
                    JOptionPane.showMessageDialog(null, "Congrats you are a Female"
                                            , "Gender : ", JOptionPane.INFORMATION_MESSAGE);                        
                }
            }
        };

        maleBox.addActionListener(state);
        femaleBox.addActionListener(state);
        add(maleLabel);
        add(maleBox);
        add(femaleLabel);
        add(femaleBox);
    }
}

关于java - 向导不重绘/重新验证 jframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18406386/

相关文章:

java - keygen标签的私钥

java - 我怎么知道可以从方法中抛出哪些异常?

java - FileUtils 写入行 "not applicable for arguements"?

java - 如何制作JPanel的背景渐变

java - Java 中的操作优先级。 (对象在 GUI 更新之前实例化并运行?)

java - Selenium 与 Java 发送键与keys.TAB

java - 从字符串中删除最多 4 个空格

Java矩形图像

java - 将 JPanelForm 类转换为 JFrameForm 类

java - 添加自定义背景时 JButton 和 JLabel 消失