使用java AWT做一个增加按钮的简单菜单窗体

package com.ysq.Swing;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.management.JMException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class Test1 extends JFrame{
JPanel p1=new JPanel();
JButton jb1=null;
Container container=null;
public static void main(String[] args) {
new Test1();
}
public Test1(){
container=getContentPane();
setVisible(true);
setBounds(500,400,500,550);
setSize(500, 500);
JMenuBar menubar1=new JMenuBar();
JMenu menu=new JMenu("菜单");
JMenuItem additem=new JMenuItem("增加");
JMenuItem deleteitem=new JMenuItem("删除");
JMenuItem exititem=new JMenuItem("退出");
menu.add(additem);
menu.add(deleteitem);
menu.add(exititem);
menubar1.add(menu);
setJMenuBar(menubar1);
container.add(p1);
validate();
exititem.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);

}
});
additem.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str1=JOptionPane.showInputDialog("请输入按钮的标题");
addButton(str1);
validate();

}
});



}
protected void addButton(String text) {
jb1=new JButton(text);
p1.add(jb1);



}





}
posted @ 2017-03-30 21:57  cache.yuan  阅读(519)  评论(0编辑  收藏  举报