用java在JPanel中建一个表格_利用JPanel类 JFrame JButton 以及布局管理类实现的一个简单布局界面...

该博客展示了如何使用Java创建一个包含三个JPanel的GUI界面,每个面板可以通过点击相应颜色按钮来改变背景颜色。主要涉及到JFrame、JPanel、JButton及布局管理类的应用。
摘要由CSDN通过智能技术生成

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JButton;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.Color;

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.FlowLayout;

public class Outer extends JFrame implements ActionListener{

/**

*

*/

private static final long serialVersionUID = 1L;

public static final int WIDTH = 300;

public static final int HEIGTH = 200;

private JPanel redPanel;

private JPanel whitePanel;

private JPanel bluePanel;

public static void main(String []args)

{

Outer gui = new Outer();

gui.setVisible(true);

}

public Outer()

{

super("Panel Demonstration");

this.setSize(WIDTH,HEIGTH);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setLayout(new BorderLayout());

JPanel biggerPanel = new JPanel();

biggerPanel.setLayout(new GridLayout(1,3));

redPanel = new JPanel();

redPanel.setBackground(Color.LIGHT_GRAY);

biggerPanel.add(redPanel);

whitePanel = new JPanel();

whitePanel.setBackground(Color.LIGHT_GRAY);

biggerPanel.add(whitePanel);

bluePanel = new JPanel();

bluePanel.setBackground(Color.LIGHT_GRAY);

biggerPanel.add(bluePanel);

this.add(biggerPanel,BorderLayout.CENTER);

JPanel buttonPanel = new JPanel();

buttonPanel.setBackground(Color.LIGHT_GRAY);

buttonPanel.setLayout(new FlowLayout());

JButton redButton = new JButton("Red");

redButton.setBackground(Color.RED);

redButton.addActionListener(this);

buttonPanel.add(redButton);

JButton whiteButton = new JButton("White");

whiteButton.setBackground(Color.WHITE);

whiteButton.addActionListener(this);

buttonPanel.add(whiteButton);

JButton blueButton = new JButton("Blue");

blueButton.setBackground(Color.BLUE);

blueButton.addActionListener(this);

buttonPanel.add(blueButton);

this.add(buttonPanel,BorderLayout.SOUTH);

}

public void actionPerformed(ActionEvent e)

{

String str = e.getActionCommand();

if(str.equals("Red"))

redPanel.setBackground(Color.RED);

else if(str.equals("White"))

whitePanel.setBackground(Color.WHITE);

else if(str.equals("Blue"))

bluePanel.setBackground(Color.BLUE);

}

}

5deb706ce882c88394fe75aae2650130.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值