窗口的按钮和文本域/区

一些网页或是手机界面总是可以看到一些按钮,如最常见的确定和取消按钮。自己很久以前也觉得按钮很强大,很神奇。

如今,自己终于可以了解按钮的基本情况了,原来它的实现并不难。

以下实例是在窗口建三个按钮,分别控制窗口的背景颜色。

package button;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ButtonFrame extends JFrame{
	private JPanel buttonPanel;
	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 200;
	
	public ButtonFrame(){
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		
		JButton yellowButton = new JButton("Yellow");	//创建三个按钮
		JButton blueButton = new JButton("Blue");
		JButton redButton = new JButton("Red");
		
		buttonPanel = new JPanel();		//创建按钮面板
		
		buttonPanel.add(yellowButton);		//把按钮添加到按钮面板
		buttonPanel.add(blueButton);
		buttonPanel.add(redButton);
		
		add(buttonPanel);		//把按钮面板添加到窗口
		
		ColorAction yellowAction = new ColorAction(Color.YELLOW);
		ColorAction blueAction = new ColorAction(Color.BLUE);
		ColorAction redAction = new ColorAction(Color.RED);		//创建按钮监听事件
		
		yellowButton.addActionListener(yellowAction);
		blueButton.addActionListener(blueAction);
		redButton.addActionListener(redAction);		//让按钮监听动作事件并作出回应
	}
	
	private class ColorAction implements ActionListener{
		private Color backgroundColor;
		
		public ColorAction(Color c){
			backgroundColor = c;
		}
		
		public void actionPerformed(ActionEvent event){		//动作
			buttonPanel.setBackground(backgroundColor);
		}

	}

}

下面这个是测试类

package button;

import java.awt.EventQueue;

import javax.swing.JFrame;

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


文本域与文本区的区别:

文本域只能放一行文字,而文本区可以放多行文字

package test;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class TextFrame extends JFrame{
	public static final int TEXT_ROWS = 8;
	public static final int TEXT_COLUMNS = 20;
	
	public TextFrame(){
		final JTextField textField = new JTextField();
		final JPasswordField passwordField = new JPasswordField();
		
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new GridLayout(2, 2));		//使用网格布局
		northPanel.add(new JLabel("User name:", SwingConstants.RIGHT));//第一行一列放 User name
		northPanel.add(textField);		//第一行二列放文本域
		northPanel.add(new JLabel("Password:", JLabel.RIGHT));//第二行一列
		northPanel.add(passwordField);	//第二行二列
		
		add(northPanel, BorderLayout.NORTH);
		
		final JTextArea textArea = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
		JScrollPane scrollPane = new JScrollPane(textArea);		//使用滚动面板
		
		add(scrollPane, BorderLayout.CENTER);		//放在边框布局中心
		
		JPanel southPanel = new JPanel();
		
		JButton insertButton = new JButton("Insert");
		southPanel.add(insertButton);
		insertButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				textArea.append("User name:" + textField.getText() + "\n" +
						"Passward:" + new String(passwordField.getPassword()) + "\n");
				
			}
		});
		add(southPanel, BorderLayout.SOUTH);
		pack();
	}
}

测试类略。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值