java菜单栏 怎么加监听

java菜单栏 怎么加监听,第1张

我帮你编了个最简单的例子。代码中已经标志了关键性代码。你可以运行看看的^_^ import java.awt.BorderLayoutimport javax.swing.JPanelimport javax.swing.JFrameimport javax.swing.JToolBarimport java.awt.Rectangleimport javax.swing.JMenuBarimport javax.swing.JMenuimport javax.swing.JMenuItempublic class MyItem extends JFrame { private static final long serialVersionUID = 1Lprivate JPanel jContentPane = nullprivate JMenuBar jJMenuBar = nullprivate JMenu jMenu = nullprivate JMenuItem jMenuItem = null/** * This is the default constructor */ public MyItem() { super()initialize()} /** * This method initializes this * * @return void */ private void initialize() { this.setSize(300, 200)this.setJMenuBar(getJJMenuBar())this.setContentPane(getJContentPane())this.setTitle("JFrame")this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0)} })} /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel()jContentPane.setLayout(null)} return jContentPane} /** * This method initializes jJMenuBar * * @return javax.swing.JMenuBar */ private JMenuBar getJJMenuBar() { if (jJMenuBar == null) { jJMenuBar = new JMenuBar()jJMenuBar.add(getJMenu())} return jJMenuBar} /** * This method initializes jMenu * * @return javax.swing.JMenu */ private JMenu getJMenu() { if (jMenu == null) { jMenu = new JMenu()jMenu.setText("菜单")jMenu.add(getJMenuItem())} return jMenu} /** * This method initializes jMenuItem * * @return javax.swing.JMenuItem */ private JMenuItem getJMenuItem() { if (jMenuItem == null) { jMenuItem = new JMenuItem()jMenuItem.setText("d出窗口")/* * 这一块是关键性代码!!!!!! * 这一块是关键性代码!!!!!! */ jMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { ///MyOut是你要d出的窗体的类 MyOut myout=new MyOut()} })/* * 这一块是关键性代码!!!!!! */ } return jMenuItem} }

****针对楼主的补充说明,我已经作了相应的修改了****

关键的代码是如这样子的:

jComboBox.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

int nIndex=jComboBox.getSelectedIndex()

////然后针对不同的nIndex值(即不同的被选项)而写入不同的代码。

}

})

我这里帮你编写了一个非常简单的案例,你可以运行看看。

代码如下:

import java.awt.BorderLayout

import javax.swing.JPanel

import javax.swing.JFrame

import java.awt.Dimension

import javax.swing.JComboBox

import java.awt.Rectangle

import javax.swing.JLabel

public class JianTing extends JFrame {

private static final long serialVersionUID = 1L

private JPanel jContentPane = null

private JComboBox jComboBox = null

private JLabel jLabel = null

private JLabel jLabel1 = null

/**

* This is the default constructor

*/

public JianTing() {

super()

initialize()

}

/**

* This method initializes this

*

* @return void

*/

private void initialize() {

this.setSize(314, 204)

this.setContentPane(getJContentPane())

this.setTitle("JFrame")

this.setVisible(true)

this.addWindowListener(new java.awt.event.WindowAdapter() {

public void windowClosing(java.awt.event.WindowEvent e) {

System.exit(0)

}

})

}

/**

* This method initializes jContentPane

*

* @return javax.swing.JPanel

*/

private JPanel getJContentPane() {

if (jContentPane == null) {

jLabel1 = new JLabel()

jLabel1.setBounds(new Rectangle(51, 89, 65, 18))

jLabel1.setText("选项内容:")

jLabel = new JLabel()

jLabel.setBounds(new Rectangle(51, 110, 186, 36))

jLabel.setText("")

jContentPane = new JPanel()

jContentPane.setLayout(null)

jContentPane.add(getJComboBox(), null)

jContentPane.add(jLabel, null)

jContentPane.add(jLabel1, null)

}

return jContentPane

}

/**

* This method initializes jComboBox

*

* @return javax.swing.JComboBox

*/

/////这里是重点代码!!!!

private JComboBox getJComboBox() {

if (jComboBox == null) {

jComboBox = new JComboBox()

jComboBox.setBounds(new Rectangle(62, 25, 170, 27))

jComboBox.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

int nIndex=jComboBox.getSelectedIndex()

if(nIndex==0){

jLabel.setText(("选项A"))

}

else if(nIndex==1){

jLabel.setText(("选项B"))

}

else if(nIndex==2){

jLabel.setText(("选项C"))

}

}

})

String[] myList={"选项A","选项B","选项C"}

jComboBox.addItem(myList[0])

jComboBox.addItem(myList[1])

jComboBox.addItem(myList[2])

}

return jComboBox

}

public static void main(String args[]){

new JianTing()

}

} // @jve:decl-index=0:visual-constraint="10,10"


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11750852.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存