import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;public class jtwo extends JApplet implements MenuListener {
static int VERTSPLIT=JSplitPane.VERTICAL_SPLIT;
JMenuItem t1, t2, t3, x1, x2, x3;
JLabel actionlabel = new JLabel("中国首都北京");
JPanel jcd = new JPanel();
public void init() {
Container jmain = getContentPane();
JPanel jcp = new JPanel(); JMenuBar JMB1 = new JMenuBar();
JMenuBar JMB2 = new JMenuBar();
JMenu JM1 = new JMenu("中国");
JMenu JM2 = new JMenu("美国");
JM1.add(t1 = new JMenuItem("北京"));
JM1.add(t2 = new JMenuItem("上海"));
JM1.add(t3 = new JMenuItem("广州"));
JM2.add(x1 = new JMenuItem("纽约"));
JM2.add(x2 = new JMenuItem("华盛顿"));
JM2.add(x3 = new JMenuItem("芝加哥"));
JMB1.add(JM1);
JMB1.add(JM2);
jcp.setLayout(new FlowLayout(FlowLayout.LEFT));
jcp.add(JMB1);
JSplitPane JS1 = new JSplitPane(VERTSPLIT, true, jcp, jcd);
jmain.add(JS1);
//t1.addMenuKeyListener(this);
} public void menuCanceled(MenuEvent e) { } public void menuDeselected(MenuEvent e) { } public void menuSelected(MenuEvent e) { } private void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == t1)
jcd.add(actionlabel);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;public class jtwo extends JApplet implements MenuListener {
static int VERTSPLIT=JSplitPane.VERTICAL_SPLIT;
JMenuItem t1, t2, t3, x1, x2, x3;
JLabel actionlabel = new JLabel("中国首都北京");
JPanel jcd = new JPanel();
public void init() {
Container jmain = getContentPane();
JPanel jcp = new JPanel(); JMenuBar JMB1 = new JMenuBar();
JMenuBar JMB2 = new JMenuBar();
JMenu JM1 = new JMenu("中国");
JMenu JM2 = new JMenu("美国");
JM1.add(t1 = new JMenuItem("北京"));
JM1.add(t2 = new JMenuItem("上海"));
JM1.add(t3 = new JMenuItem("广州"));
JM2.add(x1 = new JMenuItem("纽约"));
JM2.add(x2 = new JMenuItem("华盛顿"));
JM2.add(x3 = new JMenuItem("芝加哥"));
JMB1.add(JM1);
JMB1.add(JM2);
jcp.setLayout(new FlowLayout(FlowLayout.LEFT));
jcp.add(JMB1);
JSplitPane JS1 = new JSplitPane(VERTSPLIT, true, jcp, jcd);
jmain.add(JS1);
//t1.addMenuKeyListener(this);
} public void menuCanceled(MenuEvent e) { } public void menuDeselected(MenuEvent e) { } public void menuSelected(MenuEvent e) { } private void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == t1)
jcd.add(actionlabel);
}
}
JM1.add(t2=new JMenuItem("上海"));
JM1.add(t3=new JMenuItem("广州"));
JM2.add(x1=new JMenuItem("纽约"));
JM2.add(x2=new JMenuItem("华盛顿"));
JM2.add(x3=new JMenuItem("芝加哥"));
没有这样写的!改成:
JMenuItem t1 = new JMenuItem("北京");
...
JM1.add(t1);
...
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.MenuListener;public class C4 extends JApplet implements ActionListener {
static int VERTSPLIT = JSplitPane.VERTICAL_SPLIT;
JLabel actionlabel = new JLabel("中国首都北京");
Container jmain = getContentPane();
JPanel jcp = new JPanel();
JPanel jcd = new JPanel();
JMenuBar JMB1 = new JMenuBar();
JMenuBar JMB2 = new JMenuBar();
JMenu JM1 = new JMenu("中国");
JMenu JM2 = new JMenu("美国");
JMenuItem t1 = new JMenuItem("北京");
JMenuItem t2 = new JMenuItem("上海");
JMenuItem t3 = new JMenuItem("广州");
JMenuItem x1 = new JMenuItem("纽约");
JMenuItem x2 = new JMenuItem("华盛顿");
JMenuItem x3 = new JMenuItem("芝加哥"); public void init() {
JM1.add(t1);
JM1.add(t2);
JM1.add(t3);
JM2.add(x1);
JM2.add(x2);
JM2.add(x3);
JMB1.add(JM1);
JMB1.add(JM2);
jcp.setLayout(new FlowLayout(FlowLayout.LEFT));
jcp.add(JMB1);
JSplitPane JS1 = new JSplitPane(VERTSPLIT, true, jcp, jcd);
jmain.add(JS1);
t1.addActionListener(this);
t2.addActionListener(this);
t3.addActionListener(this);
x1.addActionListener(this);
x2.addActionListener(this);
x3.addActionListener(this);
jcd.add(actionlabel); } public void actionPerformed(ActionEvent evt) {
if(evt.getSource() instanceof JMenuItem)
actionlabel.setText(((JMenuItem)evt.getSource()).getText());
}}
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;public class jtwo extends JApplet implements MenuListener {
static int VERTSPLIT=JSplitPane.VERTICAL_SPLIT;
JMenuItem t1, t2, t3, x1, x2, x3;
JLabel actionlabel = new JLabel("中国首都北京");
JPanel jcd = new JPanel();
JMenuBar JMB1 = new JMenuBar();
JMenuBar JMB2 = new JMenuBar();
JMenu JM1 = new JMenu("中国");
JMenu JM2 = new JMenu("美国"); public jtwo() { }
public void init() {
Container jmain = getContentPane();
JPanel jcp = new JPanel();
JM1.add(t1 = new JMenuItem("北京"));
JM1.add(t2 = new JMenuItem("上海"));
JM1.add(t3 = new JMenuItem("广州"));
JM2.add(x1 = new JMenuItem("\u7EBD\u7EA6"));
JM2.add(x2 = new JMenuItem("\u534E盛\u987F"));
JM2.add(x3 = new JMenuItem("芝加哥"));
JMB1.add(JM1);
JMB1.add(JM2);
jcp.setLayout(new FlowLayout(FlowLayout.LEFT));
jcp.add(JMB1);
JSplitPane JS1 = new JSplitPane(VERTSPLIT, true, jcp, jcd);
jmain.add(JS1);
JM1.addMenuListener(this);
JM2.addMenuListener(this);
} public void menuCanceled(MenuEvent e) {
} public void menuDeselected(MenuEvent e) { } public void menuSelected(MenuEvent e) {
Object source = e.getSource();
if (source == JM1) {
if (JM1.getSelectedObjects()[0].equals("中国")) {
jcd.add(actionlabel);
jcd.updateUI();
}
}
}
}搞定了