jframe

Java Swing : why must resize frame, so that can show components have added

倾然丶 夕夏残阳落幕 提交于 2019-11-26 09:13:42
问题 I have a simple Swing GUI. (and not only this, all swing GUI I have written). When run it, it doesn\'t show anything except blank screen, until I resize the main frame, so every components have painted again, and I can show them. Here is my simple code : public static void main(String[] args) { JFrame frame = new JFrame(\"JScroll Pane Test\"); frame.setVisible(true); frame.setSize(new Dimension(800, 600)); JTextArea txtNotes = new JTextArea(); txtNotes.setText(\"Hello World\"); JScrollPane

Java center text in rectangle

北城以北 提交于 2019-11-26 09:11:39
问题 I use drawString() method to draw string using Graphics, but I want to center my text in a rectangle. How should I do that? 回答1: 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); 回答2: Centering text has a lot of "options". Do

JFrame Exit on close Java

别等时光非礼了梦想. 提交于 2019-11-26 08:22:19
问题 I don\'t get how can I employ this code: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); to close the program with the x button. 回答1: 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

JFrame background image

佐手、 提交于 2019-11-26 07:43:55
问题 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. 回答1: This is a simple example for adding the background image in a JFrame: import javax.swing.*; import java.awt.*; import java.awt.event.*; class

How to set Icon to JFrame

 ̄綄美尐妖づ 提交于 2019-11-26 06:45:42
问题 I tried this way, but it didnt changed? ImageIcon icon = new ImageIcon(\"C:\\\\Documents and Settings\\\\Desktop\\\\favicon(1).ico\"); frame.setIconImage(icon.getImage()); 回答1: Better use a .png file; .ico is Windows specific. And better to not use a file, but a class resource (can be packed in the jar of the application). URL iconURL = getClass().getResource("/some/package/favicon.png"); // iconURL is null when not found ImageIcon icon = new ImageIcon(iconURL); frame.setIconImage(icon

Java Swing: How can I implement a login screen before showing a JFrame?

假如想象 提交于 2019-11-26 06:03:30
问题 I\'m trying to make a little game that will first show the player a simple login screen where they can enter their name (I will need it later to store their game state info), let them pick a difficulty level etc, and will only show the main game screen once the player has clicked the play button. I\'d also like to allow the player to navigate to a (hopefully for them rather large) trophy collection, likewise in what will appear to them to be a new screen. So far I have a main game window with

Layering multiple GlassPane's in a Root Container

落爺英雄遲暮 提交于 2019-11-26 05:51:11
问题 Is it possible to add multiple GlassPane s for a single JFrame , or do I have to use the uncomfortable LayeredPane with the Opacity attribute. I have attached some code that shows what I want to do (provided by @camickr). import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.border.*; public class MultiplayGlassPane { private static final long serialVersionUID = 1L; private JFrame frame = new JFrame(\"frameTitle\"); private JPanel fPanel =

how can i change the color of titlebar in JFrame?

扶醉桌前 提交于 2019-11-26 05:38:42
问题 I am using the following code, UIManager.put(\"JFrame.activeTitleBackground\", Color.red); for change the toolbar color in JFrame. But it didn\'t work. Is it possible to change the color of titlebar in JFrame? 回答1: Its not possible. The top level JFrame is controlled by the look & feel of the underlying OS. You CAN change the color of an InternalFrame . 回答2: //source : javax/swing/plaf/metal/MetalTitlePane.java import javax.swing.*; import javax.swing.plaf.*; import javax.swing.plaf.metal.*;

Printing a JFrame and its components

好久不见. 提交于 2019-11-26 05:15:01
I have been working in a big program and one of its functionalities should be to print the contents of the main window. I checked the API and found this example: http://docs.oracle.com/javase/tutorial/2d/printing/gui.html it was very helpful, I tried to use that code in my program by placing this inside the actionperformed method of my print button: PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); boolean ok = job.printDialog(); if (ok) { try { job.print(); } catch (PrinterException ex) { /* The job did not successfully complete */ } } If I click the print button, I get a

Close one JFrame without closing another?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 04:46:41
问题 I want to display two (or more) JFrames at the same time. When I close one of them (use the default close button), the other frames should still be visible. How can I do that? 回答1: If you do not want your application to terminate when a JFrame is closed, use setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) instead of setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); From the documentation: DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the