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

📄 buttontest.java

📁 A plane with different buttons, just need to press the button and the background color will be chang
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonTest
{
	public static void main(String[] args)
	{
		ButtonFrame frame = new ButtonFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}

class ButtonFrame extends JFrame
{
	public ButtonFrame()
	{	setTitle("Button Test");
		setSize(DefualtX,DefualtY);

		ButtonPanel bluepanel = new ButtonPanel(Color.BLUE,"BLUE");
		ButtonPanel yellowpanel = new ButtonPanel(Color.YELLOW,"Yellow");
		ButtonPanel greenpanel = new ButtonPanel(Color.GREEN,"Green");
		ButtonPanel redpanel = new ButtonPanel(Color.RED,"Red");
		ButtonPanel exitpanel = new ButtonPanel("Exit");

		add(bluepanel, BorderLayout.NORTH);
		add(yellowpanel, BorderLayout.WEST);
		add(greenpanel, BorderLayout.EAST);
		add(redpanel, BorderLayout.SOUTH);
		add(exitpanel, BorderLayout.CENTER);
	}
	public int DefualtX = 300;
	public int DefualtY = 150;
		
}

class ButtonPanel extends JPanel
{
	public ButtonPanel(Color color, String strs)
	{
		JButton colorButton = new JButton(strs);
		add(colorButton);
		colorButton.addActionListener(new ColorAction(color));
	}
	
	public ButtonPanel(String strs)
	{	JButton exitButton = new JButton(strs);
		add(exitButton);
		exitButton.addActionListener(new ExitAction());
	}

	private class ColorAction implements ActionListener
	{ 
		public ColorAction(Color c)
		{ backgroundColor = c;
		}

		public void actionPerformed(ActionEvent event)
		{ setBackground(backgroundColor);
		  JButton source = (JButton)event.getSource();
		  source.setText("Exit");
		  source.addActionListener(new ExitAction());
		}

		private Color backgroundColor;
	}
	
	private class ExitAction implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{  System.exit(0);	
		}
	}

}

⌨️ 快捷键说明

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