- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我在我正在处理的项目中使用 JFileChooser
时,一切正常,除了一个问题外没有任何问题。
当您在对话框中单击“打开”时,它会更改我的背景,然后 JFileChooser
对话框会再次打开。谁能告诉我我需要做什么才能避免这种情况发生??
下面是我所有的来源..
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class COS extends JPanel implements ActionListener{
static JFrame f=new JFrame();
static Image bgImage=null;
static String message="";
JButton chbg=new JButton("change background");
public COS(){
}
public void paintComponent(Graphics g){
if(bgImage!=null){
g.drawImage(bgImage,0,0,this);
chbg.setBounds(10,10,150,25);
chbg.addActionListener(this);
add(chbg);
}
else{
g.drawString(message,40,40);
}
}
public static void loadbg(){
try{
String xmlpath="background.xml";
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try{
String fimg="";
DocumentBuilder db=dbf.newDocumentBuilder();
Document dom=db.parse(xmlpath);
dom.getDocumentElement().normalize();
NodeList ndlst=dom.getElementsByTagName("background");
Node firstnd=ndlst.item(0);
if(firstnd.getNodeType()==Node.ELEMENT_NODE){
Element firstele=(Element)firstnd;
NodeList firstnamenodelist=firstele.getElementsByTagName("bgimage");
Element firstnamele=(Element)firstnamenodelist.item(0);
NodeList firstname=firstnamele.getChildNodes();
fimg=((Node) firstname.item(0)).getNodeValue();
}
getFileImage(fimg);
} catch(Exception e){
}
} catch(Exception e){
message="File load failed: "+e.getMessage();
}
}
public static void getFileImage(String filein) throws IOException, InterruptedException{
FileInputStream in=new FileInputStream(filein);
byte[] b=new byte[in.available()];
in.read(b);
in.close();
bgImage=Toolkit.getDefaultToolkit().createImage(b);
}
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
JFileChooser jfc=new JFileChooser();
if(source==chbg){
int returnVal=jfc.showOpenDialog(null);
if(returnVal==JFileChooser.APPROVE_OPTION){
File file=jfc.getSelectedFile();
String fileone=file.getName();
changebg(fileone);
}
}
}
public void changebg(String filein){
try{
getFileImage(filein);
saveDefaultImage(filein);
repaint();
} catch(IOException e){
} catch(InterruptedException ie){
}
}
public void saveDefaultImage(String filein){
String newdefbg=filein;
//don't mind this method, i am still working on it...
}
public static void main(String[] args){
COS newcos=new COS();
loadbg();
f.setSize(825,640);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);
newcos.setBounds(5,5,800,600);
f.setLocation(10,5);
f.getContentPane().add(newcos);
f.setVisible(true);
}
}
最佳答案
只是因为您在每次重绘时都添加了新的 Action 监听器。Paint 方法仅用于绘画,没有其他用途。您必须重新考虑您的策略。
关于在选择器对话框中单击打开后,Java JFileChooser 打开备份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1272977/
我正在对 JFileChooser UI 进行一些定制。我正在寻找 JFilechooser 的渐变背景。我所能做的只是一个自定义的 JFilechooser,但我不确定如何将 gardientbac
我想将 JComboBox 添加到 JFileChooser 中。 我尝试扩展 JFileChooser 并手动添加组合框。我实际上设法做到了这一点,但是从 JFileChooser 对话框中删除了文
我试图在文件选择器对话框中打开一个文件,但是,当我打开一个文件或只是关闭对话框时。对话框再次出现,我必须关闭它两次。这是我的代码,不知道有什么问题 private void jMenuItem1Act
如何强制我的 JFileChooser 具有垂直滚动的单列而不是水平滚动的多列? 最佳答案 如果我理解您的要求,您也许可以使用 JList#setLayoutOrientation(JList.VER
所以我得到了这段非常基本的代码,它使用文件名过滤器初始化 JFileChooser。目前您应该只能看到 .txt 文件,稍后我想使用自定义文件名。 JFileChooser dialog = new
我想在我的程序中使用 JFileChooser 来选择目录并处理它。但是,无论我为文件选择器使用什么 FileFilter,选择目录时 Open 按钮都是锁定的。下面是我的 FileFilter 的代
我创建了一个文件,然后需要将其另存为 .docx 扩展名,但我的代码将其另存为 .txt,如何更改扩展名? JFileChooser saveFile = new JFileChooser()
我使用 JFileChooser 让用户保存文件。但我不希望用户选择一个名称来保存文件。不得启用名称文本字段。 我阅读了文档,但没有找到这样的方法或属性。 最佳答案 评论中的Squiddie给你推荐了
我想更改“查看菜单”菜单下的“列表”按钮文本,但我找不到方法。这是文件选择器窗口的图片。 JFileChooser dialog window 我尝试了这些,但没有成功: UIManager.put(
我有以下 Java 8 Swing 代码: JButton button = new JButton("Browse"); button.addActionListener(new ActionLis
继我之前的两篇文章之后here另一个one here ,以下代码打开常规文件浏览器而不是扩展的文件浏览器: public class GuiHandler extends javax.swing.JF
我开发了一个程序,用于计算文件中的行数,如下所示 Scanner in=new Scanner(System.in); System.out.println("Enter the Drive
我的程序允许您保存已制作的图像。我只需要将其另存为 png 即可。 我目前有: ... File file = fc.getSelectedFile(); try{ ImageIO.write(res
我想自定义我的 JFileChooser,这样我不想在底部说“文件夹名称”,而是将其设置为“图片名称”,并且在文本字段中我不希望它根据我所在的位置进行更改我点击,我希望它一直保持空白。无论如何我可以做
在我的 java 应用程序中,有一个浏览按钮。单击浏览按钮时,弹出文件选择器以选择文件。当我通过单击右上角的十字标记而不选择文件来关闭文件选择器时,它会给出一个异常,提示“线程“AWT-EventQu
阅读 documentation 后我没有看到使用 showDialog() 时使对话框模式化的好方法。 . 是否有一种好方法可以使 JFileChooser 实例成为文档模式而不是应用程序模式? 最
我正在使用 JFileChooser,我想让它在单击 fileItem1 时显示文件选择对话框。这是我的 SwingMenu 类。 问题:我在 showFileChooser 方法中遗漏/做错了什么?
我一直在尝试使用 JFileChooser,但遇到程序无法停止运行的问题,这是我的代码: import java.io.File; import javax.swing.JFileChooser; i
如何在屏幕中央显示 JFile-Chooser。使用 ToolKit 的常规方法不起作用。 最佳答案 查看 Javadoc 中的方法 JFileChooser#showDialog(Component
我对 Swing 和 AWT 还很陌生。虽然我有Java开发经验。我正在尝试使用 JFileChooser 读取文件并获取主方法中的内容。所有 Swing 和 AWT 的大师请帮助我确定我缺少什么。
我是一名优秀的程序员,十分优秀!