如何为JButton设置动作侦听器以打开相应的面板

时间:2018-11-12 07:33:50

标签: java swing

var path = require('path');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');
var MiniCssExtractPlugin = require('mini-css-extract-plugin');
var OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");

var dirName = __dirname;
var contextPath = path.resolve(dirName + '/src');
var distPath = path.resolve(dirName + '/dist');
var prodPath = path.resolve(dirName + '/build');

var pathsToClean = [
    'dist',
    'build'
];

var cleanOptions = {
    root: path.resolve(__dirname),
    verbose: false,
    dry: false
}

module.exports = {
    target: 'web',
    mode: "production",
    entry: {
        server: path.resolve(contextPath + '/entry.js'),
        css: path.resolve(contextPath + '/Common/common.css')
    },
    output: {},
    optimization: {
        minimizer: [
            new OptimizeCSSAssetsPlugin({})
        ]
    },
    plugins: [
        new CleanWebpackPlugin(pathsToClean, cleanOptions),
        new MiniCssExtractPlugin([
            {
                filename: path.resolve(contextPath + '/Common/common.css'),
                chunkFilename: path.resolve(prodPath + "/test.min.css")
            }
        ])
    ],
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    "css-loader"
                ]
            }
        ]
    }
};

When white is clicked, the white panel should appear. When black is clicked, the black panel should appear

1 个答案:

答案 0 :(得分:0)

public class Test extends JFrame
{
    public static void main(String[] args) {
        Test frame = new Test();
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel buttons = new JPanel(new BorderLayout());
        frame.add(buttons, BorderLayout.WEST);
        JButton blackButton = new JButton("Black");
        buttons.add(blackButton, BorderLayout.NORTH);
        JButton whiteButton = new JButton("White");
        buttons.add(whiteButton, BorderLayout.SOUTH);
        JPanel content = new JPanel(new BorderLayout());
        content.setPreferredSize(new Dimension(200, 0));
        frame.add(content, BorderLayout.EAST);
        frame.pack();
        frame.setVisible(true);

        JPanel blackPanel = new JPanel();
        blackPanel.setBackground(Color.BLACK);
        JPanel whitePanel = new JPanel();
        whitePanel.setBackground(Color.WHITE);
        blackButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                content.removeAll();
                content.add(blackPanel);
                content.revalidate();
                content.repaint();
            }
        });
        whiteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                content.removeAll();
                content.add(whitePanel);
                content.revalidate();
                content.repaint();
            }
        });
    }
}