如何在Java中设计一个按钮,点了之后可以弹出文件选择页面,或者进入某个盘?
答案:1 悬赏:10 手机版
解决时间 2021-02-11 05:38
- 提问者网友:不要迷恋哥
- 2021-02-10 08:19
如何在Java中设计一个按钮,点了之后可以弹出文件选择页面,或者进入某个盘?
最佳答案
- 五星知识达人网友:逐風
- 2021-02-10 08:47
import java.awt.EventQueue;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class ImageViewer {
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame=new ImageViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
}
});
}
}
class ImageViewerFrame extends JFrame
{
public ImageViewerFrame()
{
setTitle("ImageViewer");
setSize(DEFAULT_WIDTH,DEFAULE_HEIGHT);
//use a label to display the image
label =new JLabel();
label.setSize(DEFAULT_WIDTH, DEFAULE_HEIGHT);
add(label);
//set up the file chooser
chooser=new JFileChooser();
chooser.setCurrentDirectory(new File("."));
//set up the menu bar
JMenuBar menuBar =new JMenuBar();
setJMenuBar(menuBar);
JMenu menu=new JMenu("File");
menuBar.add(menu);
JMenuItem openItem=new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
//show file chooser dialog
int result =chooser.showOpenDialog(null);
//if file selected,set it as icon of the label
if(result==JFileChooser.APPROVE_OPTION)
{
String name=chooser.getSelectedFile().getPath();
label.setIcon(new ImageIcon(name));
}
}
});
JMenuItem exitItem=new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
}
private JLabel label;
private JFileChooser chooser;
private static final int DEFAULT_WIDTH=300;
private static final int DEFAULE_HEIGHT=400;
}楼主给你个例子看看吧
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class ImageViewer {
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame=new ImageViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
}
});
}
}
class ImageViewerFrame extends JFrame
{
public ImageViewerFrame()
{
setTitle("ImageViewer");
setSize(DEFAULT_WIDTH,DEFAULE_HEIGHT);
//use a label to display the image
label =new JLabel();
label.setSize(DEFAULT_WIDTH, DEFAULE_HEIGHT);
add(label);
//set up the file chooser
chooser=new JFileChooser();
chooser.setCurrentDirectory(new File("."));
//set up the menu bar
JMenuBar menuBar =new JMenuBar();
setJMenuBar(menuBar);
JMenu menu=new JMenu("File");
menuBar.add(menu);
JMenuItem openItem=new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
//show file chooser dialog
int result =chooser.showOpenDialog(null);
//if file selected,set it as icon of the label
if(result==JFileChooser.APPROVE_OPTION)
{
String name=chooser.getSelectedFile().getPath();
label.setIcon(new ImageIcon(name));
}
}
});
JMenuItem exitItem=new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
}
private JLabel label;
private JFileChooser chooser;
private static final int DEFAULT_WIDTH=300;
private static final int DEFAULE_HEIGHT=400;
}楼主给你个例子看看吧
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯