「java窗体按钮」java窗口点击按钮菜单实现各种功能
本篇文章给大家谈谈java窗体按钮,以及java窗口点击按钮菜单实现各种功能对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
Java中怎么在一个窗体点击一个按钮打开另一个窗体?
假如你的那个按钮叫button,你要打开的那个窗体的类名叫Form2.
你在button的click事件里面写个
Form2 fm=new Form2();
fm.show();
就行了。。当然,你的Form2类,要设置Visible为True,同时设置大小位置。不然,你看不到窗体。
给你贴个代码,你自己看吧
该代码经过调试,验证可行。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class formshow extends JFrame implements ActionListener{
JButton button;
public formshow(){
button=new JButton("点击我,出现新窗体");
add(button);
button.addActionListener(this);
this.setLayout(new FlowLayout());
this.setBounds(520, 130, 200, 100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
formshow fs=new formshow();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==button){
form2 fm=new form2();
}
}
}
class form2 extends JFrame{
//第二个窗体;
JLabel jl;
static int n=1;
public form2(){
n++;
jl=new JLabel("我是第"+n+"个窗体。");
add(jl);
this.setTitle("我是第"+n+"个窗体");
//设置属性。
this.setLayout(new FlowLayout());
this.setBounds(120+11*n, 230+10*n, 200, 100);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}
一个窗体,一个按钮,最简单的java代码怎写?
public class Demo extends JFrame
{
JButton jb; //一个按钮
public static void main(String []args){
new Demo();
}
public Demo()
{
this.setLayout(new FlowLayout());
jb=new JButton("按扭");
this.add(jb);
this.setSize(400,300);
this.setVisible(true);
this.setLocation(500, 200);
}
}
JAVA如何用按钮关闭窗体
很久没有用过界面编程了,就当复习一下了,哈哈
如一楼所说的,给按钮加一个监听器ActionListener,写一个实现方法
actionPerformed.此时当按钮点击时会调用actionPerformed方法,代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Close extends JFrame implements ActionListener{
JButton close;
public Close(){
close = new JButton("close");//增加一个按钮
add(close);
close.addActionListener(this);//给按钮增加一个监听器
setLayout(new FlowLayout());
setSize(200,100);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//捕捉到按钮点击时的事件处理方法
//按钮点击时一定会自动执行actionPerformed(ActionEvent e)方法
public void actionPerformed(ActionEvent e){
//关闭整个应用程序.如果只是是想关闭当前窗口,可以用
//dispose();
System.exit(0);
}
public static void main(String[] args){
new Close();
}
}
关于java窗体按钮和java窗口点击按钮菜单实现各种功能的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。