一些网页或是手机界面总是可以看到一些按钮,如最常见的确定和取消按钮。自己很久以前也觉得按钮很强大,很神奇。
如今,自己终于可以了解按钮的基本情况了,原来它的实现并不难。
以下实例是在窗口建三个按钮,分别控制窗口的背景颜色。
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();
}
}
测试类略。