首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

Frame中为什么setLayout(null)时无法显示Choice?解决办法

2012-01-21 
Frame中为什么setLayout(null)时无法显示Choice????问题同上,代码如下:只是很奇怪!!!谢谢...Java codeimpo

Frame中为什么setLayout(null)时无法显示Choice????
问题同上,代码如下:只是很奇怪!!!谢谢...

Java code
import java.awt.Panel;import java.awt.Button;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.Choice;import java.awt.Frame;import java.awt.event.ActionEvent;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.ActionListener;public class AdvanceSetFrame extends Frame implements ActionListener,        ItemListener {    private Choice timeChoice;    private Button btnEnter, btnCancel;    private Panel panel;    final String enterStr = "确定";    final String cancelStr = "取消";    final static String title = "高级设置";// 当static不标时会有提示于super(title):Cannot    // refer to an instance field title    // while explicitly invoking a    // constructor    public AdvanceSetFrame() {        super(title);        setBounds(500, 300, 400, 200);        setLayout(null);        panel = new Panel();        timeChoice = new Choice();        timeChoice.add("1.0");        timeChoice.add("1.5");        timeChoice.add("2.0");        timeChoice.add("2.5");        timeChoice.add("3.0");        timeChoice.setVisible(true);        timeChoice.addItemListener(this);        panel.add(timeChoice);        add(panel);        setVisible(true);        btnEnter = new Button(enterStr);        btnCancel = new Button(cancelStr);        btnEnter.addActionListener(this);        btnCancel.addActionListener(this);        btnEnter.setBounds(145, 170, 50, 20);        btnCancel.setBounds(205, 170, 50, 20);        add(btnEnter);        add(btnCancel);        addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {                setVisible(false);            }        });    }    public void itemStateChanged(ItemEvent e) {        // TODO Auto-generated method stub        if (e.getItemSelectable() == timeChoice) {//            if (timeChoice.getSelectedIndex() == 0) {//            } else if (timeChoice.getSelectedIndex() == 1) {//            } else if (timeChoice.getSelectedIndex() == 2) {//            } else if (timeChoice.getSelectedIndex() == 3) {//            } else if (timeChoice.getSelectedIndex() == 4) {//            }        }    }    @Override    public void actionPerformed(ActionEvent e) {        // TODO Auto-generated method stub        if(e.getSource() == btnEnter){            this.setVisible(false);        }else if(e.getSource() == btnCancel){            this.setVisible(false);        }    }}public class BeadrollFrame{        public static void main(String []args){            new AdvanceSetFrame();        }}


[解决办法]
使用new FlowLayout()吧

热点排行