当前位置: 代码迷 >> J2SE >> Frame中为什么setLayout(null)时无法显示Choice?解决办法
  详细解决方案

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

热度:189   发布时间:2016-04-24 12:12:35.0
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()吧
  相关解决方案