awt

Java - Screen turns black, when setting a JFrame to Fullscreen

给你一囗甜甜゛ 提交于 2019-12-01 14:09:57
I'm trying to draw something on a Canvas, add it to a JFrame and then set this JFrame to Fullscreen. My problem is: in fullscreenmode I only see a black screen. Before the screen turns black I shortly can see the pink background of the canvas. Drawing directly on a JFrame and then setting it to fullscreen works perfectly fine and I can see the testtext. I assume there is a problem with displaying the Canvas properly. Here is my code: public class FullscreenTest extends Canvas { private JFrame mainFrame; public FullscreenTest(){ this.mainFrame = new JFrame(); JPanel contentPane = (JPanel)

How do you simulate a click on a JTextField? Equivalent of JButton doClick()?

爷,独闯天下 提交于 2019-12-01 14:04:58
I am working on a Java project and need to have a keypress simulate a click on a JTextField. What I'm looking for is the equivalent of the JButton doClick() method. I am trying to have the keypress "enter" perform the exact same function as a click on the JTextField. Not sure what other info to provide. Thanks in advance. public void simulateKey(KeyEvent e, Component c) { Field f = KeyEvent.class.getField("focusManagerIsDispatching"); f.setAccessible(true); f.set(e, Boolean.TRUE); c.dispatchEvent(e); } Send "Enter" to your JTextField. This was stolen from here . Vox OK, thanks for the help. I

BUG: Java Swing Key Bindings Lose Function with JDK 7 in OSX with awt setFullScreenWindow

ε祈祈猫儿з 提交于 2019-12-01 13:12:19
EDIT 1/16/2013: The original question has been deleted. This seems to be a bug with the JDK 7 on mac OSX. I have filed a bug report with Sun (Oracle). The file below uses the awt class GraphicsEnvironment and the method setFullScreenWindow to display an image to fullscreen. No images are included, so the screen will be gray when running the code. The key bindings should, however, still work. There are two key bindings. Pressing 'ENTER' should print "Enter was pressed." to stdout. Pressing 'ESCAPE' should print "Program Terminated by ESC Key" to stdout and quit the program. Using Windows 7 64

Java: How do I do a “onclick” for TextField?

蹲街弑〆低调 提交于 2019-12-01 12:24:08
I want to make my text field clear the text when someone clicks it. How can I do this? on java.awt.TextField you can add a MouseListener like so TextField field = new TextField(); field.addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } }); The reason being that java.awt.TextField is a subclass of java.awt.TextComponent (which, in turn, is a subclass of java.awt.Component ). The Component class

FontMetrics.getStringBounds for AttributedString gives wrong result?

只愿长相守 提交于 2019-12-01 11:49:10
As shown in the following picture, an AttributedString is drawn on a JPanel (500X500). The FontMetrics.getStringBounds() of that AttributedString gives a width of 164.0, as indicated by the trace output. java.awt.geom.Rectangle2D$Float[x=0.0,y=-12.064453,w=164.0,h=15.09375] However, the picture suggests the width should be 300-400 (because the width of the panel is 500). Could you help to comment the reason and the workaround ? MyJFrame.java import javax.swing.*; import java.awt.*; import java.awt.font.TextAttribute; import java.text.AttributedString; class MyJPanel extends JPanel { MyJPanel()

Java.awt.Robot keyPress and keyRelease not working at all

徘徊边缘 提交于 2019-12-01 11:21:31
Whenever I try using the java.awt.Robot keyPress or keyRelease, it gives me the error message pid(25807)/euid(501) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!! . No matter how simple I make the code, it keeps giving this error message. This is my code: package com; import java.awt.AWTException; import java.awt.Robot; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; public class JavaRobotExample { public static void main(String[] args) throws AWTException { Robot robot = new Robot(); robot.keyPress

CardLayout, switch between JPanels by ButtonClick

送分小仙女□ 提交于 2019-12-01 09:28:41
I want to switch between JPanels by clicking buttons on the JPanels. For example: I have a JPanel sim with a JButton simknop and a JPanel help with JButton helpknop I want to switch between these 2 JPanels by clicking the buttons. When I click JButton simknop JPanel help should appear and when I click JButton help JPanel sim should appear. Below you can find the different classes: main.java public class main extends JFrame { JPanel cards; sim sim; help help; public main() { this.setSize(1024,768); //this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); this.setDefaultCloseOperation(JFrame

Drag/Moving a shape around jPanel

China☆狼群 提交于 2019-12-01 08:19:21
Yesterday I ask a question about how to draw a bounding box to hold a shape inside and how to drag and drop that selected shape . The first question is solved. But I'm having some trouble to move the shape. Is there any especific transformations to move a shape around the jPanel? I have this code: public boolean drag(MouseEvent e) { if(points.isEmpty()) //if point's vector is empty return false; if(!selected) return false; int x = e.getX(), y = e.getX(); if (!dragging) lastMovePoint.setLocation(x, y); dragging = true; int deslocX = 0; int deslocY = 0; int oldX = -1; int oldY = -1; int size =

Java.awt.Robot keyPress and keyRelease not working at all

和自甴很熟 提交于 2019-12-01 07:58:43
问题 Whenever I try using the java.awt.Robot keyPress or keyRelease, it gives me the error message pid(25807)/euid(501) is calling TIS/TSM in non-main thread environment, ERROR : This is NOT allowed. Please call TIS/TSM in main thread!!! . No matter how simple I make the code, it keeps giving this error message. This is my code: package com; import java.awt.AWTException; import java.awt.Robot; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; public class JavaRobotExample { public

Loading some TrueType Font from TTF file in Java causes FontFormatException: Font name not found

风流意气都作罢 提交于 2019-12-01 07:40:24
I am attempting to create a java.awt.Font instance from a TTF file on my system, but only some fonts are able to load without error. The code below is some test code I found online. When run on my system, it is able to load 285 fonts successfully (ex. Arial.ttf), but fails on 83 fonts, (ex. AmericanTypewriter.ttf). All errors are of the form FontFormatException: Font name not found with no embedded cause. Are there known issues with java.awt.Font and format compatibility? I can't find anything after much Googling. public static void main(String[] args) { String rootPath = "/Library/Fonts";