awt

How can I convert an Icon to an Image

被刻印的时光 ゝ 提交于 2019-11-27 04:40:19
问题 I'm trying to convert an Icon ( javax.swing.Icon ) to an Image ( java.awt.Image ) using this code: private Image iconToImage(Icon icon) { if(icon instanceof ImageIcon) { return ((ImageIcon) icon).getImage(); } else { BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB); icon.paintIcon(null, image.getGraphics(), 0, 0); return image; } } The thing is, the paintIcon function throws a NullPointerException on the image.getGraphics() . For

Position Image in any Screen Resolution

≯℡__Kan透↙ 提交于 2019-11-27 04:08:15
问题 I have a problem with my program. Every time when I change my screen resolution, my image starts to move out of position. Any suggestions on how to make my image stay in the same location even though I change to any other resolution? p2 = new JPanel(); p2.setLayout(new FlowLayout()); ImageIcon img2 = new ImageIcon("C:\\Drum\\Invisible4.png"); jbtn2 = new JLabel(img2); p2.add(jbtn2); add(jbtn2); jbtn2.setSize(jbtn2.getPreferredSize()); jbtn2.setLocation(140, 380); 回答1: I prefer to always try

Get effective screen size from Java

China☆狼群 提交于 2019-11-27 03:58:42
I would like to get the effective screen size. That is: the size of the screen without the taskbar (or the equivalent on Linux/Mac). I am currently using... component.getGraphicsConfiguration().getBounds() ...and subtracting the default taskbar size depending on the OS, but I would like a way that works even if the user has resized/moved the taskbar. mKorbel This could determine the screen size in pixels without the taskbar //size of the screen Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //height of the task bar Insets scnMax = Toolkit.getDefaultToolkit()

Choosing a file in-pane with Swing

两盒软妹~` 提交于 2019-11-27 03:57:16
问题 I'm writing a GUI in Swing where I want to do a file chooser that is in the main window, looking something like the image below: while there seem to be quite a few tutorials on how to write a popup file chooser, i don't see much information on how this type of chooser might be accomplished in swing. also sorry if this has been asked before, i did a good bit of searching around and wan't able to find anything else.. 回答1: JFileChooser actually extends JComponent, so you can use like any other

How to use FileDialog?

∥☆過路亽.° 提交于 2019-11-27 03:47:50
问题 I created an interface and I'd like to add a function that allows user to open a file. I'm using AWT. I don't understand how to use FileDialog. Can you please give me an example or a good link that explain this? 回答1: A complete code example, with file filtering: FileDialog fd = new FileDialog(yourJFrame, "Choose a file", FileDialog.LOAD); fd.setDirectory("C:\\"); fd.setFile("*.xml"); fd.setVisible(true); String filename = fd.getFile(); if (filename == null) System.out.println("You cancelled

Java: using an image as a button

徘徊边缘 提交于 2019-11-27 03:42:37
I would like to use an image as a button in Java, and I tried to do this: BufferedImage buttonIcon = ImageIO.read(new File("buttonIconPath")); button = new JButton(new ImageIcon(buttonIcon)); But this still shows the actual button behind the image, I would only like the image to function as the button, how can I do this? jzd Remove the border like so: button.setBorder(BorderFactory.createEmptyBorder()); and then also the contents 1 : button.setContentAreaFilled(false); 1 : Taken from the solution added to the question by @3sdmx thotheolh A suggestion would be to set the Image as a label and

how to drag and drop files from a directory in java

别说谁变了你拦得住时间么 提交于 2019-11-27 03:41:05
I want to implement dragging and dropping of files from a directory such as someones hard drive but can't figure out how to do it. I've read the java api but it talks of color pickers and dragging and dropping between lists but how to drag files from a computers file system and drop into my application. I tried writing the transferhandler class and a mouse event for when the drag starts but nothing seems to work. Now I'm back to just having my JFileChooser set so drag has been enabled but how to drop? Any info or point in the right direction greatly appreciated. import javax.swing.*; import

Using the coordinate plane in the JFrame

北城以北 提交于 2019-11-27 03:34:20
问题 //I am trying to learn how to draw objects in java. I'm getting better at it, but once I get an image on the screen I am having trouble manipulating it. The numbers I put in don't make sense to how the shapes are turning out. At least to me they don't. In algebra if you increase a number on the x axis it goes to the right and if you increase a number on the y axis it goes up. Thats not whats happening here. Can anyone explain to me how this works? I'm still new to java, so the more

BeginnersBook Java 教程

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 03:29:26
来源: ApacheCN BeginnerBook 翻译项目 译者: 飞龙 协议: CC BY-NC-SA 4.0 贡献指南 本项目需要校对,欢迎大家提交 Pull Request。 请您勇敢地去翻译和改进翻译。虽然我们追求卓越,但我们并不要求您做到十全十美,因此请不要担心因为翻译上犯错——在大部分情况下,我们的服务器已经记录所有的翻译,因此您不必担心会因为您的失误遭到无法挽回的破坏。(改编自维基百科) 目录 Java 基础知识教程 Java 编程简介 Java 虚拟机(JVM),JDK 差异,JRE 和 JVM - 核心 Java 如何编译和运行您的第一个 Java 程序 Java 中的变量 Java 中的数据类型 Java 中的运算符 Java 中的 if 和 if-else 语句 Java 中的 switch-case 语句 Java 中的 for 循环 Java 中的 while 循环 Java 中的 do-while 循环示例 Java continue 语句 Java 中的 break 语句 Java OOP 教程 Java 中的构造函数 - 一个完整的研究 Java - 静态类,块,方法和变量 Java 编程中的继承 Java 中的继承类型:单一,多重,多级和混合 OOP 概念 - 什么是 java 中的聚合? OOP 概念 - java 中的关联是什么? java

How to get the top card in Java's CardLayout

偶尔善良 提交于 2019-11-27 03:18:44
问题 Is it possible to get the top card in Java's CardLayout? I've tried looping through each component to check for visibility with isVisible() but it seems that they're all "visible". Edit: By "top card" I mean the one that's currently at the "top", being displayed, not the first or last cards. Also, I don't know if it helps but I'm looking for a JPanel (or a subclass thereof) Edit: Code snippet for (Component component : getComponents()) { if (component instanceof JPanel && component.isVisible(