Java基础知识(十一)GUI图形用户界面设计
Java详解:GUI图形用户界面设计
本文详细介绍了JavaSwing中的JFrame和JPanel组件,以及如何使用FlowLayout、BorderLayout和GridLayout进行窗口和面板的布局设计,以创建用户界面。
Graphical User Interface(GUI):图形用户界面
swing介绍:
• swing是一个为java设计的GUI工具包javax.swing,包括了用户界面的各种组件.
• swing中组件可以分为两大类:
(1) 容器组件(窗口、面板、对话框)
(2) 功能组件(按钮、菜单)
1.1 容器组件
1.1.1窗口(JFrame)
一.JFrame JFrame的主要作用是创建一个窗口类,用来装载其他组件1.构造方法 JFrame jframe = new JFrame(String title)直接构造一个带有指定title的窗口实例;JFrame()构建一个默认的窗口实例。2.窗口属性 1)setDefaultCloseOperation(int operation):设置窗口关闭时的行为,如EXIT_ON_CLOSE(退出程序)、DISPOSE_ON_CLOSE(仅关闭当前窗口)、HIDE_NO_CLOSE(隐藏当前窗口) 2)setTitle(String title):设置窗口标题。 3)setIconImage(Image image):设置窗口图标。 4)setResizable(boolean resizable):设置窗口是否可调整大小。 5)setBounds(int x, int y, int width, int height) 或 setLocation(int x, int y) 和 setSize(int width, int height):设置窗口的位置和大小。 6)setVisible(boolean visible):控制窗口是否可见。7)setLocationRelativeTo(null):设置窗口居中显示
//继承JFrame类
public class FrameDesign extends JFrame {//设置操作必须写在构造方法中public FrameDesign() {this.setSize(400, 400);//设置窗口大小this.setTitle("登录界面"); //设置标题this.setLocationRelativeTo(null); //居中位置this.setResizable(false); //设置窗口不可调整大小this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口并退出程序运行this.setVisible(true); //让窗口显示出来(放在设置最后一行)}public static void main(String[] args) {new FrameDesign();//创建FrameDesign类对象}
}
运行如下:
解释:
⑴ 首先我们创建一个类并继承JFrame类,JFrame类用来创建窗体
⑵ 创建类的构造方法,在构造方法中对窗口进行设置
1.1.2按钮(JButton)
JButton的作用是创建一个按钮,但是如果想要实现用户与界面的交互效果,还需要添加按钮的动作监听器,监听用户的输入并实现相关的功能
1)构造方法
JButton jbutton = new JButton(String title):构建带有title的按钮2)其他方法
addActionListener(new ActionListener):添加动作监听器
add(button):添加到容器中
setIcon(Image):添加图像图标
setMnemonic():设置默认快捷键
1.1.3面板(JPanel)
提示:建一个面板并上色 ( 面板为轻量级容器,需在窗口基础之上添加 )
JPanel
JPanel的作用是创建一个面板容器,这个面板容器可以将按钮添加进容器中,并通过布局管理器对容器中的组件进行排版,面板也可以被加入到窗口中1)构建方法
JPanel jpanel = new JPanel() :构建出的jpanel默认布局管理器为FlowLayout(线性布局)2)其他方法
- setLayout():设置布局管理器,BorderLayout(区域布局)、GridLayout(单元格网格布局)、GridBagLayout(网格布局)
核心代码如下:
JPanel panel =new JPanel();//创建面板panel.setBackground(Color.CYAN);//设置背景this.add(panel);//将面板添加到窗口中
完整代码如下:
import java.awt.Color;import javax.swing.JFrame;
import javax.swing.JPanel;//继承JFrame类
public class FrameDesign extends JFrame {//设置操作必须写在构造方法中public FrameDesign() {this.setSize(400, 400);//设置窗口大小this.setTitle("登录界面"); //设置标题this.setLocationRelativeTo(null); //居中位置this.setResizable(false); //设置窗口不可调整大小this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口并退出程序运行JPanel panel =new JPanel();//创建面板panel.setBackground(Color.CYAN);//设置背景this.add(panel);//将面板添加到窗口中this.setVisible(true); //让窗口显示出来(放在设置最后一行)}public static void main(String[] args) {new FrameDesign();//创建FrameDesign类对象}
}
运行如下:
详细介绍:
面板为轻量级的容器且面板可以布局
构造方法 | 说明 |
---|---|
JPanel() | 创建一个空面板 |
JPanel(LayoutManaer layout) | 创建带有指定布局的面板 |
布局方式分为三大类:
• FlowLayout(流式布局)
• BorderLayout(边界布局)
• GridLayout(网格布局)
1) 流式布局FlowLayout
a、流式布局也是默认的布局方式,组件在面板上从左到右,从上到下排列.
b、 流式布局默认水平居中且不影响组件大小.
需求: 面板是轻量级的容器,所以需要添加到窗口上,我们创建面板(JPanel)后,通过创建5个按钮组件来体验下不同的布局方式的效果。
import javax.swing.*;
import java.awt.*;//继承JFrame类
public class FrameDesign extends JFrame {//设置操作必须写在构造方法中public FrameDesign() {this.setSize(400, 400);//设置窗口大小this.setTitle("登录界面"); //设置标题this.setLocationRelativeTo(null); //居中位置this.setResizable(false); //设置窗口不可调整大小this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口并退出程序运行JPanel panel =new JPanel(new FlowLayout());JButton button1 =new JButton("按钮1");JButton button2 =new JButton("按钮2");JButton button3 =new JButton("按钮3");JButton button4 =new JButton("按钮4");JButton button5 =new JButton("按钮5");panel.add(button1);panel.add(button2);panel.add(button3);panel.add(button4);panel.add(button5);this.add(panel);//将面板添加到窗口中this.setVisible(true); //让窗口显示出来(放在设置最后一行)}public static void main(String[] args) {new FrameDesign();//创建FrameDesign类对象}
}
运行如下:
2)边界布局BorderLayout
共有5个区域(上下左右中) 中间不能少 若其余(除中间之外)位置没有指明,中间位置的组件会对其进行填充
需求: 我们通过改变JPanel构造方法中的布局类型,改为BorderLayout,并且在添加时指明具体的位置(上下左右中),其余代码和上面流式布局代码一样.
import javax.swing.*;
import java.awt.*;//继承JFrame类
public class FrameDesign extends JFrame {//设置操作必须写在构造方法中public FrameDesign() {this.setSize(400, 400);//设置窗口大小this.setTitle("登录界面"); //设置标题this.setLocationRelativeTo(null); //居中位置this.setResizable(false); //设置窗口不可调整大小this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口并退出程序运行JPanel panel =new JPanel(new BorderLayout());JButton button1 =new JButton("按钮1");JButton button2 =new JButton("按钮2");JButton button3 =new JButton("按钮3");JButton button4 =new JButton("按钮4");JButton button5 =new JButton("按钮5");panel.add(button1,BorderLayout.NORTH);//添加时指明位置panel.add(button2,BorderLayout.SOUTH);panel.add(button3,BorderLayout.WEST);panel.add(button4,BorderLayout.EAST);panel.add(button5,BorderLayout.CENTER);//中间不能省略this.add(panel);//将面板添加到窗口中this.setVisible(true); //让窗口显示出来(放在设置最后一行)}public static void main(String[] args) {new FrameDesign();//创建FrameDesign类对象}
}
运行如下;
3)网格布局GridLayout •
我们可以对网格的进行行列设置
需求: 相比上面的流式布局代码,我们只需改变JPanel构造方法中的布局类型即可(将FlowLayout改为GridLayout),并且我们可以设置网格的行列数.
案例1:
import javax.swing.*;
import java.awt.*;//继承JFrame类
public class FrameDesign extends JFrame {//设置操作必须写在构造方法中public FrameDesign() {this.setSize(400, 400);//设置窗口大小this.setTitle("登录界面"); //设置标题this.setLocationRelativeTo(null); //居中位置this.setResizable(false); //设置窗口不可调整大小this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口并退出程序运行JPanel panel =new JPanel(new GridLayout(5, 1));//5行1列JButton button1 =new JButton("按钮1");JButton button2 =new JButton("按钮2");JButton button3 =new JButton("按钮3");JButton button4 =new JButton("按钮4");JButton button5 =new JButton("按钮5");panel.add(button1,BorderLayout.NORTH);//添加时指明位置panel.add(button2,BorderLayout.SOUTH);panel.add(button3,BorderLayout.WEST);panel.add(button4,BorderLayout.EAST);panel.add(button5,BorderLayout.CENTER);//中间不能省略this.add(panel);//将面板添加到窗口中this.setVisible(true); //让窗口显示出来(放在设置最后一行)}public static void main(String[] args) {new FrameDesign();//创建FrameDesign类对象}
}
运行如下:
案例2:
import java.awt.*;
import javax.swing.*;import java.awt.*;
import javax.swing.*;public class Border extends JFrame
{JButton jb[]=new JButton[9];public static void main(String[] args){Border demo=new Border();}public Border(){//创建按钮for(int i=0;i<9;i++){jb[i]=new JButton(String.valueOf(i));}//设置网格布局管理器(行,列,行间距,列间距)this.setLayout(new GridLayout(3,3,10,10));//添加组件for(int i=0;i<9;i++){this.add(jb[i]);}//设置窗体属性this.setTitle("计算器");this.setSize(300,300);this.setLocation(100,100);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.setVisible(true);}
}
运行如下:
1.1.4文本框类(JtextField)
JtextField:文本框类用于创建一个文本控件,用于接收多行文本的输入,可以通过该控件接收用户输入的文本内容,也可以在程序运行中改变文本框的内容以输出信息
//创建默认默认文本框
JTextField textField = new JTextField();
JtextField类构造函数如下:
new JTextField(),创建默认文本框
new JTextField(int columns),创建文本框并设定可以显示的列数
new JTextField(String text),创建文本框并指定内容
new JTextField(String text, int columns),指定内容并设定可以显示的列数
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;public class GuiDemo extends JFrame{//创建文本标签和文本框JLabel usernamel=new JLabel("用户名:");JLabel usernuml=new JLabel("密 码: ");JTextField usernamet=new JTextField(18);JTextField usernumt=new JTextField(18);//创建一个容器用来储存JPanel jp=new JPanel();//注册和登录的按钮JButton jbutton1=new JButton("注册");JButton jbutton2=new JButton("登录");public GuiDemo() {// Toolkit t=Toolkit.getDefaultToolkit();//工具类
// Dimension d=t.getScreenSize();
//
// int height=(int)d.getHeight();//得到显示屏的高度
// int width=(int)d.getWidth();//得到显示屏的宽度
// this.setBounds((width-300)/2, (height-400)/2, 250, 150);//设置一个宽为250,高为150的窗口,并且让窗口居中this.setSize(300, 150);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(3);//关闭窗口的同时,结束运行this.setTitle("登录系统");//窗口标题init();this.setVisible(true);//让窗口显示}public void init() {//将内容添加到容器中jp.add(usernamel);jp.add(usernamet);jp.add(usernuml);jp.add(usernumt);jp.add(jbutton1);jp.add(jbutton2);this.add(jp);}public static void main(String[] args) {new GuiDemo();}
}
运行如下:
1.1.5单选框(JRadioButton)
完整程序如下:
import java.awt.*;
import javax.swing.*;
public class UserLogin extends JFrame{
//正确定义一个完整的对象得1分private JPanel p,p1,p2,p3,p4,p5;private JLabel userNameLabel,passWordLabel,title;private JTextField userName,passWord;private JRadioButton student,teacher;private JButton okButton,cancelButton;public UserLogin() {this.setTitle("用户管理系统");//super("大数据系成绩系统")this.setSize(300,250);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.doLogin();this.setVisible(true);}void doLogin(){JPanel p1 = new JPanel();title = new JLabel("用户管理系统登录界面");p1.add(title,JLabel.CENTER);JPanel p2 = new JPanel();userNameLabel = new JLabel("用户名:");userName = new JTextField(15);p2.add(userNameLabel);p2.add(userName);JPanel p3 = new JPanel();passWordLabel = new JLabel("密 码:");passWord = new JTextField(15);p3.add(passWordLabel);p3.add(passWord);JPanel p4 = new JPanel();p4.setLayout(new BorderLayout(10,3));student = new JRadioButton("游客");teacher = new JRadioButton("管理员");p4.add(student,BorderLayout.WEST);p4.add(teacher,BorderLayout.EAST);JPanel p5 = new JPanel();p5.setLayout(new BorderLayout(30,5));okButton = new JButton("登录");cancelButton = new JButton("取消");p5.add(okButton,BorderLayout.WEST);p5.add(cancelButton,BorderLayout.EAST);JPanel p = new JPanel();p.add(p1);p.add(p2);p.add(p3);p.add(p4);p.add(p5);this.add(p);}public static void main(String[] args) {new UserLogin();//或 UserLogin ab=new UserLogin();}
}
运行如下:
1.1.5复选框()
只需要在上诉的单选框替换为复选框,并定义即可,如下:
private JButton okButton,cancelButton;
student = new JCheckBox("游客");
teacher = new JCheckBox("管理员");
完整代码如下:
import java.awt.*;
import javax.swing.*;
public class UserLogin extends JFrame{
//正确定义一个完整的对象得1分private JPanel p,p1,p2,p3,p4,p5;private JLabel userNameLabel,passWordLabel,title;private JTextField userName,passWord;private JCheckBox student,teacher;private JButton okButton,cancelButton;public UserLogin() {this.setTitle("用户管理系统");//super("大数据系成绩系统")this.setSize(300,250);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.doLogin();this.setVisible(true);}void doLogin(){JPanel p1 = new JPanel();title = new JLabel("用户管理系统登录界面");p1.add(title,JLabel.CENTER);JPanel p2 = new JPanel();userNameLabel = new JLabel("用户名:");userName = new JTextField(15);p2.add(userNameLabel);p2.add(userName);JPanel p3 = new JPanel();passWordLabel = new JLabel("密 码:");passWord = new JTextField(15);p3.add(passWordLabel);p3.add(passWord);JPanel p4 = new JPanel();p4.setLayout(new BorderLayout(10,3));student = new JCheckBox("游客");teacher = new JCheckBox("管理员");p4.add(student,BorderLayout.WEST);p4.add(teacher,BorderLayout.EAST);JPanel p5 = new JPanel();p5.setLayout(new BorderLayout(30,5));okButton = new JButton("登录");cancelButton = new JButton("取消");p5.add(okButton,BorderLayout.WEST);p5.add(cancelButton,BorderLayout.EAST);JPanel p = new JPanel();p.add(p1);p.add(p2);p.add(p3);p.add(p4);p.add(p5);this.add(p);}public static void main(String[] args) {new UserLogin();//或 UserLogin ab=new UserLogin();}
}
运行如下:
1.1.6文本区域类(JTextArea)
JTextArea是Java Swing的文本区域组件,它允许用户在程序界面上输入和显示多行文本,可以认为是创建了一个像是记事本的简单文本编辑器
1)构建方法
JTextArea textArea = new JTextArea():
JTextArea textArea = new JTextArea(5, 列数); 创建一个初始有5行、每行20列的文本区域
2)其他方法
使用 JTextArea 时,通常会配合 JScrollPane 来使用,以便当文本内容超过其容器大小时,能够自动出现滚动,如下所示:
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
frame.add(scrollPane);setText():设置文本内容
getText():获取文本内容
setFont():设置字体、字体样式、字号
getDocument():设置文本监听器
setSelectionColor():设置字体颜色
setEditable(Blooean):设置用户是否可以编辑
此案例添加滚动条设置,需要导入JScrollPane类并实例化,具体实如下:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class UItest extends JFrame{private JButton b1,b2,b3,b4;private JPanel p,p1,p2;private JTextArea a1;private JScrollPane jp;public UItest() {this.setSize(400,300);//窗休大小this.setLocationRelativeTo(null);//居中this.setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭窗口并结束程序this.setTitle("卡片布局演示");nini();this.setVisible(true);}private void nini() {p=new JPanel();p1=new JPanel();b1=new JButton("白居易");b2=new JButton("孟浩然");b3=new JButton("李白");b4=new JButton("杜甫");p1.add(b1);p1.add(b2);p1.add(b3);p1.add(b4);p2=new JPanel();a1=new JTextArea("床前明月光\n疑是地上霜\n举头望明月\n低头思故乡低头思故乡低头思故乡低头思故乡低头思故乡低头思故乡",10,20);p2.add(a1);jp=newJScrollPane(a1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);p2.add(jp);p.add(p1);p.add(p2);this.add(p);}public static void main(String[] args) {new UItest();}
}
运行结果:
1.1.7菜单栏(JMenu)
1、定义:菜单由 Swing 中的 JMenu 类实现,可以包含多个菜单项和带分隔符的菜单。在菜单中,菜单项由 JMenuItem 类表示,分隔符由 JSeparator 类表示。
2、构造方法
JMenu() 创建一个无文本的 JMenu 对象
JMenu(String s) 创建一个带有指定文本的 JMenu 对象
3、JMenu类的常用方法
add(Action a) 创建连接到指定 Action 对象的新菜单项,并将其追加到此菜单的末尾
add(Component c) 将某个组件追加到此菜单的末尾
add(Component c,int index) 将指定组件添加到此容器的给定位置
add(JMenuItem menultem) 将某个菜单项追加到此菜单的末尾
add(String s) 创建具有指定文本的新菜单项,并将其追加到此菜单的末尾
addSeparator() 将新分隔符追加到菜单的末尾
doCliclc(int pressTime) 以编程方式执行“单击”操作
getDelay() 返回子菜单向上或向下弹出前建议的延迟(以毫秒为单位)
getltem(int pos) 返回指定位置的 JMenuItem
getItemCount() 返回菜单上的项数,包括分隔符
getMenuComponent(int n) 返回位于位置 n 的组件
getMenuComponents() 返回菜单子组件的 Component 数组
getSubElements() 返回由 MenuElement 组成的数组,其中包含此菜单组件的子菜单
insert(JMenuItem mi,int pos) 在给定位置插入指定的 JMenuitem
insert(String s,pos) 在给定位置插入具有指定文本的新菜单项
insertSeparator(int index) 在指定的位置插入分隔符
isMenuComponent(Component c) 如果在子菜单层次结构中存在指定的组件,则返回 true
isPopupMenuVisible() 如果菜单的弹出窗口可见,则返回 rue
isSelected() 如果菜单是当前选择的(即高亮显示的)菜单,则返回 true
isTopLevelMenu() 如果菜单是“顶层菜单”(即菜单栏的直接子级),则返回 true
setDelay(int d) 设置菜单的 PopupMenu 向上或向下弹出前建议的延迟
setMenuLocation(int x,int y) 设置弹出组件的位置
setPopupMenuVisible(boolean b) 设置菜单弹出的可见性
setSelected(boolean b) 设置菜单的选择状态
以记事本界面设计为例,实现如下:
import java.awt.*;
import javax.swing.*;
public class JMenueTest extends JFrame {private JMenuBar bar;private JMenu file, edit, format,see,help;private JMenuItem fnew, fwindow,fopen, fsave, fasave,fuItem,fprint,fexit;private JMenuItem ecut, ecopy, epaste,edelete,ereplace,ezd,eall;private JMenuItem line,font;private JMenuItem scale,state;private JMenuItem about,seehelp;private JTextArea ta;private JPanel p;JMenueTest() {//构造函数super("记事本");this.setSize(600, 400);this.setLocationRelativeTo(null);p = new JPanel();bar = new JMenuBar();p.setLayout(new BorderLayout());newfile_fun();edit_fun();format_fun();help_fun();bar.add(file);bar.add(edit);bar.add(format);bar.add(help);this.setJMenuBar(bar);ta = new JTextArea();p.add(ta, BorderLayout.CENTER);this.add(p);this.setVisible(true);}void newfile_fun() {//定义文件菜单功能file = new JMenu("文件");fnew = new JMenuItem("新建(N) Ctrl+N");fwindow= new JMenuItem("新窗口(W) Ctrl+Shift+N");fopen = new JMenuItem("打开(O) Ctrl+O");fsave = new JMenuItem("保存(S) Ctrl+S");fasave = new JMenuItem("另存为(A) Ctrl+Shift+S");fuItem = new JMenuItem("页面设置(U)");fprint = new JMenuItem("打印(P) Ctrl+P");fexit = new JMenuItem("退出(X)");file.add(fnew);file.add(fwindow);file.add(fopen);file.add(fsave);file.add(fasave);file.addSeparator(); file.add(fuItem);file.add(fprint);file.addSeparator(); file.add(fexit);}void edit_fun() {//定义编辑菜单功能edit = new JMenu("编辑");ecut = new JMenuItem("剪切 Ctrl+T");ecopy = new JMenuItem("复制");epaste = new JMenuItem("粘贴");edelete = new JMenuItem("删除");ereplace = new JMenuItem("替换");ezd = new JMenuItem("转到");eall = new JMenuItem("全选");edit.add(ecut);edit.add(ecopy);edit.add(epaste);edit.add(edelete);edit.addSeparator(); edit.add(ereplace);edit.add(ezd);edit.addSeparator(); edit.add(eall);}void format_fun() {//定义格式菜单功能format=new JMenu("格式");line=new JMenuItem("自动换行");font=new JMenuItem("字体");format.add(line);format.add(font);}void see_fun() {//定义查看菜单功能see=new JMenu("查看");scale=new JMenuItem("缩放");state=new JMenuItem("状态栏");see.add(scale);see.add(state);}void help_fun() {//定义帮助菜单功能help = new JMenu("帮助");seehelp = new JMenuItem("查看帮助");about = new JMenuItem("关于记事本");help.add(seehelp);help.add(about);}public static void main(String[] args) {new JMenueTest();
}
}
运行如下:
1.1.8事件监听
一、接口引入
1、引入:如果某一个功能的方法由多个人编写时,每个人声明方式不同,考虑用接口
2、一个类可以实现多个接口
3、定义格式:通过interface关键字来定义
public interface 接口名 {
声明变量;
方法实现;
}
4、接口实现
通过关键字implement来实现,格式如下:
(public) class 类名 implement 接口列表名{
实现内容;
}
二、卡片案例实现
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;public class Card extends JFrame implements ActionListener{private JPanel card,containbtn; //card为卡片显示区域, containbtn为按钮显示区域private JButton btn1,btn2,btn3,btn4; //此处定义诗人的按钮private JPanel pan1,pan2,pan3,pan4; //此处定义的面板中显示诗句private CardLayout cardlayout; //定义卡片布局private JTextArea ta1,ta2,ta3,ta4; //定义放诗句的文本域public Card(){//以下为初始化组件this.setTitle("卡片布局演示");this.setSize(400,400);this.setLocation(200,200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);nini();this.add(containbtn,BorderLayout.NORTH);//把显示按钮的组件放到上面显示this.add(card,BorderLayout.CENTER);//把显示文本域的组件放到中间显示this.setVisible(true);}void nini() {card=new JPanel();containbtn=new JPanel();cardlayout=new CardLayout();card.setLayout(cardlayout);btn1=new JButton("李白"); btn2=new JButton("白居易");btn3=new JButton("杜甫");btn4=new JButton("孟浩然");ta1=new JTextArea(30,30);ta2=new JTextArea(30,30);ta3=new JTextArea(30,30);ta4=new JTextArea(30,30);//把定义的按钮放到显示按钮的面板中containbtn.add(btn1);containbtn.add(btn2);containbtn.add(btn3);containbtn.add(btn4);//给按钮添加监听器btn1.addActionListener(this);//将当前对象(this)作为事件监听器添加到一个组件中。当该组件发生相应事件时,事件监听器将被调btn2.addActionListener(this);btn3.addActionListener(this);btn4.addActionListener(this);//给定义的文本域中添加诗人相应的诗句ta1.append("君不见,黄河之水天上来,奔流到海不复回。\r\n君不见,高堂明镜悲白发,朝如青丝暮成雪。\r\n人生得意须尽欢,莫使金樽空对月。\r\n天生我材必有用,千金散尽还复来。r\n烹羊宰牛且为乐,会须一饮三百杯。\r\n岑夫子,丹丘生,将进酒,杯莫停。\r\n与君歌一曲,请君为我倾耳听。\r\n钟鼓馔玉不足贵,但愿长醉不复醒。\r\n古来圣贤皆寂寞,惟有饮者留其名。\r\n陈王昔时宴平乐,斗酒十千恣欢谑。\r\n主人何为言少钱,径须沽取对君酌。");ta2.append("离离原上草一岁一枯荣.\r\n"+ "野火烧不尽春风吹又生.\r\n"+ "远芳侵古道晴翠接荒城.\r\n"+ "又送王孙去萋姜满别情。");ta3.append("国破山河在,城春草木深。\r\n"+ "感时花溅泪,恨别鸟惊心。");ta4.append("春眠不觉晓,处处闻啼鸟。\r\n"+ "夜来风雨声,花落知多少。");pan1=new JPanel();pan1.add(ta1);pan2=new JPanel();pan2.add(ta2);pan3=new JPanel();pan3.add(ta3);pan4=new JPanel();pan4.add(ta4);//把定义好的4个面板放到卡片布局管理器中card.add("b1",pan1);card.add("b2",pan2);card.add("b3",pan3);card.add("b4",pan4);}public void actionPerformed(ActionEvent e) {String b = e.getActionCommand();switch (b) {case "李白":cardlayout.show(card, "b1");break;case "白居易":cardlayout.show(card, "b2");break;case "杜甫":cardlayout.show(card, "b3");break;case "孟浩然":cardlayout.show(card, "b4");break;}
}
public static void main(String[] args) {new Card();}}
运行结果如下: