给窗口添加下拉框
给窗口添加下拉框
gui中下拉框用JComboBox类来实现,以下代码演示添加下拉框的过程:
package com.cxf.gui.downpull;
import javax.swing.*;
import java.awt.*;
public class TestForDownPull {
public static void main(String[] args) {
new MyFrame().init();
}
}
class MyFrame extends JFrame{
public void init(){
setVisible(true);
setBounds(200,200,400,300);
JComboBox jComboBox = new JComboBox();
jComboBox.addItem("now");
jComboBox.addItem("before");
jComboBox.addItem("after");
Container container = this.getContentPane();
container.add(jComboBox);
}
}
输出结果:
依然存在缺陷,需要拉动窗口大小才能显示组件。