
public class Test extends JFrame
{
public Test()
{
Contianer contentPane = this.getContentPane()
contentPane.setLayout(new BorderLayout()) //设置为BorderLayout布局
contentPane.add(new Button("按键") , BorderLayout.NORTH) //放在北边
contentPane.add(new Button("按键") , BorderLayout.SOUTH) //放在南边
setVisible(true)
setSize(400,300)
}
public static void main(String args[])
{
new Test()
}
}
(代码没有调试过,自己调试吧)
基本上就可以 把Button入进去了,不知道你是这个意思没。
首先在你的helloworld程序对应的layout配置文件(res/layout/下的XXX.xml文件)中添加一个按钮,具体代码如下<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 下面这段就是添加的button -->
<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="change background" />
</LinearLayout>
然后在你的继承Activity类的java类中添加按钮的事件监听以及事件处理,代码如下:
public class 你的helloworld类名 extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.你的layout文件名)
//下面的代码用于为按钮注册一个监听
findViewById(R.id.frame_layout).setOnClickListener(new OnClickListener() {
//下面的代码用于处理按钮点击后的事件
public void onClick(View v) {
//下面的代码用于使背景变色
findViewById(R.id.layout).setBackgroundColor(Color.BLUE)
}
})
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)