⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cardlayoutdemo.java

📁 本压缩文件中含有线程的控制
💻 JAVA
字号:
package gui;


import javax.swing.*;

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

public class CardLayoutDemo extends JFrame implements ActionListener{
    public static final int WIDTH = 300;
    public static final int HEIGHT = 200;

    private CardLayout dealer;
    private JPanel deckPanel;

    public CardLayoutDemo( ) {
        setSize(WIDTH, HEIGHT);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("CardLayout Demonstration");
        Container contentPane = getContentPane( );
        contentPane.setLayout(new BorderLayout( ));

        deckPanel = new JPanel( );
        dealer = new CardLayout( );
        deckPanel.setLayout(dealer);

        JPanel startCardPanel = new JPanel( );
        startCardPanel.setLayout(new FlowLayout( ));
        startCardPanel.setBackground(Color.LIGHT_GRAY);
        JLabel startLabel = new JLabel("Hello");
        startCardPanel.add(startLabel);
        deckPanel.add("start", startCardPanel);

        JPanel greenCardPanel = new JPanel( );
        greenCardPanel.setLayout(new FlowLayout( ));
        greenCardPanel.setBackground(Color.GREEN);
        JLabel goLabel = new JLabel("Go");
        greenCardPanel.add(goLabel);
        deckPanel.add("green", greenCardPanel);

        JPanel redCardPanel = new JPanel( );
        redCardPanel.setLayout(new FlowLayout( ));
        redCardPanel.setBackground(Color.RED);
        JLabel stopLabel = new JLabel("Stop");
        redCardPanel.add(stopLabel);
        deckPanel.add("red", redCardPanel);

        contentPane.add(deckPanel, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel( );
        buttonPanel.setBackground(Color.WHITE);
        buttonPanel.setLayout(new FlowLayout( ));
        JButton stopButton = new JButton("Red");
        stopButton.addActionListener(this);
        buttonPanel.add(stopButton);
        JButton goButton = new JButton("Green");
        goButton.addActionListener(this);
        buttonPanel.add(goButton);
        JButton resetButton = new JButton("Reset");
        resetButton.addActionListener(this);
        buttonPanel.add(resetButton);
        contentPane.add(buttonPanel, BorderLayout.SOUTH);
        dealer.first(deckPanel);//Optional
   }


   public void actionPerformed(ActionEvent e){
       String actionCommand = e.getActionCommand( );

       if (actionCommand.equals("Red"))
           dealer.show(deckPanel, "red");
       else if (actionCommand.equals("Green"))
           dealer.show(deckPanel, "green");
       else if (actionCommand.equals("Reset"))
           dealer.show(deckPanel, "start");
       else
           System.out.println("Error in CardLayout Demo.");
    }


    public static void main(String[] args){
        CardLayoutDemo demoGui = new CardLayoutDemo( );
        demoGui.setVisible(true);
    }
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -