首页 新闻 会员 周边 捐助

java awt菜单增加按钮后,如何再关掉窗体后再打开还能看到临时增加的按钮?

0
[待解决问题]

RT:代码如下;

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);



}

}

cache.yuan的主页 cache.yuan | 菜鸟二级 | 园豆:204
提问于:2017-03-30 22:26
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册