package graphic;import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class HelloAwt extends Frame { /**
*
*/
// private static final long serialVersionUID = 1L; public static void main(String[] args)
{
new HelloAwt().run();
}
/**
* 运行程序
*/
private void run()
{
createButton();
configureFrame();
createButton();
createButton();
createButton();
createButton();
createButton();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
/**
* 创建主窗口
*
*/
private void configureFrame()
{
setTitle("Hello");
setLayout( new FlowLayout());
setSize(new Dimension(400,400));
setLocation(100,110);
}
/**
* 创建按钮
*/
private void createButton()
{
Button button = new Button("打开");
button.addActionListener(new ActionListener(){ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
createDialog();
}
});
add(button,BorderLayout.NORTH);
// setVisible(true);
}
private void createDialog()
{
//第一个参数表示是该类,第二个名字,
final Dialog dialog = new Dialog(HelloAwt.this,"对话框",true);
dialog.setSize(new Dimension(267,117));
Toolkit toolkit = dialog.getToolkit();
Dimension screenSize = toolkit.getScreenSize();
// x代表对话框的左边的位置
int x = HelloAwt.this.getX()
+(HelloAwt.this.getWidth()-dialog.getWidth())/2;
// 如果其小于 0 ,则代表其超出了左边屏幕,
// 则将其设置成0
if(x < 0)
x = 0;
//其加上对话框的宽度为右边对话框的x位置,如果其大于屏幕宽度,则将其
//设置为屏幕狂度减去对话框宽度,也就让对话框的右边对其屏幕右边
if((x+dialog.getWidth())>screenSize.width)
{
x = screenSize.width - dialog.getWidth();
}
//获取dialog的上方的Y坐标
int y = HelloAwt.this.getY()
+(HelloAwt.this.getHeight()-dialog.getHeight())/2;
if(y < 0)
y = 0;
//如果其下方在屏幕之下,则将其移至贴着屏幕的地方
if((y + dialog.getHeight()) > screenSize.height)
{
y = screenSize.height - dialog.getHeight();
}
dialog.setLocation(x, y);
dialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
dialog.dispose();
}
});
dialog.setLayout(new GridLayout(2,1));
Panel topPanel = new Panel();
Label label = new Label("这是一个对话框");
topPanel.add(label,BorderLayout.NORTH);
dialog.add(topPanel);
Panel bottomPanel = new Panel();
Button button = new Button("按钮");
button.addActionListener(new ActionListener(){ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dialog.dispose();
}
});
bottomPanel.add(button,BorderLayout.SOUTH);
dialog.add(bottomPanel);
dialog.setVisible(true);
}
}
问题1:请问这个Frame和Button是怎么关联到一起的。
是通过这句代码么?add(button,BorderLayout.NORTH);
这个add的使用者是谁呢?
问题2:我在里面复制了很多次createButton,这些Button都具有相同的名字button,他们都存在于Frame上,如何区分它们呢。
import java.awt.Button;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class HelloAwt extends Frame { /**
*
*/
// private static final long serialVersionUID = 1L; public static void main(String[] args)
{
new HelloAwt().run();
}
/**
* 运行程序
*/
private void run()
{
createButton();
configureFrame();
createButton();
createButton();
createButton();
createButton();
createButton();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
/**
* 创建主窗口
*
*/
private void configureFrame()
{
setTitle("Hello");
setLayout( new FlowLayout());
setSize(new Dimension(400,400));
setLocation(100,110);
}
/**
* 创建按钮
*/
private void createButton()
{
Button button = new Button("打开");
button.addActionListener(new ActionListener(){ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
createDialog();
}
});
add(button,BorderLayout.NORTH);
// setVisible(true);
}
private void createDialog()
{
//第一个参数表示是该类,第二个名字,
final Dialog dialog = new Dialog(HelloAwt.this,"对话框",true);
dialog.setSize(new Dimension(267,117));
Toolkit toolkit = dialog.getToolkit();
Dimension screenSize = toolkit.getScreenSize();
// x代表对话框的左边的位置
int x = HelloAwt.this.getX()
+(HelloAwt.this.getWidth()-dialog.getWidth())/2;
// 如果其小于 0 ,则代表其超出了左边屏幕,
// 则将其设置成0
if(x < 0)
x = 0;
//其加上对话框的宽度为右边对话框的x位置,如果其大于屏幕宽度,则将其
//设置为屏幕狂度减去对话框宽度,也就让对话框的右边对其屏幕右边
if((x+dialog.getWidth())>screenSize.width)
{
x = screenSize.width - dialog.getWidth();
}
//获取dialog的上方的Y坐标
int y = HelloAwt.this.getY()
+(HelloAwt.this.getHeight()-dialog.getHeight())/2;
if(y < 0)
y = 0;
//如果其下方在屏幕之下,则将其移至贴着屏幕的地方
if((y + dialog.getHeight()) > screenSize.height)
{
y = screenSize.height - dialog.getHeight();
}
dialog.setLocation(x, y);
dialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
dialog.dispose();
}
});
dialog.setLayout(new GridLayout(2,1));
Panel topPanel = new Panel();
Label label = new Label("这是一个对话框");
topPanel.add(label,BorderLayout.NORTH);
dialog.add(topPanel);
Panel bottomPanel = new Panel();
Button button = new Button("按钮");
button.addActionListener(new ActionListener(){ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
dialog.dispose();
}
});
bottomPanel.add(button,BorderLayout.SOUTH);
dialog.add(bottomPanel);
dialog.setVisible(true);
}
}
问题1:请问这个Frame和Button是怎么关联到一起的。
是通过这句代码么?add(button,BorderLayout.NORTH);
这个add的使用者是谁呢?
问题2:我在里面复制了很多次createButton,这些Button都具有相同的名字button,他们都存在于Frame上,如何区分它们呢。
解决方案 »
- spring中ApplicationContext.xml文件能不能改为其他文件名?
- java中如何判断一个字符串是不是数值类型,不是判断是不是数字。
- 问个初学struts 1 的问题。
- 如何更加深入的理解Struts?
- 在Maven里,Hibernate4和Spring3应该怎么配置C3P0?我有个报错大家能不能帮着看看?
- ibatis怎么调用oracle的存储过程,得到返回值?
- 怎么用JSP将JAVA程序里已经赋值的a[4]里面的数字显示出来???
- WebSphere Studio Application Developer 移植指南(转贴3)
- 请教两种tomcat的区别???
- 大家给点意见,什么时候应该用EJB,什么时候不用?
- 求助:Hsql Transfer to MSSQL时提示的JAVA错误
- 女人与java
所以add的使用者本质上是HelloAwt的当前对象。因为HelloAwt继承Frame所以也可说成Frame,二者通过add进行联系
2、应为你在主面板的北部使用了Flowlayout则会有一个排列的先后关系给楼主点建议我认为该关心的是程序的监听,以及对关闭等细节的处理;
或者得到子集等方式
参考我这里用Awt做个简单的计算器
http://www.niwozhi.com/question/117_1886.html