jframe

Java Block last JFrame from being focused like a JOptionPane

蹲街弑〆低调 提交于 2019-12-11 13:56:03
问题 I got a program. When I clic on a button, it opens a 2nd JFrame. (NB: I do not not want to use a JOptionPane because it is too basic). I would like, just like the JOptionPane, that my first Frame cannot be focusable and if clicked, the 2nd one will blink like the JOP. Basicly: - I want to have the 2nd Jframe to be the focused frame out of the two and the 1st one cannot be clicked until the 2nd is closed. 回答1: You want to: Either use a modal JDialog in place of the second JFrame Or use a

Java Button Action Command

大憨熊 提交于 2019-12-11 12:59:58
问题 I'm creating a simple Minesweeper game in Java. Size 9x9. I create an array of JPanels and an array of buttons; I add each button to its respective JPanel . then i add the JPanels to the JFrame . How do i distinguish between each button on the action event? Here's some of my code: int gridx = 9; int gridy = 9; JButton[] buttons = new JButton[gridx*gridy]; JPanel[] jpanels = new JPanel[gridx*gridy]; public Minesweeper(){ super("Minesweeper"); setLayout(new GridLayout(9,9)); JPanel panel = new

adding background pic to JFrame

别等时光非礼了梦想. 提交于 2019-12-11 12:51:48
问题 I'm trying to follow this answer to add a background picture to a JFrame and I'm getting a weird error. While debugging my url is coming back null and I get a window that pops up saying "Class File Editor" source not found the source attachment does not contain the source for the file Launcher.class you can change the source attachment by clicking Chang Attached Source below. What does that mean? here's the code that I have so far: import java.awt.Graphics; import java.awt.image.BufferedImage

Center a JPanel in a JFrame

你说的曾经没有我的故事 提交于 2019-12-11 12:42:58
问题 How can I put my JPanel in the center of a JFrame without using a layout manager? I want it to be generic for all screen resolutions of course. Thanks, Tomer 回答1: If you don't use layouts ( setLayout(null) ), you need to calculate the location of the JPanel inside the JFrame , like: import java.awt.Color; import javax.swing.*; public class a extends JFrame { public a() { JPanel panel = new JPanel(); panel.setSize(200, 200); panel.setBackground(Color.RED); setSize(400, 400); // JFrame

stopping a jTimer on frame close

大憨熊 提交于 2019-12-11 12:37:20
问题 I have a frame which starts a swingTimer to perform a periodic task. The problem is when I close the frame, the task still continues. I want the swingTimer to stop if the close button is pressed. I have tried specifying EXIT_ON_CLOSE and DISPOSE_ON_CLOSE but these do not work. Does someone know what I should do? Thanks 回答1: Swing Timer has a stop method. You can always call that if the "frame" (JFrame??) ends via a WindowListener. Also, per my tests, the Timer should stop on its own if the

Shapes not drawing in Java

痴心易碎 提交于 2019-12-11 12:19:01
问题 Can anyone help me out and tell me why the rectangle will not appear? The frame runs fine, but no shapes show up. I have tried doing this a couple of different ways, including with two separate classes, but all I get is an empty frame. import java.awt.Color; import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JPanel; public class Surface extends JPanel { public void paintComponent(Graphics2D g) { super.paintComponent(g); g.setColor(Color.RED); g.drawRect(100, 100, 30, 40

JProgressBar painting issue

旧城冷巷雨未停 提交于 2019-12-11 12:18:19
问题 When I set the min, value, and max of my JProgressBar they do not update unless I close the window and reopen it. Picture: Can anyone give me any insight? I am scratching my head on this one. I tested to make sure that the parsing is done correctly(which is is). I tested to just put numbers in directly. Clearly it works, it just does not show the first time the window is opened(which makes me think that if I update the values it will only show the last values. * EDIT * Ladies and Gentleman...

Check what triggered WindowClosing event

六眼飞鱼酱① 提交于 2019-12-11 11:59:01
问题 Is there a way to see what exactly triggered WindowClosing event in a JFrame? At the moment getSource(), it seems to only be returning the JFrame: public void windowClosing(WindowEvent e) { JOptionPane.showMessageDialog(null, "event source: " + e.getSource(), "Test", JOptionPane.OK_OPTION); methodA(); } I want to know this due to the method dispose() triggering a WindowClosing event. So if a button is clicked which invokes methodA() and then dispose(), dispose() triggers a closing event which

JFrame opening empty

与世无争的帅哥 提交于 2019-12-11 11:56:18
问题 I'm trying to create a program and it's not showing anything in JFrame which is rather odd. The code seems to fit but java appears to be confused or something. Here's my code so far: import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.io.IOException; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; public class Img extends JFrame{ /** * */ private

How to Disable/Enable JFrame Component when JInternalFrame isVisible inside JFrame/JDesktopPane?

本秂侑毒 提交于 2019-12-11 11:50:56
问题 How to Disable/Enable JFrame components when JInternalFrame isVisible inside JFrame / JDesktopPane ? For example, make every JInternalFrame1 visible on the JDesktopPane (desktop pane set on the JFrame ) all frame components like JMenuBar / JButton /etc. set disabled. Have a solution? 回答1: If you have a reference to all your components, the best shot is to write a simple method that would disable everything (more or less like a state machine). If you don't have a reference to all of them (like