栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java Swing卡布局会更改显示的面板吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java Swing卡布局会更改显示的面板吗?

试试这个代码片段,希望这些注释可以帮助您理解序列。

import java.awt.*;import java.awt.event.*;import javax.swing.*;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));                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);                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);                previousButton.addActionListener(new ActionListener()        { public void actionPerformed(ActionEvent ae) {     CardLayout cardLayout = (CardLayout) contentPane.getLayout();     cardLayout.previous(contentPane); }        });        nextButton.addActionListener(new ActionListener()        { 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()        { public void run() {     createAndShowGUI(); }        });    }}class Window1 extends JPanel{          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()        { 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 {        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);    }    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{          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()        { 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);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/497445.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号