JAVA中下拉菜单中的菜单项怎样做监听

JAVA中下拉菜单中的菜单项怎样做监听,第1张

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

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

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"

Spinner组件的主要功能是用于进行下拉列表显示的功能,当用户选中下拉列表中的某个选项之后可以使用Spinner类中提供的setOnItemClickListener()方法进行监听。

下拉菜单监听范例:

package com.richard.onitemselectedlistener

import android.os.Bundle

import android.app.Activity

import android.view.Menu

import android.view.View

import android.widget.AdapterView

import android.widget.AdapterView.OnItemSelectedListener

import android.widget.ArrayAdapter

import android.widget.Spinner

public class MainActivity extends Activity {

private Spinner city = null                                // 定义下拉列表框

private Spinner area = null                                // 定义下拉列表框

private String[][] areaData = new String[][] {              // 定义联动菜单项

{ "东城", "西城", "朝阳", "大兴", "平谷" },           // 第一级子选项

{ "黄浦", "杨浦", "闵行" },                           // 第二级子选项

{ "广州" } }                                     // 第三级子选项

private ArrayAdapter<CharSequence> adapterArea = null        // 下拉列表内容适配器

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState)                     // 父类onCreate()

super.setContentView(R.layout.activity_main)                   // 调用布局管理器

this.city = (Spinner) super.findViewById(R.id.city)    // 取得组件

this.area = (Spinner) super.findViewById(R.id.area)    // 取得组件

this.city.setOnItemSelectedListener(

new OnItemSelectedListenerImpl())              // 设置监听器

}

private class OnItemSelectedListenerImpl implements OnItemSelectedListener {

@Override

public void onItemSelected(AdapterView<?> adapterView, View view,

int position, long id) {                        // 选项选中时触发

MainActivity.this.adapterArea = new ArrayAdapter<CharSequence>(

MainActivity.this,

android.R.layout.simple_spinner_item,

MainActivity.this.areaData[position])  // 实例化列表项

MainActivity.this.adapterArea.setDropDownViewResource(

android.R.layout.simple_spinner_dropdown_item) // 设置下拉列表显示风格

MainActivity.this.area

.setAdapter(MainActivity.this.adapterArea) // 设置数据

}

@Override

public void onNothingSelected(AdapterView<?> adapterView) {   // 没有选项时触发

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu)

return true

}

}

import java.awt.event.ItemListener

import java.awt.event.ItemEvent

import javax.swing.JFrame

import javax.swing.JComboBox

public class ComBoxDemo extends JFrame

{

JComboBox computer//主类别下拉框

JComboBox fittings//配件下拉框

public ComBoxDemo()

{

super("ComBoxDemo")

this.setLayout(null)

computer=new JComboBox(new String[]{"", "联想电脑", "华硕笔记本", "IBM笔记本"})

fittings = new JComboBox()

//设置两个下拉框的位置和大小

computer.setBounds(100, 40, 100, 25)

fittings.setBounds(100, 80, 100, 25)

//添加事件监听器

computer.addItemListener(new ItemListener()

{

//设置几组常量保存各电脑品牌的配件信息

private final String[] ITEM_LX = {"联想CPU", "联想内存", "联想显示器"}

private final String[] ITEM_HS = {"华硕CPU", "华硕内存", "华硕液晶屏"}

private final String[] ITEM_IBM = {"IBMCPU", "IBM内存", "IBM液晶屏"}

//开始itemStateChanged方法,该方法在选择一台电脑时自动调用

public void itemStateChanged(ItemEvent e) {

//首先获得选择的电脑品牌的索引号

int index = computer.getSelectedIndex()

//将配件下拉框中的所有项移除

fittings.removeAllItems()

//判断所选的电脑品牌

switch (index)

{

case 1: //联想电脑

addItems(ITEM_LX)

break

case 2: //华硕笔记本

addItems(ITEM_HS)

break

case 3: //IBM笔记本

addItems(ITEM_IBM)

break

default: //其它情况

break

}

} // itemStateChanged()方法结束

//该方法将指定的字符串数组当中的内容添加到配件下拉列表中

private void addItems(String[] s)

{

int sLen = s.length

if (sLen == 0)

{

return

}

for (int i=0i<sLeni++)

{

fittings.addItem(s[i])

}

} // addItems()方法结束

})/* 事件监听添加完成 */

//初始化窗口

this.add(computer)

this.add(fittings)

this.setBounds(240, 200, 320, 240)

this.setVisible(true)

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

}

//主函数

public static void main(String[] args)

{

new ComBoxDemo()

}

}


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

原文地址:https://54852.com/bake/11611980.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存