「java编写窗体」java编写窗体程序

博主:adminadmin 2022-11-28 22:44:08 73

本篇文章给大家谈谈java编写窗体,以及java编写窗体程序对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

在JAVA中如何做菜单窗体?

package test.combobox;

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JToolBar;

public class TestFrame extends JFrame

{

JTextArea theArea=null;

static final String ComboStr[] = ;

public TestFrame()

{

super("JMenu1");

theArea=new JTextArea();

theArea.setEditable(true);

getContentPane().setLayout(new BorderLayout());

getContentPane().add(new JScrollPane(theArea),BorderLayout.CENTER);

JToolBar theBar=buildToolBar();

getContentPane().add(theBar,BorderLayout.NORTH);

JMenuBar MBar=new JMenuBar();

JMenu mfile=buildFileMenu();

MBar.add(mfile);

setJMenuBar(MBar);

}//end of JMenu1()

public JMenu buildFileMenu()

{

JMenu thefile=new JMenu("文件");

JMenuItem newf=new JMenuItem("新建");

JMenuItem open=new JMenuItem("打开");

JMenuItem close=new JMenuItem("关闭");

JMenuItem exit=new JMenuItem("退出");

thefile.add(newf);

thefile.add(open);

thefile.add(close);

thefile.addSeparator();//分隔线

thefile.add(exit);

return thefile;

}//end of buildFileMenu()

public JToolBar buildToolBar()

{

JToolBar toolBar=new JToolBar();

toolBar.setFloatable(true);//设置JToolBar可否浮动.

//分别构造三个ToolBarAction的类组件,准备将这三个组件设置到JToolBar中.ToolBaarAction类是我们自己编写的inner

// class,这个inner class在程序后半部份,这个类继承AbstractAction,AbstractAction是一个抽象类,实现Action

//interface,而Action interface又继承ActionListener interface又继承AActionListener interface,因此可直接在

//AbstractAction中覆写actionPerformed()方法,处理ActionEvent事件.在JToolBar中有一个add(Action a)方法,只要传入

//Action参数就可以得到一个JButton对象,因此在程序中下面的98,100,102就是以这样的方式来构造出JToolBar上的按钮组

//件.

ToolBarAction tba_new=new ToolBarAction("new",new ImageIcon(

"icons/new24.gif"));

ToolBarAction tba_open=new ToolBarAction("open",new ImageIcon(

"icons/open24.gif"));

ToolBarAction tba_close=new ToolBarAction("close",new ImageIcon(

"icons/close24.gif"));

JButton JB;

JB=toolBar.add(tba_new);//将ToolBarAction组件加入JToolBar中.

JB.setActionCommand("#TooBar_NEW performed!");

JB=toolBar.add(tba_open);//将ToolBarAction组件加入JToolBar中.

JB.setActionCommand("#ToolBar_OPEN performed!");

JB=toolBar.add(tba_close);//将ToolBarAction组件加入JToolBar中.

JB.setActionCommand("#ToolBar_CLOSE performed!");

toolBar.addSeparator();//在JToolBar加上分隔线,如同在JMenu上利用addSeparator()方法加上分隔线一样,不同的是在

//JMenu中分隔线是以灰色直线的样式表现,而在JToolBar中则是以一小段空来表示.

//在JToolBar中加入一个JLabel组件.

JLabel JLfont=new JLabel("Font Type");

toolBar.add(JLfont);

toolBar.addSeparator();

//利用上面定义的ComboStr数建立一个JComboBox组件.

JComboBox jcb = new JComboBox(ComboStr);

//将JComboBar加入事件处理模式.这里是将所选到的组件名称填入JTextArea中.

jcb.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

theArea.append("*Combobox "+((JComboBox)e.getSource()).getSelectedItem()+

" performed!\n");

}});

toolBar.add(jcb);

return toolBar;

}//end of buildToolBar()

class ToolBarAction extends AbstractAction

{

public ToolBarAction(String name,Icon icon)

{

// super(name,icon);

super(name );

}

//对于JToolBar上按钮的事件处理是将组件的ActionCommand返回值字符串加入JTextArea中.

public void actionPerformed(ActionEvent e)

{

try

{

theArea.append(e.getActionCommand()+"\n");

} catch(Exception ex)

{

}

}

}

public static void main(String[] args)

{

JFrame F=new TestFrame();

F.setSize(400,200);

F.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});//end of addWindowListener

F.setVisible(true);

} // end of main

}//end of class JMenu1

怎么用java写一个窗体程序?

下面介绍如何用简单的几句话在eclipse环境下出现一个窗口。

首先写一个frame类,继承Frame,是继承widows 然后把,出现窗口的语句封装成一个函数

public void lunchFrame(){

this.setLocation(0,0);

this.setSize(20,20);

setVisible(True);  //一定要写这句话

}

最后只需要在主函数里面调用就可以

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 。

Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点 。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

求Java编写窗体

import java.awt.*;

import java.awt.event.*;

public class TestActionEvent{

public static void main(String[] args){

Frame f = new Frame();

f.setLayout(null);

f.setBounds(100,100,200,270);

Button b = new Button("点击我");

b.setBounds(25,75,150,150);

Monitor bn = new Monitor(); //设置时间监听

b.addActionListener(bn);

f.add(b,BorderLayout.CENTER); //添加按钮

f.setVisible(true); //设置可见

f.setResizable(false); //设置窗口不可改变

}

}

class Monitor implements ActionListener{

public void actionPerformed(ActionEvent e){

System.out.println("您点击了按钮");

}

}

我试验过了 可用

java编写一个窗体应用程序,在窗体中安排两个文本框,一个标签,两个标记为+和*的按钮。从两个文本

按照你的要求编写的Java程序如下:

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;

import javax.swing.JOptionPane;

import javax.swing.JTextField;

public class Calculate extends JFrame implements ActionListener{

 JTextField jtf1=new JTextField(10);

 JTextField jtf2=new JTextField(10);

 JLabel jl=new JLabel();

 JButton jb1=new JButton("+");

 JButton jb2=new JButton("*");

 Calculate(){

  setLayout(new FlowLayout());

  jb1.addActionListener(this);

  jb2.addActionListener(this);

  add(jtf1);add(jtf2);add(jl);add(jb1);add(jb2);

  setResizable(false);

  setSize(250, 150);

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  setLocationRelativeTo(null);

  setVisible(true);

 }

 @Override

 public void actionPerformed(ActionEvent ae) {

  if(ae.getSource()==jb1){

   if(jtf1.getText().trim().equals("")){

    JOptionPane.showMessageDialog(this, "文本框不能为空");

    jtf1.requestFocus();

   }else if(jtf2.getText().trim().equals("")){

    JOptionPane.showMessageDialog(this, "文本框不能为空");

    jtf2.requestFocus();

   }else{

    int a=Integer.parseInt(jtf1.getText().trim());

    int b=Integer.parseInt(jtf2.getText().trim());

    jl.setText(String.valueOf(a+b));

   }

  }

  if(ae.getSource()==jb2){

   if(jtf1.getText().trim().equals("")){

    JOptionPane.showMessageDialog(this, "文本框不能为空");

    jtf1.requestFocus();

   }else if(jtf2.getText().trim().equals("")){

    JOptionPane.showMessageDialog(this, "文本框不能为空");

    jtf2.requestFocus();

   }else{

    int a=Integer.parseInt(jtf1.getText().trim());

    int b=Integer.parseInt(jtf2.getText().trim());

    jl.setText(String.valueOf(a*b));

   }

  }

 }

 public static void main(String[] args) {

  new Calculate();

 }

}

运行结果:

java编写窗体的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java编写窗体程序、java编写窗体的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-28,除非注明,否则均为首码项目网原创文章,转载请注明出处。