java按钮的添加
① 怎么样在java中添加按钮
首先在类里面声明一个按钮的对象 JButton b1;
然后在构造函数里声明b1的事件监听 b1.addActionListener(this);
再然后进行事件处理 public void actionPerformed(ActionEvent e) {};
至于你在监听到按钮行为后想要有什么操作你就自己编写吧
如果还有什么问题 可以再我在线的时候问我 不过我的水平也不高哦
② java怎么给按钮添加事件
类名后面添加一个actionListener 接口
解决方法:
(1)public void actionPerformed(ActionEvent e) {
}
这么一个方法,然后再在构造方法里面添加注册事件,button.addActionListener(this);
(2)在res/layout中的activity_main.xml添加一个按钮,命名为button1。
(3)在MainActivity.Java中添加按钮事件绑定:
③ java怎么创建一个按钮
java当中创建按钮,必须使用java swing来创建,实例如下:
JButton有个构造函数是JButton(Icon icon),就是回用来创建带图标答的按钮的。
可以这样用,如:
JButton jb = new JButton(new ImageIcon("images/myImage.gif"));
这样得到的就是带图标的按钮了。
④ java界面添加按钮
为什么要多此一举写继承?写paint?直接new一个JPanel设置背景就完事了。
⑤ java中如何给button添加事件
Java Swing本身复提供了现成的按钮制控件JButton 创建一个新的按钮:JButton about = new JButton; 这个按钮该放到菜单区:toolBar.add(about); 要为按钮添加事件响应,需要使用about.addActionListener(this)来告诉程序监听按钮按下时的事件,ActionListener是一个程序接口。 public class KyodaiUI extends JFrame implements ActionListener {...}实现ActionListener接口是为了告诉程序我要进行事件处理了。 最后我们得添加响应事件的代码: public void actionPerformed(ActionEvent e) { if (e.getSource() == about) {
⑥ java在窗口中添加按钮怎么显示在窗口啊
用frame.add(button)添加按钮对象到窗体对象,
再用frame.setVisable(true)显示窗口即可
或用
panel.add(button);
frame.add(panel);
frame.setVisable(true);
⑦ JAVA界面添加按钮
按照你的要求添加两个按钮的Java程序如下:
packagecom.sunshine.customer;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjavax.swing.table.DefaultTableModel;
{
privateJPaneltopPanel;
privateJTabletable;
privateJButtonaddRoom=newJButton("添加房间");
privateJButtonremoveRoom=newJButton("删除房间");
privateDefaultTableModeldtm;
publicJTableDemo(){
setTitle("SimpleJTableApplication");
setSize(560,400);
setBackground(Color.gray);
topPanel=newJPanel();
topPanel.setLayout(newBorderLayout());
getContentPane().add(topPanel);
Stringcols[]={"房间号","是否预定","房间价格","房间类型","房间状态","卫生情况","剩余时间"};
StringrowData[][]={{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},{"","","","","","",""},
{"","","","","","",""},{"","","","","","",""},{"","","","","","",""},};
dtm=newDefaultTableModel();
dtm.setDataVector(rowData,cols);
table=newJTable(dtm);
JScrollPanescrollPane=newJScrollPane(table);
topPanel.add(scrollPane,BorderLayout.CENTER);
addRoom.addActionListener(this);
removeRoom.addActionListener(this);
JPanelp=newJPanel();
p.add(addRoom);
p.add(removeRoom);
topPanel.add(p,BorderLayout.SOUTH);
}
publicstaticvoidmain(Stringargs[]){
JTableDemomainFrame=newJTableDemo();
mainFrame.setVisible(true);
}
@Override
publicvoidactionPerformed(ActionEventae){
if(ae.getSource()==addRoom){
Stringrow[]={"","","","","","",""};
dtm.addRow(row);
}
if(ae.getSource()==removeRoom){
dtm.removeRow(table.getSelectedRow());
}
}
}
⑧ 怎样在java中的按钮JButton上加图片
JButton有一个setIcon(Icon icon);方法可以在JButton上加图片。例如:
publicclassButtonImage
{
publicButtonImage()
{
JFrameframe=newJFrame();
JButtonbutton=newJButton("confirm");
ImageIconicon=newImageIcon("image.jpg");
button.setIcon(icon);
frame.add(button);
frame.setLayout(newFlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
publicstaticvoidmain(String[]args)
{
newImageButton();
}
}
可以把iamge.jpg裁剪为合适的大小,效果可能就会很好。
⑨ Java 添加按钮点击事件
xml文件代码如下:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
四种方法分述如下:
匿名内部类:
public class TestButtonActivity extends Activity {
Button btn1, btn2;
Toast tst;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_button);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast tst = Toast.makeText(TestButtonActivity.this, "111111111", Toast.LENGTH_SHORT);
tst.show();
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast tst = Toast.makeText(TestButtonActivity.this, "222222222", Toast.LENGTH_SHORT);
tst.show();
}
});
}
}
自定义单击事件监听类:
public class TestButtonActivity extends Activity {
Button btn1, btn2;
Toast tst;
class MyClickListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
tst = Toast.makeText(TestButtonActivity.this, "111111111", Toast.LENGTH_SHORT);
tst.show();
break;
case R.id.button2:
tst = Toast.makeText(TestButtonActivity.this, "222222222", Toast.LENGTH_SHORT);
tst.show();
break;
default:
break;
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_button);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(new MyClickListener());
btn2.setOnClickListener(new MyClickListener());
}
}
Activity继承View.OnClickListener,由Activity实现OnClick(View view)方法,在OnClick(View view)方法中用switch-case对不同id代表的button进行相应的处理
public class TestButtonActivity extends Activity implements OnClickListener {
Button btn1, btn2;
Toast tst;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_button);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
tst = Toast.makeText(this, "111111111", Toast.LENGTH_SHORT);
tst.show();
break;
case R.id.button2:
tst = Toast.makeText(this, "222222222", Toast.LENGTH_SHORT);
tst.show();
break;
default:
break;
}
}
}
最后一种是我今天看到的一种写法,在XML文件中逗显示指定按钮的onClick属性,这样点击按钮时会利用反射的方式调用对应Activity中的click()方法地
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Button2" />
这里在输完android:的时候按下 Alt+/ 会有 onClick 属性的提示, 但输入到 android:onClick=逗 的地方按下 Alt+/ 并没有提示 onClick 选项,让我突然觉得这里好像有点问题。
public class TestButtonActivity extends Activity {
Button btn1, btn2;
Toast tst;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_button);
}
// 注意 这里没有 @Override 标签
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
tst = Toast.makeText(this, "111111111", Toast.LENGTH_SHORT);
tst.show();
break;
case R.id.button2:
tst = Toast.makeText(this, "222222222", Toast.LENGTH_SHORT);
tst.show();
break;
default:
break;
}
}
}
这种写法整个代码中都不用声明button就可以实现button的单击事件。
⑩ java怎么在按钮里添加数据
<html>
<head>
<script>
function operation()
{
var lg=document.gerElementById("l");
var wh=document.gerElementById("w");
var perimeter=document.gerElementById("perimeter");<!--根据id得到周长文本框对象-->
var proportion=document.gerElementById("proportion");<!--面积文本框对象-->
if(lg.value==""||wh.value=="")
{
window.alert("输入不能为空")
}
else if(lg.value)||wh.value=="")
{
window.alert("输入的不是数字!")
}
else
{
var l=parseInt(lg.value));
var w=parseInt(wh.value));
perimeter.value=(l+w)*2;<!--得到周长-->
proportion.value=l*w;<!--得到面积-->
}
}
</script>
</head>
<body>
<input type="text" name="lon" id="l"/>
<input type="text" name="wid" id="w"/>
<input type="button" name="operat" id="operation" onclick="operation()"/>
<input type="text" name="perim" id="perimeter"/>
<input type="text" name="propo" id=="proportion" />
</body>
</body>