awt

Centering the entire window Java

孤街浪徒 提交于 2019-11-28 09:08:43
问题 My question is quite simple I guess ... I would like to have my java Frame centered when I run my program. I used the following code : setLocationRelativeTo(null); Problem : This is the top left corner of the frame which is centred but not the entire frame. How can I correct this please, and having the full frame centred? Thank you for your help! 回答1: setLocationRelativeTo(null); ... This is the top left corner of the frame which ... It seems the call is being made at the wrong time, before

Translucent JPopupMenu inside a Translucent Window - alternative?

。_饼干妹妹 提交于 2019-11-28 09:00:59
问题 I'm not sure if this is possible, but is there a way to safely allow popups to be translucent even when the parent container is also translucent? If not, what would be wise alternative to use or extend instead of JPopupMenu ? Note: Translucent refers to a component not 'having a background', similar to the effect of setOpaque(false); . Thanks. From a forum answer by user camickr in 2009: I don't know if transparency painting has changed in 1.6.0_10. Prior to that I believe transparency can

Positioning component inside card layout

元气小坏坏 提交于 2019-11-28 08:56:50
问题 I have a main frame where i want to display object of my NewUser class in center position using card layout.Here is my main class import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CardLayoutDemo implements ItemListener { JPanel cards; //a panel that uses CardLayout final static String BUTTONPANEL = "Card with JButtons"; final static String TEXTPANEL = "Card with JTextField"; public void addComponentToPane(Container pane) { //Put the JComboBox in a JPanel to get a

Using Java to send key combinations

ε祈祈猫儿з 提交于 2019-11-28 08:32:19
As per this previous link ( How to send keyboard outputs ) Java can simulate a key being pressed using the Robot class. However, how could a combination of key presses be simulated? If I wanted to send the combination "alt-123" would this be possible using Robot? The simple answer is yes. Basically, you need to wrap the keyPress/Release of the Alt around the other keyPress/Release s public class TestRobotKeys { private Robot robot; public static void main(String[] args) { new TestRobotKeys(); } public TestRobotKeys() { try { robot = new Robot(); robot.setAutoDelay(250); robot.keyPress(KeyEvent

How to select multiple files using java.awt.FileDialog

橙三吉。 提交于 2019-11-28 08:26:26
问题 Is it possible to select multiple files using java.awt.FileDialog? Because I want my dialog to have the OSX finder interface, I can't use JFileChooser. 回答1: According to Javadoc it seems to be possible in JDK7 (see setMultipleMode(boolean) or getFiles() which returns an array of files). However cross checking with the Javadoc for JDK6 it is not possible in older versions... 回答2: You just need to set the options (default is false) setMultiSelectionEnabled(true) http://download.oracle.com

Drawing an image at a point of another image

≡放荡痞女 提交于 2019-11-28 07:52:04
问题 So I'm creating a side scroller and I'm trying to draw an image at the point of another image. I have my background image which is 5000 x 500 and lets say I want to draw an image that's 25x25 at 500, 50 of the background image. How would I do that? So far I've tried: Coins c = new Coins(img.getWidth(this) - 4500, img.getHeight(this) - 250); But this just draws it at 500, 50 of the frame so it "moves" across the image as I scroll to the right. I want, after scroll once to the right, the coin

Java - control Z order of JPanels

吃可爱长大的小学妹 提交于 2019-11-28 07:24:56
问题 In short, my need is to have a background Image in my java app, and upon some event, create some other graphics on top of that image. I was thinking I would use a JPanel to draw the background image in, add it at to my JFrame the start of program, and then add other JPanels on top of that upon certain events. The problem is Swing gives the JPanels added first the highest Z index, so what should be my background is showing up on top of everything. Is there any way to control the Z index/order

Java: Getting resolutions of one/all available monitors (instead of the whole desktop)?

試著忘記壹切 提交于 2019-11-28 07:01:21
问题 I have two different-sized monitors, connected together using (I believe) TwinView. I tried System.out.println(Toolkit.getDefaultToolkit().getScreenSize()); and get java.awt.Dimension[width=2960,height=1050] which is true if you count both monitors together . Instead of this, I would like to be able achieving one of the following: getting resolution of the current monitor getting resolution of the main monitor 回答1: you'll want to use the GraphicsEnvironment. In particular, getScreenDevices()

Java keyboard input - game development

∥☆過路亽.° 提交于 2019-11-28 06:42:17
问题 I have a specific "problem" with a game I'm creating for class. The game is an implementation of "Break it". To move the platform at the bottom I just used a key listener. The problem is that after the first key press there is a short "lag" or "stutter" before the platform starts moving. How could I prevent this to get a smooth response? Is there another way than KeyListener? KeyBindings? Here is the key listener implementation private class KeyControl implements KeyListener { private int dx

Copying to global clipboard does not work with Java in Ubuntu

Deadly 提交于 2019-11-28 05:52:01
The following code from a standalone application works in ubuntu: import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; public class ClipboardTest { public static void main(String[] args) throws Exception { Clipboard clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard(); // print the last copied thing System.out.println(clipBoard.getContents(null).getTransferData(DataFlavor.stringFlavor)); StringSelection