JMenuItem menu102 = new JMenuItem("菜单102")
menu102.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addIFame(new InternalFrame())
}
})
上述代码就能实现在MenuBar中的MenuItem中增加监听。
第一个问题:使用veiwMenuItem.addActionListener()方法添加监听器。具体使用可以查看JDK API第二个问题,一时想到两种实现方法
1、你要知道,JPanel不单是一个组件,更是一个组件容器,所以你可以往其中添加一个表格组件,而这个表格组件则将你的查询结果显示出来
2、如果你不希望使用其他组件了的话,倒也可以重载paint()方法,直接将你要显示的信息“绘”出来。例如:
public class DisplayPanel extends JPanel{
public void pain(Graphics g){
super.paint(g)
g.drawString(student.getName())
g.drawString(student.getNumber())
.... ....
}
}
然后将这个Panel添加到你原来的位置即可。
你可以在图片和JMenuItem的设置背景颜色。具体的给你看一个例子:(设定方法,见注)进口的java.awt.BorderLayout
进口java.awt.Color中
导入java.awt中。 event.ActionEvent
进口java.awt.event.ActionListener
进口javax.swing.ImageIcon中
进口javax.swing.JButton中
进口的javax。 swing.JFrame
进口javax.swing.JMenu中
进口javax.swing.JMenuBar中
进口javax.swing.JMenuItem中
进口javax.swing.JSeparator
公共E类扩展JFrame中实现的ActionListener {
JButton的紧密= NULL
JMenuItem的mopen = NULL
JMenuItem的mclose = NULL
JMenuItem的MEXIT = NULL
E(){
紧密=新的JButton(“退出”)
添加(接近,BorderLayout.SOUTH)
close.addActionListener(本)
的JMenuBar JMB =新的JMenuBar()
JMenu的文件=新的JMenu(“文件”)
mopen =新的JMenuItem(“打开”)
的ImageIcon logoing =新的ImageIcon(“black_bishop.gif”) / /这里定义的图标图片
mopen.setIcon(logoing) / /这里设置图标的图片到菜单项
mopen.setBackground(颜色发黄。)/ /在哪里设置背景色为黄色的MenuItem
mclose =新的JMenuItem(“关闭”)
MEXIT =新的JMenuItem(“退出”)
了JSeparator分隔符=新了JSeparator()
file.add(mopen)
file.add(mclose)
file.add(分离器)
文件。添加(MEXIT)
jmb.add(文件)
setJMenuBar(JMB)
mclose.addActionListener(本)
mexit.addActionListener(本)
的SetSize(300,300)
其setVisible(真)
setDefaultCloseOperation(DISPOSE_ON_CLOSE)
公共静态无效的主要(字串[] args){
新的E()
公共无效的actionPerformed(ActionEvent的AE){
如果(ae.getSource()==接近){
处置()
}否则,如果(ae.getSource()== mclose){
的dispose()
}否则,如果(ae.getSource()== MEXIT){
的dispose()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)