
JFileChooser 就行, 参考以下帮助文档JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
choosersetFileFilter(filter);
int returnVal = choosershowOpenDialog(parent);
if(returnVal == JFileChooserAPPROVE_OPTION) {
Systemoutprintln("You chose to open this file: " +
choosergetSelectedFile()getName());
}
就是设置默认 的选项
api中的解释是:
设置 visibleRowCount 属性,对于不同的布局方向,此方法有不同的含义:对于 VERTICAL 布局方向,此方法设置要显示的首选行数(不要求滚动);对于其他方向,它影响单元的包装。 在 VERTICAL 方向上:
设置此属性将影响 getPreferredScrollableViewportSize() 方法(它用于计算封闭视口的首选大小)的返回值。有关更多信息,请参阅该方法的文档。 在 HORIZONTAL_WRAP 和 VERTICAL_WRAP 方向上:
此属性的默认值为 8。 使用负值调用此方法将导致该属性被设置为 0。 这是一个 JavaBeans 绑定属性。
package test;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingDefaultListModel;
import javaxswingJButton;
import javaxswingJList;
import javaxswingWindowConstants;
public class ListFrame extends javaxswingJFrame {
private JList jList1;
private JButton jButton1;
private JButton jButton2;
private JList jList2;
private DefaultListModel listModel1;
private DefaultListModel listModel2;
private JButton jButton4;
private JButton jButton3;
{
//Set Look & Feel
try {
javaxswingUIManagersetLookAndFeel("comsunjavaswingplafwindowsWindowsLookAndFeel");
} catch(Exception e) {
eprintStackTrace();
}
}
/
Auto-generated main method to display this JFrame
/
public static void main(String[] args) {
ListFrame inst = new ListFrame();
instsetVisible(true);
}
public ListFrame() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstantsDISPOSE_ON_CLOSE);
getContentPane()setLayout(null);
{
listModel1 = new DefaultListModel();
listModel1addElement("item 1");
listModel1addElement("item 2");
listModel1addElement("item 3");
listModel1addElement("item 4");
listModel1addElement("item 5");
listModel1addElement("item 6");
jList1 = new JList(listModel1);
getContentPane()add(jList1);
jList1setBounds(42, 28, 119, 203);
}
{
listModel2 = new DefaultListModel();
jList2 = new JList(listModel2);
getContentPane()add(jList2);
jList2setBounds(238, 28, 119, 196);
}
{
jButton1 = new JButton();
getContentPane()add(jButton1);
jButton1setText(">");
jButton1setBounds(168, 84, 63, 28);
jButton1addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
}
{
jButton2 = new JButton();
getContentPane()add(jButton2);
jButton2setText("<");
jButton2setBounds(168, 133, 63, 28);
jButton2addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
}
{
jButton3 = new JButton();
getContentPane()add(jButton3);
jButton3setText("添加");
jButton3setBounds(168, 35, 63, 28);
jButton3addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
}
{
jButton4 = new JButton();
getContentPane()add(jButton4);
jButton4setText("删除");
jButton4setBounds(168, 189, 63, 28);
jButton4addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
}
pack();
setSize(400, 300);
setLocationRelativeTo(null);
} catch (Exception e) {
eprintStackTrace();
}
}
/ jList1移动到jList2/
private void jButton1ActionPerformed(ActionEvent evt) {
if(jList1getSelectedIndex()!=-1){
listModel2addElement(jList1getSelectedValue());
int i=jList1getSelectedIndex();
listModel1remove(i);
jList1setSelectedIndex(i>0 i-1:0);
jList2setSelectedIndex(listModel2size()-1);
}
}
/ jList2移动到jList1/
private void jButton2ActionPerformed(ActionEvent evt) {
if(jList2getSelectedIndex()!=-1){
listModel1addElement(jList2getSelectedValue());
int i=jList2getSelectedIndex();
listModel2remove(i);
jList2setSelectedIndex(i>0 i-1:0);
}
}
/ jList1增加项/
private void jButton3ActionPerformed(ActionEvent evt) {
listModel1addElement("new item");
}
/ jList1删除项/
private void jButton4ActionPerformed(ActionEvent evt) {
if(jList1getSelectedIndex()!=-1){
listModel1remove(jList1getSelectedIndex());
}
}
}
这是一个警告的JDK15,JDK15增加了对泛型的支持,这将有一个集合里面的数据类型检查。如果你使用泛型时,没有指定的信息类型(名单<your对象>列表=新的ArrayList <你的对象>的(),其中,“对象”类型的信息),你可以不检查给定的参数遵守的要求出现unchecked警告,这并不影响程序的运行。
在Java中,要向JList添加ArrayList集合,只需调用其setListData方法即可。
具体添加步骤如下:
1、首先要实例化一个JList对象
2、其次是实例一个ArrayList对象,并向其中添加一些数据
3、再次是将ArrayList对象转换为Object数组,调用其toArray方法即可。
4、最后就是向JList中添加ArrayList了,调用setListData方法将ArrayList转换好的Object数组设置进去即可。
JList是Java GUI编程中的一个重要的列表组件,可以方便地显示对象数组或对象 Vector,不过也仅限于图形界面编程中使用,在web开发或者其他应用开发中,基本上是用不到的。
以上就是关于Java中编写出一个文件选择器。能选择文件夹的。全部的内容,包括:Java中编写出一个文件选择器。能选择文件夹的。、JAVA中 list.setVisibleRowCount(3);是什么意思、java swing 中的列表框JList如何在程序中动态的添加和删除元素等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)