📄 12.txt
字号:
public class FrameDemo extends Frame
{
Label lblDisplayLabe = new Label();
//Construct the frame
public FrameDemo(String strTitle)
{
//设置框架窗体标题
super(strTitle);
lblDisplayLabe.setText("这是个Frame的例子");
this.add(lblDisplayLabe);
}
//程序的入口方法
public static void main( String[] args )
{
//创建框架窗体
FrameDemo frmFrameDemo=new FrameDemo("这是个使用Frame的例子");
//设置框架窗体的事件监听(关闭窗体事件)
frmFrameDemo.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
//正常退出Java虚拟机
System.exit(0);
}
});
//显示框架窗体
frmFrameDemo.pack();
frmFrameDemo.show();
}
}
例程12-12
//DialogDemo.java
import java.awt.*;
import java.awt.event.*;
public class DialogDemo extends Frame
{
//定义两个Button组件
Button btnDisplayDialog = new Button("显示对话框");
Button btnCloseDialog = new Button("关闭对话框");
//定义Dialog组件,并且设置其为非模式对话框
Dialog dlgDialog=new Dialog(this,"对话框标题",false);
//frame的构造方法
public DialogDemo(String strTitle)
{
//设置框架窗体标题
super(strTitle);
//设置对话框的大小
dlgDialog.setSize(300,300);
//把两个Button组件加入到Frame框架窗口中
this.add(btnDisplayDialog,BorderLayout.WEST);//把该按钮显示窗口的左侧
this.add(btnCloseDialog,BorderLayout.EAST);//把该按钮显示窗口的右侧
//添加两个Button的事件监听
btnDisplayDialog.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//如果对话框已经显示在屏幕上,则直接返回
if( dlgDialog.isShowing()) return;
//显示对话框
dlgDialog.show();
}
});
btnCloseDialog.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//如果对话框已经显示在屏幕上,则关闭对话框
if( dlgDialog.isShowing())
dlgDialog.dispose();
}
});
}
//程序的入口方法
public static void main( String[] args )
{
//创建框架窗体
DialogDemo frmDialogDemo=new DialogDemo("这是个使用Dialog的例子");
//设置框架窗体的事件监听(关闭窗体事件)
frmDialogDemo.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
//关闭框架窗口
System.exit(0);
}
});
//显示框架窗体
frmDialogDemo.pack();
frmDialogDemo.show();
}
}
例程12-13
//FileDialogDemo.java
import java.awt.*;
import java.awt.event.*;
public class FileDialogDemo extends Frame
{
FileDialog dlgFileDialog;
//Frame的构造方法
public FileDialogDemo(String title)
{
//设置框架窗体的标题
super(title);
//创建Button
Button btnLoadFile= new Button("打开文件");
//添加Button的事件监听
btnLoadFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if( dlgFileDialog == null )
dlgFileDialog=new FileDialog(FileDialogDemo.this);
//设置文件对话框为打开模式
dlgFileDialog.setMode(FileDialog.LOAD);
//如果对话框已经显示在屏幕上,则直接返回
if( dlgFileDialog.isShowing()) return;
//显示对话框
dlgFileDialog.show();
String strFileName=dlgFileDialog.getFile();
if( strFileName == null )
System.out.println("你取消了文件的选择");
else
System.out.println("你选择的文件名称:"+ strFileName );
}
});
//创建Button
Button btnSaveFile= new Button("保存文件");
//添加Button的事件监听
btnSaveFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if( dlgFileDialog == null )
dlgFileDialog=new FileDialog(FileDialogDemo.this);
//设置文件对话框为保存模式
dlgFileDialog.setMode(FileDialog.SAVE);
//如果对话框已经显示在屏幕上,则直接返回
if( dlgFileDialog.isShowing()) return;
//显示对话框
dlgFileDialog.show();
String strFileName=dlgFileDialog.getFile();
if( strFileName == null )
System.out.println("你取消了文件的选择");
else
System.out.println("你选择的文件名称:"+ strFileName );
}
});
//设置流式布局管理器
this.setLayout( new FlowLayout());
//把两个Button加入到Frame中,用FlowLayout布局管理器对其进行管理
this.add(btnLoadFile);
this.add(btnSaveFile);
}
public static void main(String[] args)
{
//创建框架窗体
FileDialogDemo frmFileDialogDemo=new FileDialogDemo("文件对话框例子");
//设置框架窗体的事件监听(关闭窗体事件)
frmFileDialogDemo.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//显示框架窗体
frmFileDialogDemo.pack();
frmFileDialogDemo.show();
}
}
例程12-14
//FlowLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
public class FlowLayoutDemo extends Frame
{
public FlowLayoutDemo()
{
//设置框架窗口的布局管理器为Flowlayout
this.setLayout( new FlowLayout());
//向框架窗口添加8个Button
this.add( new Button("1") );
this.add( new Button("2") );
this.add( new Button("3") );
this.add( new Button("4") );
this.add( new Button("5") );
this.add( new Button("6") );
this.add( new Button("7") );
this.add( new Button("8") );
}
public static void main( String[] args )
{
FlowLayoutDemo frmFlowLayout=new FlowLayoutDemo();
//设置框架窗体的事件监听(关闭窗体事件)
frmFlowLayout.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//显示框架窗体
frmFlowLayout.pack();
frmFlowLayout.show();
}
}
例程12-15
//BorderLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
public class BorderLayoutDemo extends Frame
{
public BorderLayoutDemo()
{
//设置框架窗口的布局管理器为BorderLayoutDemo
this.setLayout( new BorderLayout());
//上面一行代码可以不要,Frame默认是BorderLayout布局管理器
//向框架窗口添加5个Button
this.add( new Button("南"), BorderLayout.SOUTH);
this.add( new Button("北") ,BorderLayout.NORTH);
this.add( new Button("中间") ,BorderLayout.CENTER);
this.add( new Button("西") ,BorderLayout.WEST);
this.add( new Button("东") ,BorderLayout.EAST);
}
public static void main( String[] args )
{
BorderLayoutDemo frmBorderLayout=new BorderLayoutDemo();
//设置框架窗体的事件监听(关闭窗体事件)
frmBorderLayout.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//显示框架窗体
frmBorderLayout.pack();
frmBorderLayout.show();
}
//设置框架窗体的大小为300×300
public Dimension getPreferredSize()
{
return new Dimension(300,300);
}
}
例程12-16
//CardLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
public class CardLayoutDemo extends Frame
{
//包含四个功能按钮的Panel的定义和创建
Panel pnlCommandArea=new Panel();
//显示功能Panel的定义和创建
Panel pnlDisplayArea=new Panel();
//CardLayout布局管理器的创建
CardLayout cardlayout1=new CardLayout();
//四个功能按钮的定义和创建
Button btnFirst=new Button("第一个");
Button btnPrevious=new Button("前一个");
Button btnNext=new Button("后一个");
Button btnLast=new Button("最后一个");
//框架窗体的构造方法
public CardLayoutDemo()
{
//设置Frame的布局管理器为BorderLayout
this.setLayout(new BorderLayout());
//把两个Panel加入到布局管理器中
this.add( pnlCommandArea, BorderLayout.NORTH);
this.add( pnlDisplayArea, BorderLayout.CENTER); //=======================================================
// 显示功能区域
//=======================================================
//把显示功能区域Panel的布局管理器设置为CardLayout
pnlDisplayArea.setLayout(cardlayout1);
//创建第一个显示Panel
Panel pnlFirst=new Panel();
pnlFirst.setBackground(Color.blue);
pnlFirst.setForeground(Color.white);
pnlDisplayArea.add("first",pnlFirst);
pnlFirst.add(new Label("This is the first Panel") );
//创建第二个显示Panel
Panel pnlSecond=new Panel();
pnlSecond.setBackground(Color.red);
pnlSecond.setForeground(Color.blue);
pnlDisplayArea.add("second",pnlSecond);
pnlSecond.add(new Label("This is the second Panel") );
//创建第三个显示Panel
Panel pnlThird=new Panel();
pnlThird.setBackground(Color.black);
pnlThird.setForeground(Color.white);
pnlDisplayArea.add("third",pnlThird);
pnlThird.add(new Label("This is the third Panel") );
//创建第四个显示Panel
Panel pnlFourth=new Panel();
pnlFourth.setBackground(Color.green);
pnlFourth.setForeground(Color.black);
pnlDisplayArea.add("fourth",pnlFourth);
pnlFourth.add(new Label("This is the fourth Panel") );
//======================================================
// 创建和显示功能按钮区域 //=====================================================
//为四个功能按钮设置事件监听器
btnFirst.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
processAction(e);
}
});
btnPrevious.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
processAction(e);
}
});
btnNext.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
processAction(e);
}
});
btnLast.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
processAction(e);
}
});
//把四个功能按钮加入到Panel
pnlCommandArea.add( btnFirst );
pnlCommandArea.add( btnPrevious );
pnlCommandArea.add( btnNext );
pnlCommandArea.add( btnLast );
}
//程序的入口方法
public static void main( String[] args )
{
//创建框架窗体的实例
CardLayoutDemo frmCardLayout=new CardLayoutDemo();
//设置框架窗体的事件监听(关闭窗体事件)
frmCardLayout.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
//正常退出Java虚拟机
System.exit(0);
}
});
//显示框架窗体
frmCardLayout.pack();
frmCardLayout.show();
}
//设置框架窗体的大小为300×300
public Dimension getPreferredSize()
{
return new Dimension(300,300);
}
//处理按钮的事件
private void processAction(ActionEvent e)
{
//获取事件源(用户选择是哪个按钮)
Button btnEvent=(Button)e.getSource();
//判断是用户选择哪个按钮并调用相应的方法
if( btnEvent.equals(btnFirst))
cardlayout1.first( pnlDisplayArea );
else if( btnEvent.equals(btnLast))
cardlayout1.last( pnlDisplayArea );
else if( btnEvent.equals(btnPrevious))
cardlayout1.previous( pnlDisplayArea );
else if( btnEvent.equals(btnNext))
cardlayout1.next( pnlDisplayArea );
}
}
例程12-17
//GridLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
public class GridLayoutDemo extends Frame
{
public GridLayoutDemo()
{
//设置框架窗口的布局管理器为GridLayout(2行4列)
this.setLayout( new GridLayout(2,4));
//向框架窗口添加8个Button
this.add( new Button("1") );
this.add( new Button("2") );
this.add( new Button("3") );
this.add( new Button("4") );
this.add( new Button("5") );
this.add( new Button("6") );
this.add( new Button("7") );
this.add( new Button("8") );
}
public static void main( String[] args )
{
GridLayoutDemo frmGridLayout=new GridLayoutDemo();
//设置框架窗体的事件监听(关闭窗体事件)
frmGridLayout.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//显示框架窗体
frmGridLayout.pack();
frmGridLayout.show();
}
}
例程12-18
//NullLayoutDemo.java
import java.awt.*;
import java.awt.event.*;
public class NullLayoutDemo extends Frame
{
Label lblUserName = new Label("用户名称:");
TextField txtUserName = new TextField();
Label lblUserPasswd = new Label("用户密码:");
TextField txtUserPasswd = new TextField();
Button btnLogin = new Button("登录");
Button btnReset = new Button("重设");
public NullLayoutDemo()
{
//设置框架窗口的布局管理器为null布局管理器
this.setLayout(null);
//手工设置各个组件的位置和大小
lblUserName.setBounds(new Rectangle(45, 38, 67, 34));
txtUserName.setBounds(new Rectangle(115, 39, 210, 33));
lblUserPasswd.setBounds(new Rectangle(43, 86, 66, 26));
txtUserPasswd.setBounds(new Rectangle(115, 84, 210, 33));
txtUserPasswd.setEchoChar('*');
btnLogin.setBounds(new Rectangle(78, 150, 86, 30));
btnReset.setBounds(new Rectangle(193, 150, 86, 30));
//把各个组件添加到框架窗口中
this.add(lblUserName);
this.add(txtUserName);
this.add(lblUserPasswd);
this.add(txtUserPasswd);
this.add(btnLogin);
this.add(btnReset);
}
public static void main( String[] args )
{
NullLayoutDemo frmNullLayout=new NullLayoutDemo();
//设置框架窗体的事件监听(关闭窗体事件)
frmNullLayout.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//显示框架窗体
frmNullLayout.pack();
frmNullLayout.show();
}
//设置框架窗口的大小
public Dimension getPreferredSize()
{
return new Dimension(355, 204);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -