jframe

Java/Swing: Obtain Window/JFrame from inside a JPanel

孤街醉人 提交于 2019-11-27 11:37:52
How can I get the JFrame in which a JPanel is living? My current solution is to ask the panel for it's parent (and so on) until I find a Window: Container parent = this; // this is a JPanel do { parent = parent.getParent(); } while (!(parent instanceof Window) && parent != null); if (parent != null) { // found a parent Window } Is there a more elegant way, a method in the Standard Library may be? You could use SwingUtilities.getWindowAncestor(...) method that will return a Window that you could cast to your top level type. JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this);

JPanel vs JFrame in Java

馋奶兔 提交于 2019-11-27 11:20:17
I am learning Java gui. The way I learnt to create a window is to inherit or Extend JFrame class and it is good to use it, as JFrame contains all the properties of a Window. Now If I want to add something to this window, I need to use add() method. But Today I came across JPanel which also creates a windows and we can add stuff by jpanelObjec.add() . What is the difference between the two methods? Are they somehow related? You should not extend the JFrame class unnecessarily (only if you are adding extra functionality to the JFrame class) JFrame : JFrame extends Component and Container . It is

FocusEvent doesn't get the last value of JFormattedTextField, How I can get it?

最后都变了- 提交于 2019-11-27 09:29:47
I have two JFormattedTextField objects on my JFrame object. I want a basic Math (addition) by the values of these JFormattedTextField objects. I want it happen when focus lost either the first or the second textfield. But when " focusLost() ", event doesn't get the last value, it gets the previous value. For example; tf1 has 0 and tf2 has 0 at first. I write 2 to tf1 , and when focusLost() , result ( tf1+tf2 ) become still 0. when I change any of them, the result becomes 2 (the previous value) How do I get the last values on focusLost? Here is my code: JFormattedTextField tf1,tf2; NumberFormat

Running a JFrame with a JProgressBar

跟風遠走 提交于 2019-11-27 09:21:39
public void myMethod { MyProgessBarFrame progFrame = new MyProgressBarFrame(); // this is a JFrame progFrame.setVisible(true); // show my JFrame loading // do some processing here while the progress bar is running // ..... progFrame.setvisible(false); // hide my progress bar JFrame } // end method myMethod I have the code above. But when I run it, the do some processing section does not process until I close the progress bar JFrame. How will I show my progress bar and tell Java to continue in the do processing section? You've got a classic problem with concurrency and Swing. Your problem is

Swing , Multiple Screens , Transfer of control

↘锁芯ラ 提交于 2019-11-27 09:20:10
I am developing a swing application. I am using two screens 1.A button from screen1 will launch screen 2. PseudoCode : ScreenA extends JFrame{ onButtonClick(){ Screen2.setVisible(true); } System.out.println("Hai"); } Screen2 extends JFrame{ onButtonClick{ Hide this screen; } } Now the output is : The screen 2 will be displayed 2.Hai will be printed. My Objective : I want to display hai only when a button from screen 2 is clicked and screen 2 dissppears. How do i achieve it ? I tried setting a flag for buttonclicked in screen two. But the program just passes through the condition and goes on to

Java frame not displaying properly during a while loop

旧时模样 提交于 2019-11-27 08:50:43
问题 Here's a serious issue im having with a jFrame. I have one "Main Frame" where everything is done from, however i have another frame with a gif image in it (Loading, that circle that goes round and round), that is used as a splash screen saying "Processing, Please wait", however when i pull through that frame and my while loop runs, the frame goes blank and nothing displays until after the loop. I am assuming that this is due to the while loop consuming computer resources and "blocking" any

JFrame with Background Image and a JPanel

六眼飞鱼酱① 提交于 2019-11-27 08:48:28
问题 I have the following: public class Frame { public static void main(String[] args) { JFrame frame = new JFrame("Frame Demo"); Panel panel = new Panel(); frame.add(panel); frame.setBounds(250,100,800,500); frame.setVisible(true); } } public class Panel extends JPanel { JButton singlePlayerButton; JButton multiPlayerButton; BufferedImage image; Gui gui; public Panel() { gui = new Gui(); add(gui); try { image = ImageIO.read(new File("C:\\Users\\void\\workspace\\BattleShips\\src\\Testumgebung\

KeyListener not working (requestFocus not fixing it)

梦想的初衷 提交于 2019-11-27 08:28:56
问题 I am trying to make my first game without the help a youtube video. I have watched many youtube series about making java games and they never where what I wanted to do, so after learning enough I started making my own game. I know enough Java to get around and so far everything has worked with no issues. I have tried searching for some help but all I could find where people saying the try something along the lines of: setFocusable(true); requestFocus(); This however has not worked for me. I

How do I use CardLayout for my Java Program for Login and Menu Items

被刻印的时光 ゝ 提交于 2019-11-27 08:28:06
问题 I am creating a "Store" program that basically can allow an employee to log in with a username and password I provide. After logging in, the employee can then see a "main menu" with four buttons: Sales Register, PLU Settings, Settings, and Logout. From this screen, the employee can proceed by clicking on any of the buttons to navigate to that screen. I do not want a new window to popup every time a button is clicked, but instead I want there to be some transition (or no transition) to go to

Strange JFrame Behavior

蹲街弑〆低调 提交于 2019-11-27 08:21:21
问题 I have the following program which has some very strange and unwanted behavior when it runs. Its supposed to have two buttons, "Start" and "Stop, but when I click "Start" another button shows up right below "Start". Here's a print screen of what I'm talking about: What am I doing wrong and how do I fix this ugly problem? Here's the code: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random; public class TwoButtonsTest { JFrame frame; Timer timer; boolean