JAVA中下拉菜单中的菜单项怎样做监听
小弟初学JAVA,请给个稍微详细点的答案~~但是菜单项不是在数组里的么??Stringstr[]={"aaa","bbb","ccc"};JComboBoxjcb=new...
小弟初学JAVA,请给个稍微详细点的答案~~
但是菜单项不是在数组里的么??
String str[]={"aaa","bbb","ccc"};
JComboBox jcb=new JComboBox(str);
不好意思,时我没说清楚,去想问的是下拉列表里的列表项添加监听~~~~ 展开
但是菜单项不是在数组里的么??
String str[]={"aaa","bbb","ccc"};
JComboBox jcb=new JComboBox(str);
不好意思,时我没说清楚,去想问的是下拉列表里的列表项添加监听~~~~ 展开
展开全部
****针对楼主的补充说明,我已经作了相应的修改了****
关键的代码是如这样子的:
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"
关键的代码是如这样子的:
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"
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.text.SimpleDateFormat;
/* ComboBoxDemo2.java requires no other files. */
public class ComboBoxDemo2 extends JPanel
implements ActionListener {
static JFrame frame;
JLabel result;
String currentPattern;
public ComboBoxDemo2() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
String[] patternExamples = {
"dd MMMMM yyyy",
"dd.MM.yy",
"MM/dd/yy",
"yyyy.MM.dd G 'at' hh:mm:ss z",
"EEE, MMM d, ''yy",
"h:mm a",
"H:mm:ss:SSS",
"K:mm a,z",
"yyyy.MMMMM.dd GGG hh:mm aaa"
};
currentPattern = patternExamples[0];
//Set up the UI for selecting a pattern.
JLabel patternLabel1 = new JLabel("Enter the pattern string or");
JLabel patternLabel2 = new JLabel("select one from the list:");
JComboBox patternList = new JComboBox(patternExamples);
patternList.setEditable(true);
patternList.addActionListener(this);
//Create the UI for displaying result.
JLabel resultLabel = new JLabel("Current Date/Time",
JLabel.LEADING); //== LEFT
result = new JLabel(" ");
result.setForeground(Color.black);
result.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.black),
BorderFactory.createEmptyBorder(5,5,5,5)
));
//Lay out everything.
JPanel patternPanel = new JPanel();
patternPanel.setLayout(new BoxLayout(patternPanel,
BoxLayout.PAGE_AXIS));
patternPanel.add(patternLabel1);
patternPanel.add(patternLabel2);
patternList.setAlignmentX(Component.LEFT_ALIGNMENT);
patternPanel.add(patternList);
JPanel resultPanel = new JPanel(new GridLayout(0, 1));
resultPanel.add(resultLabel);
resultPanel.add(result);
patternPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
resultPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
add(patternPanel);
add(Box.createRigidArea(new Dimension(0, 10)));
add(resultPanel);
setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
reformat();
} //constructor
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String newSelection = (String)cb.getSelectedItem();
currentPattern = newSelection;
reformat();
}
/** Formats and displays today's date. */
public void reformat() {
Date today = new Date();
SimpleDateFormat formatter =
new SimpleDateFormat(currentPattern);
try {
String dateString = formatter.format(today);
result.setForeground(Color.black);
result.setText(dateString);
} catch (IllegalArgumentException iae) {
result.setForeground(Color.red);
result.setText("Error: " + iae.getMessage());
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("ComboBoxDemo2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new ComboBoxDemo2();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.text.SimpleDateFormat;
/* ComboBoxDemo2.java requires no other files. */
public class ComboBoxDemo2 extends JPanel
implements ActionListener {
static JFrame frame;
JLabel result;
String currentPattern;
public ComboBoxDemo2() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
String[] patternExamples = {
"dd MMMMM yyyy",
"dd.MM.yy",
"MM/dd/yy",
"yyyy.MM.dd G 'at' hh:mm:ss z",
"EEE, MMM d, ''yy",
"h:mm a",
"H:mm:ss:SSS",
"K:mm a,z",
"yyyy.MMMMM.dd GGG hh:mm aaa"
};
currentPattern = patternExamples[0];
//Set up the UI for selecting a pattern.
JLabel patternLabel1 = new JLabel("Enter the pattern string or");
JLabel patternLabel2 = new JLabel("select one from the list:");
JComboBox patternList = new JComboBox(patternExamples);
patternList.setEditable(true);
patternList.addActionListener(this);
//Create the UI for displaying result.
JLabel resultLabel = new JLabel("Current Date/Time",
JLabel.LEADING); //== LEFT
result = new JLabel(" ");
result.setForeground(Color.black);
result.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.black),
BorderFactory.createEmptyBorder(5,5,5,5)
));
//Lay out everything.
JPanel patternPanel = new JPanel();
patternPanel.setLayout(new BoxLayout(patternPanel,
BoxLayout.PAGE_AXIS));
patternPanel.add(patternLabel1);
patternPanel.add(patternLabel2);
patternList.setAlignmentX(Component.LEFT_ALIGNMENT);
patternPanel.add(patternList);
JPanel resultPanel = new JPanel(new GridLayout(0, 1));
resultPanel.add(resultLabel);
resultPanel.add(result);
patternPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
resultPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
add(patternPanel);
add(Box.createRigidArea(new Dimension(0, 10)));
add(resultPanel);
setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
reformat();
} //constructor
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String newSelection = (String)cb.getSelectedItem();
currentPattern = newSelection;
reformat();
}
/** Formats and displays today's date. */
public void reformat() {
Date today = new Date();
SimpleDateFormat formatter =
new SimpleDateFormat(currentPattern);
try {
String dateString = formatter.format(today);
result.setForeground(Color.black);
result.setText(dateString);
} catch (IllegalArgumentException iae) {
result.setForeground(Color.red);
result.setText("Error: " + iae.getMessage());
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("ComboBoxDemo2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new ComboBoxDemo2();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
假设menulist是你的菜单项的一个对象,代码如下:
menulist.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
//此处写上你所要实现监听的方法
}
});
此外你还可以加鼠标监听,类似上面的代码。
menulist.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
//此处写上你所要实现监听的方法
}
});
此外你还可以加鼠标监听,类似上面的代码。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
类似这样,对你要监听的菜单项这样写
menuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
menuItem_actionPerformed(e);
}
});
void menuItem1_actionPerformed(ActionEvent e) {
........................
}
menuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
menuItem_actionPerformed(e);
}
});
void menuItem1_actionPerformed(ActionEvent e) {
........................
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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=0; i<sLen; i++)
{
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();
}
}
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=0; i<sLen; i++)
{
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();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询