import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class TestChoiceFrame extends JFrame {
Choice C=new Choice();
public TestChoiceFrame() {
C.add("choice1");
C.add("choice2");
C.add("choice3");
setLayout(new FlowLayout());
add(C);
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
menuFile.setText("File");
menuFileExit.setText("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
TestChoiceFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("TestChoice");
setJMenuBar(menuBar);
setSize(new Dimension(400, 400));
// Add window listener.
C.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(C.getItem(1).equals("choice1"))
{
Button B1=new Button("nihao!");
TestChoiceFrame.this.add(B1,BorderLayout.SOUTH);
}
else if(C.getItem(2).equals("choice2"))
{
Button B2=new Button("dajiahao!");
TestChoiceFrame.this.add(B2,BorderLayout.NORTH);
}
else if(C.getItem(3).equals("choice2"))
{
Button B3=new Button("hao!");
TestChoiceFrame.this.add(B3,BorderLayout.WEST);
}
}
});
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
TestChoiceFrame.this.windowClosed();
}
}
);
}
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
public static void main(String[] args) {
// Create application frame.
TestChoiceFrame frame = new TestChoiceFrame();
// Show frame.
frame.setVisible(true);
}
}
上面是我自己加的监听器,运行的时候出错,但我绞尽脑汁也没想起如何加入正确的监听
求哪个大哥帮忙为小弟指点迷津! 立刻送分!!
import java.awt.event.*;
import javax.swing.*;public class TestChoiceFrame extends JFrame {
Choice C=new Choice();
public TestChoiceFrame() {
C.add("choice1");
C.add("choice2");
C.add("choice3");
setLayout(new FlowLayout());
add(C);
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
menuFile.setText("File");
menuFileExit.setText("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
TestChoiceFrame.this.windowClosed();
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("TestChoice");
setJMenuBar(menuBar);
setSize(new Dimension(400, 400));
// Add window listener.
C.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(C.getItem(1).equals("choice1"))
{
Button B1=new Button("nihao!");
TestChoiceFrame.this.add(B1,BorderLayout.SOUTH);
}
else if(C.getItem(2).equals("choice2"))
{
Button B2=new Button("dajiahao!");
TestChoiceFrame.this.add(B2,BorderLayout.NORTH);
}
else if(C.getItem(3).equals("choice2"))
{
Button B3=new Button("hao!");
TestChoiceFrame.this.add(B3,BorderLayout.WEST);
}
}
});
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
TestChoiceFrame.this.windowClosed();
}
}
);
}
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
public static void main(String[] args) {
// Create application frame.
TestChoiceFrame frame = new TestChoiceFrame();
// Show frame.
frame.setVisible(true);
}
}
上面是我自己加的监听器,运行的时候出错,但我绞尽脑汁也没想起如何加入正确的监听
求哪个大哥帮忙为小弟指点迷津! 立刻送分!!
我想知道如何设置下拉列表中每一项如何加监听器!!!
给我改错就行!我加的监听器是:
C.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(C.getItem(1).equals("choice1"))
{
Button B1=new Button("nihao!");
TestChoiceFrame.this.add(B1,BorderLayout.SOUTH);
}
else if(C.getItem(2).equals("choice2"))
{
Button B2=new Button("dajiahao!");
TestChoiceFrame.this.add(B2,BorderLayout.NORTH);
}
else if(C.getItem(3).equals("choice2"))
{
Button B3=new Button("hao!");
TestChoiceFrame.this.add(B3,BorderLayout.WEST);
}
}
});
/**
* @author xiaohai
*
*/
import java.awt.Choice;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;import javax.swing.JFrame;
import javax.swing.JPanel;public class TestMain extends JFrame {
private JPanel panel = null;
private Choice c = null; public TestMain() {
super("choice ActionListion");
panel = new JPanel();
c = new Choice();
c.add("one");
c.add("two");
c.add("three");
c.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) {
if (c.getSelectedItem().equals("one")) {
System.out.println("This is First");
}else if(c.getSelectedItem().equals("two")){
System.out.println("This is Second");
}else if(c.getSelectedItem().equals("three")){
System.out.println("This is Third");
}
}
});
panel.add(c);
this.getContentPane().add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400, 350);
this.setLocationRelativeTo(null);
this.setVisible(true);
} public static void main(String[] args) {
new TestMain();
}
}
在System.out语句中写你要的效果就可以了