jframe

Java center text in rectangle

天大地大妈咪最大 提交于 2019-11-26 22:54:20
I use drawString() method to draw string using Graphics, but I want to center my text in a rectangle. How should I do that? I've used this to center text on a JPanel Graphics2D g2d = (Graphics2D) g; FontMetrics fm = g2d.getFontMetrics(); Rectangle2D r = fm.getStringBounds(stringTime, g2d); int x = (this.getWidth() - (int) r.getWidth()) / 2; int y = (this.getHeight() - (int) r.getHeight()) / 2 + fm.getAscent(); g.drawString(stringTime, x, y); Centering text has a lot of "options". Do you center absolutely or based on the base line?? Personally, I prefer the absolute center position, but it

JFrame not presenting any Components

我的未来我决定 提交于 2019-11-26 22:26:49
问题 I am using the following code to create a very simple JFrame , but for some reason it doesn't show any components, just a blank frame. Why is this happening? I created frames a bunch of times and I just can't figure out what is wrong. The code is: Main(){ JFrame frame = new JFrame("Colorizer | By: NonameSL"); frame.setSize(400,200); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); frame.setContentPane

JFrame Exit on close Java

强颜欢笑 提交于 2019-11-26 22:26:44
I don't get how can I employ this code: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); to close the program with the x button. You need the line frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Because the default behaviour for the JFrame when you press the X button is the equivalent to frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); So almost all the times you'll need to add that line manually when creating your JFrame I am currently referring to constants in WindowConstants like WindowConstants.EXIT_ON_CLOSE instead of the same constants declared directly

Converting a JFrame to a JApplet

こ雲淡風輕ζ 提交于 2019-11-26 22:09:22
问题 I have a JFrame application working nicely. However now I'd like to run it on the web as an Applet. This is what I've done: import MyPackage.*; import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class MyName extends JApplet { public void init() { setSize(600,450); new MyName() } public MyName() { JShellFrame frame = new JShellFrame(true, null, null); frame.setVisible(true); } } How can I make an html file to run this applet? Also, I have an

How to make a thread not freeze you whole JFrame. JAVA

醉酒当歌 提交于 2019-11-26 22:00:30
问题 Hey i just need a question answered... How would i make the following code not freeze my whole JFrame? try { Thread.sleep(Integer.parseInt(delayField.getText()) * 1000); System.out.println("Hello!"); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } 回答1: use a different thread to perform this task. If you do this in the main UI thread then it will freeze.. For example you can do following new Thread() { @Override public void run() { try { Thread.sleep(Integer.parseInt

Confusion with the Java Swing Timer [closed]

社会主义新天地 提交于 2019-11-26 21:58:48
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I am really confused with the Java swing timer. Does it need an action listener? Could someone please give me an example? Thanks! 回答1: Here is an example of using javax.swing.Timer: import java.awt.BorderLayout;

How can my ActionListener for a JButton access variables in another class?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 21:53:17
问题 I am doing a slot machine for a project. I am having trouble getting my JButton to generate new random numbers from my ArrayList . I can randomize the numbers when the program starts and have an actionlistener set up, but it doesn't do what I need. It was merely for testing purposes. My Actionlistener is in a different Java file. Everything works, I just can't figure out how to generate new randoms in the placeholder of plc1 , plc2 , and plc3 when the button is clicked. I have just recently

JFrame the same shape as an Image / Program running in background

北城余情 提交于 2019-11-26 21:42:26
问题 My question is simple, the solution surely not. I am looking for a way to shape a JFrame the same as an Image it will be displaying. By shape I mean the shape of the pixels that have an alpha != 0. I've already found a working example using a GeneralPath object, but it created ~110000 "nodes" for an Image of about 500*400, so starting the JFrame took more than 2 minutes, which is definitely not the desired effect, the startup should be in under 2 seconds. Thanks for your time. 回答1: I

JavaFX 2.2 Stage always on top

余生颓废 提交于 2019-11-26 21:13:52
问题 I have a normal JFrame (one part of the app) and a second JavaFX window (I can't use a JFrame for the JavaFX stage). The problem is that the JavaFX window should always be on top of all other windows. I have no idea how I should solve this problem! Any ideas? 回答1: I know this is a old thread, but things keep on changing. Coming to JDK 8u20 is a new method primaryStage.setAlwaysOnTop(true); This would be the easiest way to make a stage always on top. For early access to 8u20 visit the website.

JFrame background image

允我心安 提交于 2019-11-26 20:48:38
I am creating a GUI, albeit a simple one, and I want to have a background image (2048 X 2048) fill up the whole window and a square to the left top corner where the occasional 64 X 64 image can be loaded. Thanks in advance to anyone who helps out :) Edit: I already know how to make the JFrame a set size, its the image loading I need help with. Nambi This is a simple example for adding the background image in a JFrame: import javax.swing.*; import java.awt.*; import java.awt.event.*; class BackgroundImageJFrame extends JFrame { JButton b1; JLabel l1; public BackgroundImageJFrame() { setTitle(