
正文
java中按钮的代码 java 按钮
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Java中按钮事件代码 详请见下面
很简单嘛,代码如下
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test {
static JFrame jf1 = new JFrame("jf1");
static JFrame jf2 = new JFrame("jf2");
static JButton jb = new JButton("click");
public static void main(String[] args) {
jf1.add(jb);
jf1.setSize(200, 200);
jf2.setSize(200, 200);
jf1.setVisible(true);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
jf2.setVisible(true);
}
});
}
}
相关问答
Q1: java怎么做个简单按钮
你写的按钮计算吧,这个类是一个Applet,其中有一个按钮,这个类本身也是按钮的动作监听器,所以实现了ActionListener 接口用来给按钮调用(也就是 actionPerformed方法),其中的参数e是事件参数,当点击按钮时会发送给按钮使用。e.getSource() == b 就是如果点击是b这个按钮,当监听器给一个按钮使用时没有必要加此判断,e.getSource就是获取发生事件的源对象,比如
c = new JButton("点我有次数哦");
f.getContentPane().add(c);
c.setVisible(true);
c.addActionListener(this);
此时又增加了一个按钮,就可以用e.getSource() 判断点击的是哪一个按钮。
建议你把面向对象搞懂在学swing编程吧,很容易看懂的
Q2: java编程中按钮位置的代码
setLayoutManager(new BorderLayout());
然后像这样依次添加按钮:(具体添加到面板还是窗体由你自己决定了)
add(b1,BorderLayout.south)
add(b2,BorderLayout.north)
add(b3,BorderLayout.east)
add(b4,BorderLayout.west)
用了borderlayout之后,setbounds方法是无效的,可以删除这些冗余代码
Q3: java中做一个按钮,点击按钮后画一个矩形的代码怎么写?
兄弟帮你写了一个:
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class Print {
public static void main(String[] args) {
new Te();
}
}
class Te extends Frame implements ActionListener {
Color cc = Color.red;
int x = -20, y = -50;
Random r = new Random();
public Te() {
this.setLayout(null);
Button b = new Button("画圆");
this.add(b);
b.setBounds(30,30,50,50);
b.addActionListener(this);
this.addWindowListener(new WindowAdapter () {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setBounds(200,200,500,400);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
this.cc = Color.red;
this.x = r.nextInt(400);
do {
int x1 = r.nextInt(300);
this.y = x1;
} while (this.y 50);
this.repaint();
}
@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(cc);
g.drawRect(x,y,50,50);
g.setColor(c);
}
}
Q4: Java中按钮事件代码
加入在frame中的按钮名为sure
Button sure=new Button("确定");
sure.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
frame1.setVisible(false);
Frame frame2=new Frame();
frame2.setVisible(true);
}
});
Q5: JAVA中怎么实现按钮功能?
使用图形用户界面
class Gui extends JFrame implements ActionListener {
private JButton jb = new JButton() ;
Gui() {
super("Gui") ;
this.add(jb) ;//添加按钮
jb.addActionListener(this) ;//按钮事件监听
//当然你可以按自己的想法做布局
this.pack();
this.setVisible(true);//可见
this.setResizable(false);//不可修改大小
this.setLocation(100, 100);//起始位置
}
//覆写ActionListener接口中的事件处理方法
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jb) {
//事件处理
}
}
}
关于java中按钮的代码和java 按钮的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






