一、在窗口中输入字符串,按下回车键即在命令行中输出字符串,并统计窗口中输入的字符串长度。如在图一中输入nwafu,命令行中输出nwafu:5(如图二所示)
图一
图二
代码实现:
//进行界面搭建
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class WindowActionEvent extends JFrame{
JTextField text;
ActionListener listener;
public WindowActionEvent()
{
setLayout(new FlowLayout());
text=new JTextField(10);
add(text);
listener=new readlistener();
text.addActionListener(listener);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("处理ActionEvent事件");
setBounds(100,100,310,260);
}
}
//进行事件处理
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class readlistener implements ActionListener{
public void actionPerformed(ActionEvent e)
{
String str=e.getActionCommand();
System.out.println(str+":"+str.length());
}
}
//主类
public class main {
public static void main(String [] args)
{
new WindowActionEvent();
}
}
二、练习使用borderlayout布局、flowlayout布局
代码运行结果:
图1
图2
代码实现:
//设计五颜六色的面板
import javax.swing.*;
import java.awt.*;
public class PanelGridLayout extends JPanel{
PanelGridLayout(){
GridLayout grid=new GridLayout(12,12);
setLayout(grid);
Label label[][]=new Label[12][12];
for(int i=0;i<12;i++)
{
for(int j=0;j<12;j++)
{
label[i][j]=new Label();
if((i+j)%2==0
)
{
label[i][j].setBackground(Color.blue);
}
else {
label[i][j].setBackground(Color.BLACK);
}
add(label[i][j]);
}
}
}
}
//设计普通面板
import java.awt.*;
import javax.swing.*;
public class PanelNullLayout extends JPanel{
JButton button;
JTextField text;
PanelNullLayout(){
setLayout(null);
button=new JButton("确定");
text=new JTextField();
add(text);
add(button);
text.setBounds(100,30,90,30);
button.setBounds(190,30,66,30);
}
}
//展示一下
import java.awt.*;
import javax.swing.*;
public class Showlayout extends JFrame{
PanelGridLayout panelGrid;
PanelNullLayout panelNull;
JTabbedPane p;
Showlayout()
{
panelGrid=new PanelGridLayout();
panelNull=new PanelNullLayout();
p=new JTabbedPane();
p.add("网格布局的面板",panelGrid);
p.add("空布局的面板",panelNull);
add(p,BorderLayout.CENTER);
add(new JButton("窗体是BorderLayout布局"),BorderLayout.NORTH);
add(new JButton("南"),BorderLayout.SOUTH);
add(new JButton("西"),BorderLayout.WEST);
add(new JButton("东"),BorderLayout.EAST);
setBounds(10,10,570,390);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
validate();
}
}
//主类
public class example {
public static void main(String []args)
{
new Showlayout();
}
}
三、盒式布局
运行结果:
代码实现:
import java.awt.*;
import javax.swing.*;
public class WindowBoxLayout extends JFrame{
Box boxH;//行形盒式容器
Box boxVone,boxVtwo;//列形盒式容器
public WindowBoxLayout()
{
setLayout(new FlowLayout());
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init()
{
setTitle("嵌套盒式布局容器窗口");
setBounds(100,100,310,260);
boxH=Box.createHorizontalBox();
boxVone=Box.createVerticalBox();
boxVtwo=Box.createVerticalBox();
boxVone.add(new JLabel("姓名"));
boxVone.add(new JLabel("职业"));
boxVtwo.add(new JTextField(10));
boxVtwo.add(new JTextField(10));
boxH.add(boxVone);
boxH.add(Box.createHorizontalStrut(10));
boxH.add(boxVtwo);
add(boxH);
}
}
//主类
public class main {
public static void main(String []args)
{
new WindowBoxLayout();
}
}
欢迎批评指正!