下拉框、列表框
作者:互联网
下拉框、列表框
①下拉框
源代码:
package cn.ecut.swing.box;
import javax.swing.*;
import java.awt.*;
public class JComboBoxDemo extends JFrame {
public static void main(String[] args) {
new JComboBoxDemo();
}
public JComboBoxDemo(){
//下拉框
JComboBox jComboBox=new JComboBox();
jComboBox.addItem(null);
jComboBox.addItem("Java");
jComboBox.addItem("Linux");
jComboBox.addItem("C++");
Container container=this.getContentPane();
container.add(jComboBox);
this.setVisible(true);
this.setBounds(100,100,400,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
运行结果:
②列表框
源代码:
package cn.ecut.swing.box;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class JListDemo extends JFrame {
public static void main(String[] args) {
new JListDemo();
}
public JListDemo(){
Container container=this.getContentPane();
//列表框
Vector vector=new Vector();
JList jList=new JList(vector);
vector.add("甲");
vector.add("乙");
vector.add("丙");
container.add(jList);
this.setVisible(true);
this.setBounds(100,100,200,200);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
运行结果:
标签:列表框,jComboBox,public,add,vector,new,import,下拉框 来源: https://blog.csdn.net/weixin_45623678/article/details/114112856