Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 如何在这代码中添加一个按钮?
snooopy





发贴: 23
积分: 0
于 2005-10-22 18:34 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
因为一旦frame里的setVisible为true后,再向容器中添加组件,就显示不出来了 ?

所以我试了添加一个Button后,没办法显示出来 ?

如何在这代码中的菜单里按下preview,就会在主窗口中显示一个按钮出来 ?



package gui;

import java.awt.CheckboxMenuItem;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.event.MenuEvent;
import java.awt.event.*;
import java.awt.*;

public class TestMenuBar extends Frame {
MenuBar menubar = new MenuBar();
Menu fileM = new Menu("File");
Menu editM = new Menu("Edit");
Menu toolsM = new Menu("Tools");
Menu helpM = new Menu("Help");

MenuItem fileM1 = new MenuItem("New");
MenuItem fileM2 = new MenuItem("Open");
MenuItem fileM3 = new MenuItem("Save");

Menu fileM4 = new Menu("print");

MenuItem printM1 = new MenuItem("preview");
MenuItem printM2 = new MenuItem("setting");


CheckboxMenuItem fileM5 = new CheckboxMenuItem("Quit",true);


public TestMenuBar(){
menubar.add(fileM);
menubar.add(editM);
menubar.add(toolsM);
menubar.add(helpM);

fileM.add(fileM1);
fileM.add(fileM2);
fileM.add(fileM3);
fileM.add(fileM4);
fileM.addSeparator();
fileM.add(fileM5);

fileM4.add(printM1);
fileM4.add(printM2);

MenuListener ml = new MenuListener();
printM1.addActionListener(ml);
printM2.addActionListener(ml);

this.setMenuBar(menubar);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}

public static void main(String[] args) {
TestMenuBar tt = new TestMenuBar();
tt.setSize(400,400);
tt.setVisible(true);
}
}

class MenuListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("preview")){
System.out.println("doing preview");
} else if (e.getActionCommand().equals("setting")){
System.out.println("doing setting");
}
}
}





话题树型展开
人气 标题 作者 字数 发贴时间
3453 如何在这代码中添加一个按钮? snooopy 2381 2005-10-22 18:34

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923