
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
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)
}
})
}
lz 你好
(ps: lz 加点儿分吧 太少了……)
具体代码如下:
import java.awt.*import java.awt.event.*
import javax.swing.*
import java.io.*
public class Test extends JFrame {
private JLabel name,phone,sex
private JTextField inputName,inputPhone
private JComboBox sexBox
private String[] item = {"男", "女"}
private JButton save
public Test() {
super("信息管理")
setSize(240,300)
setLayout(new FlowLayout(FlowLayout.CENTER, 10, 30))
name = new JLabel("姓名:")
phone = new JLabel("手机号:")
sex = new JLabel("性别:")
inputName = new JTextField(14)
inputPhone = new JTextField(14)
sexBox = new JComboBox(item)
save = new JButton("储存")
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(inputName.getText().equals("") || inputPhone.getText().equals("")) {
return
}
try {
File f = new File("info.txt")
BufferedWriter bw = new BufferedWriter(new FileWriter(f, true))
if(f.length() == 0){
bw.write("姓名\t性别\t手机号")
bw.newLine()
}
String str = inputName.getText()+"\t"+sexBox.getSelectedItem()+"\t"+inputPhone.getText()
bw.write(str)
bw.newLine()
bw.close()
} catch (Exception ex) {
}
}
})
getContentPane().setBackground(Color.WHITE)
getContentPane().add(Box.createHorizontalStrut(5))
getContentPane().add(name)
getContentPane().add(inputName)
getContentPane().add(phone)
getContentPane().add(inputPhone)
getContentPane().add(Box.createHorizontalStrut(35))
getContentPane().add(sex)
getContentPane().add(sexBox)
getContentPane().add(Box.createHorizontalStrut(35))
getContentPane().add(save)
setLocationRelativeTo(null)
setVisible(true)
setDefaultCloseOperation(3)
}
public static void main (String[] args) {
new Test()
}
}
希望能帮助你哈
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)