awt

Creating a GUI with multiple panels and one frame

我们两清 提交于 2019-12-03 21:44:17
I am trying to create a simple GUI. I have a menu bar that is filled with various JMenuItems. Each menu item should link to a different "window". Currently, I am thinking the best way to do this is to create a single frame, and create various JPanels. My ActionListeners will toggle visibility of the different panels, and only one panel should be visible at a time. Is this the best way to go about the task? Or is there a better workaround. Hovercraft Full Of Eels Yes, the best way is to use a CardLayout of which there's a great tutorial (please see the link), and many examples online including

One action listener, two JButtons

元气小坏坏 提交于 2019-12-03 20:21:22
问题 I have two JButtons called "Left" and "Right". The "Left" button moves a rectangle object to the left and the "Right" button moves it to the right. I have one ActionListener in the class that acts as the listener for when either button is clicked. However I want different actions to happen when each are clicked. How can I distinguish, in the ActionListener , between which was clicked? 回答1: Set actionCommand to each of the button. // Set the action commands to both the buttons. btnOne

Screen Capture in Java not capturing whole screen

对着背影说爱祢 提交于 2019-12-03 20:20:22
I have a small piece of code that I use to keep track of time - very simply it takes a picture of my desktop every four minutes so that later I can go back over what I've been up to during the day - It works great, except when I connect to an external monitor - this code only takes a screen shot of my laptop screen, not the larger external monitor I'm working from - any ideas how to change the code? I'm running OSX in case that's relevant... import java.awt.AWTException; import java.awt.Robot; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java

How do I fade out one image and fade in another (Java)?

元气小坏坏 提交于 2019-12-03 20:08:24
I want to make it so that while one image is fading out, another is fading in. I have two BufferedImages and I'm using AWT. Edit: package com.cgp.buildtown; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.Font; import java.awt.FontFormatException; import java.awt.Graphics; import java.awt.GraphicsEnvironment; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax

How to center a Window in Java?

[亡魂溺海] 提交于 2019-12-03 18:18:46
问题 What's the easiest way to centre a java.awt.Window , such as a JFrame or a JDialog ? 回答1: From this link If you are using Java 1.4 or newer, you can use the simple method setLocationRelativeTo(null) on the dialog box, frame, or window to center it. 回答2: This should work in all versions of Java public static void centreWindow(Window frame) { Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2); int y = (int) (

Java Robot class simulating human mouse movement

谁说胖子不能爱 提交于 2019-12-03 17:45:46
问题 I am working on a project about remote control, send conrdinate x and y of cursor from client to server. But robot.mouseMove(x,y); will only move the cursor to the particular point without moving the cursor form origional point I have find this simple algorthim to simulate the continuing movement of mouse for (int i=0; i<100; i++){ int x = ((end_x * i)/100) + (start_x*(100-i)/100); int y = ((end_y * i)/100) + (start_y*(100-i)/100); robot.mouseMove(x,y); } But this algorthim still too simple,

Disabling 'paste' in a jTextfield

社会主义新天地 提交于 2019-12-03 16:19:42
I have an app that is written in Swing, awt. I want to prevent users from pasting values into the textfields. is there any way to do this without using action listeners? You can just call setTransferHandler with a null parameter like this: textComponent.setTransferHandler(null); This will disable all copy/paste actions on the field. The best way is to remove action associated with CTRL+V keystroke in components ActionMap. Rob The simplest way it to say: textComponent.setEditable(false); This disables cut & paste, but copy is still enabled. public class PastlessJTextField extends JTextField {

How to Change Font Size in drawString Java

最后都变了- 提交于 2019-12-03 14:33:50
问题 How to make the font size bigger in g.drawString("Hello World",10,10); ? 回答1: g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize)); Where fontSize is a int. The API for drawString states that the x and y parameters are coordinates, and have nothing to do with the size of the text. 回答2: Because you can't count on a particular font being available, a good approach is to derive a new font from the current font. This gives you the same family, weight, etc. just larger... Font currentFont = g

Java Glass pane

情到浓时终转凉″ 提交于 2019-12-03 14:15:41
I have a problem. I want to ask you how can I implement GlassPane to paint on it. I mean, if I click mouse button, in mouseClicked event, my transparent glass pane should be created, because I want to see all my components behind glassPane and I can paint on it using mouseDragged event. When I release mouse button my glassPane disappears. I have another question too. When I will paint on glass pane all my components behind them will be refreshing and repainting? Maybe somebody have nice example with glass pane which might help me. How to Create Translucent and Shaped Windows 来源: https:/

How to create a BufferedImage from raw data

折月煮酒 提交于 2019-12-03 13:15:38
I'm trying to get a BufferedImage from raw samples, but I get exceptions about trying to read past the available data range which I just don't understand. What I'm trying to do is: val datasize = image.width * image.height val imgbytes = image.data.getIntArray(0, datasize) val datamodel = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, image.width, image.height, Array(image.red_mask.intValue, image.green_mask.intValue, image.blue_mask.intValue)) val buffer = datamodel.createDataBuffer val raster = Raster.createRaster(datamodel, buffer, new Point(0,0)) datamodel.setPixels(0, 0, image