New window with java

New window with java

A Window object is a top-level window with no borders and no menubar. The default layout for a window is BorderLayout . A window must have either a frame, dialog, or another window defined as its owner when it’s constructed. In a multi-screen environment, you can create a Window on a different screen device by constructing the Window with Window(Window, GraphicsConfiguration) . The GraphicsConfiguration object is one of the GraphicsConfiguration objects of the target screen device. In a virtual device multi-screen environment in which the desktop area could span multiple physical screen devices, the bounds of all configurations are relative to the virtual device coordinate system. The origin of the virtual-coordinate system is at the upper left-hand corner of the primary physical screen. Depending on the location of the primary screen in the virtual device, negative coordinates are possible, as shown in the following figure. In such an environment, when calling setLocation , you must pass a virtual coordinate to this method. Similarly, calling getLocationOnScreen on a Window returns virtual device coordinates. Call the getBounds method of a GraphicsConfiguration to find its origin in the virtual coordinate system. The following code sets the location of a Window at (10, 10) relative to the origin of the physical screen of the corresponding GraphicsConfiguration . If the bounds of the GraphicsConfiguration is not taken into account, the Window location would be set at (10, 10) relative to the virtual-coordinate system and would appear on the primary physical screen, which might be different from the physical screen of the specified GraphicsConfiguration .

Window w = new Window(Window owner, GraphicsConfiguration gc); Rectangle bounds = gc.getBounds(); w.setLocation(10 + bounds.x, 10 + bounds.y);

Note: the location and size of top-level windows (including Window s, Frame s, and Dialog s) are under the control of the desktop’s window management system. Calls to setLocation , setSize , and setBounds are requests (not directives) which are forwarded to the window management system. Every effort will be made to honor such requests. However, in some cases the window management system may ignore such requests, or modify the requested geometry in order to place and size the Window in a way that more closely matches the desktop settings. Due to the asynchronous nature of native event handling, the results returned by getBounds , getLocation , getLocationOnScreen , and getSize might not reflect the actual geometry of the Window on screen until the last request has been processed. During the processing of subsequent requests these values might change accordingly while the window management system fulfills the requests. An application may set the size and location of an invisible Window arbitrarily, but the window management system may subsequently change its size and/or location when the Window is made visible. One or more ComponentEvent s will be generated to indicate the new geometry. Windows are capable of generating the following WindowEvents: WindowOpened, WindowClosed, WindowGainedFocus, WindowLostFocus.

Читайте также:  Init method in java thread

Nested Class Summary

Источник

Class JWindow

A JWindow is a container that can be displayed anywhere on the user’s desktop. It does not have the title bar, window-management buttons, or other trimmings associated with a JFrame , but it is still a «first-class citizen» of the user’s desktop, and can exist anywhere on it.

The JWindow component contains a JRootPane as its only child. The contentPane should be the parent of any children of the JWindow . As a convenience, the add , remove , and setLayout methods of this class are overridden, so that they delegate calls to the corresponding methods of the ContentPane . For example, you can add a child component to a window as follows:

And the child will be added to the contentPane. The contentPane will always be non- null . Attempting to set it to null will cause the JWindow to throw an exception. The default contentPane will have a BorderLayout manager set on it. Refer to RootPaneContainer for details on adding, removing and setting the LayoutManager of a JWindow .

Please see the JRootPane documentation for a complete description of the contentPane , glassPane , and layeredPane components.

In a multi-screen environment, you can create a JWindow on a different screen device. See Window for more information.

Warning: Swing is not thread safe. For more information see Swing’s Threading Policy.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see XMLEncoder .

Источник

Java Open a new window by clicking a button

Been sitting here at my computer for about 13 hours and I think my eyes are bleeding. I found a little gui editor I love called GuiGenie. It works perfect for creating the window with the buttons and all that good stuff. The problem is i want to click a button in my first menu and have it open my other menu i made. I just starting programming 4 weeks ago so I’m a complete noob. I have a feeling its messing up because of the main methods but I have no idea and 13 hours of sitting here trying millions of things is making me go crazy : ) here is what i got so far

 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class MyPanel extends JPanel < private JTextField How; private JLabel jcomp2; private JLabel jcomp3; private JButton jcomp4; public MyPanel() < //construct components How = new JTextField (1); jcomp2 = new JLabel ("How long were you parked?"); jcomp3 = new JLabel ("Minutes"); jcomp4 = new JButton ("openNewWindow"); //adjust size and set layout setPreferredSize (new Dimension (315, 85)); setLayout (null); //add components add (How); add (jcomp2); add (jcomp3); add (jcomp4); //set component bounds (only needed by Absolute Positioning) How.setBounds (245, 50, 60, 25); jcomp2.setBounds (35, 30, 185, 50); jcomp3.setBounds (250, 30, 60, 20); jcomp4.setBounds (0, 0, 315, 25); jcomp4.addActionListener( new ActionListener() < public void actionPerformed(ActionEvent e) < >>); > public static void main (String[] args) < JFrame frame = new JFrame ("MyPanel"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add (new MyPanel()); frame.pack(); frame.setVisible (true); >> 
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class MyPanel2 extends JPanel < private JButton jcomp1; private JButton jcomp2; private JButton jcomp3; private JTextField jcomp4; public MyPanel2() < //construct components jcomp1 = new JButton ("test1"); jcomp2 = new JButton ("test2"); jcomp3 = new JButton ("test3"); jcomp4 = new JTextField (5); //adjust size and set layout setPreferredSize (new Dimension (395, 156)); setLayout (null); //add components add (jcomp1); add (jcomp2); add (jcomp3); add (jcomp4); //set component bounds (only needed by Absolute Positioning) jcomp1.setBounds (20, 45, 100, 25); jcomp2.setBounds (135, 60, 100, 25); jcomp3.setBounds (260, 35, 100, 25); jcomp4.setBounds (105, 115, 100, 25); >public static void main (String[] args) < JFrame frame = new JFrame ("MyPanel"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add (new MyPanel2()); frame.pack(); frame.setVisible (true); >> 

If anyone could help I would appreciate it greatly!! I have a lot of respect for you pros out there because if you are a pro at this, you are probably smarter than 99.9% of the world. This stuff hurts my brain.

Stop trying random things, take a break, sleep a good night, and then read the Swing tutorial: docs.oracle.com/javase/tutorial/uiswing. Understand that a JPanel must be enclosed in a JFrame of JDialog to be shown in a new window, and that you should only have one main method in an application. You don’t need two.

2 Answers 2

Here is something you can do, for this situation, where you have multiple Forms or Windows what you can do is to use a JPanel which can have this CardLayout set as it’s LayoutManager and then you can add the two JPanel s to it and access them with the methods provided by the same.

Don’t use setBounds() when using Absolute Positioning this is really not the right way of putting components to the parent container. Instead use setLocation(. ) and setSize(. ) methods. Consider not to use Absolute Positioning as much as possible for you. Certain lines in favour of the before said line taken from Java Docs are as follows :

Although it is possible to do without a layout manager, you should use a layout manager if at all possible. A layout manager makes it easier to adjust to look-and-feel-dependent component appearances, to different font sizes, to a container’s changing size, and to different locales. Layout managers also can be reused easily by other containers, as well as other programs.

Since the output of your program is really not a soothing experience in any sense. Atleast LayoutManager, can make that work a lot more easier for you, since you need not have to specify position and size for each and every component. Try walking through the Layout Mangers Tutorials, and get accustomed to them as soon as possible. They are the real life savers 🙂

Here is a modified code taken from your SOURCE CODE

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CardLayoutExample < private JPanel contentPane; private MyPanel panel1; private MyPanel2 panel2; private void displayGUI() < JFrame frame = new JFrame("Card Layout Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel contentPane = new JPanel(); contentPane.setBorder( BorderFactory.createEmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new CardLayout()); panel1 = new MyPanel(contentPane); panel2 = new MyPanel2(); contentPane.add(panel1, "Panel 1"); contentPane.add(panel2, "Panel 2"); frame.setContentPane(contentPane); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); >public static void main(String. args) < SwingUtilities.invokeLater(new Runnable() < public void run() < new CardLayoutExample().displayGUI(); >>); > > class MyPanel extends JPanel < private JTextField How; private JLabel jcomp2; private JLabel jcomp3; private JButton jcomp4; private JPanel contentPane; public MyPanel(JPanel panel) < contentPane = panel; //construct components How = new JTextField (1); jcomp2 = new JLabel ("How long were you parked?"); jcomp3 = new JLabel ("Minutes"); jcomp4 = new JButton ("openNewWindow"); //adjust size and set layout setPreferredSize (new Dimension (315, 85)); setLayout (null); //set component bounds (only needed by Absolute Positioning) How.setBounds (245, 50, 60, 25); jcomp2.setBounds (35, 30, 185, 50); jcomp3.setBounds (250, 30, 60, 20); jcomp4.setLocation(0, 0); jcomp4.setSize(315, 25); jcomp4.addActionListener( new ActionListener() < public void actionPerformed(ActionEvent e) < CardLayout cardLayout = (CardLayout) contentPane.getLayout(); cardLayout.next(contentPane); >>); //add components add (How); add (jcomp2); add (jcomp3); add (jcomp4); > > class MyPanel2 extends JPanel < private JButton jcomp1; private JButton jcomp2; private JButton jcomp3; private JTextField jcomp4; public MyPanel2() < //construct components jcomp1 = new JButton ("test1"); jcomp2 = new JButton ("test2"); jcomp3 = new JButton ("test3"); jcomp4 = new JTextField (5); //adjust size and set layout setPreferredSize (new Dimension (395, 156)); setLayout (null); //set component bounds (only needed by Absolute Positioning) jcomp1.setBounds (20, 45, 100, 25); jcomp2.setBounds (135, 60, 100, 25); jcomp3.setBounds (260, 35, 100, 25); jcomp4.setBounds (105, 115, 100, 25); //add components add (jcomp1); add (jcomp2); add (jcomp3); add (jcomp4); >> 

Источник

Creating a window with Java Swing

So, I’m new to to the Java language, and I have come to study about the JFrame class. In a lot of tutorials, I see this way of creating a new JFrame window:

class Something extends JFrame < public static void main(String[] args) < new Something(); >Something() < . //Code here, the elements of the default JFrame window. >> 

So my question is: I always have to give the specifications of the JFrame window in the constructor of my class? Isn’t there another way? Or this way is only unfamiliar with me? Also, if I want to change the created JFrame later in the main() method, it seems like my code is a bit anarchic. Thanks for your answer!

GUI programming is very difficult and you should only learn it once you have a grasp on OO principals like encapsulation, inheritance vs composition, then checkout an MVC tutorial maybe: youtube.com/watch?v=dTVVa2gfht8 This guy has heaps of great tutorials (I am not that guy)

2 Answers 2

You can configure your JFrame the same way you can configure any object. If you have a reference to the JFrame, you can call its setters and all non-private methods on it.

Your question touches on the best-practice way to configure objects. I would look for frameworks that handle this for you. In general my belief is any important settings should be externally configurable in a properties file, or system properties (i.e. not code), and configuration should be done by a different object than the one being configured. You want your configuration to be centralized so that all windows look similar etc.

As a beginner to the language you could roll something small to do this for you:

public interface WindowConfigurer < void configure(JFrame window); >public class DefaultWindowConfigurer implements WindowConfigurer < private Font font = // Default font public void configure(JFrame window) < window.setFont(font); >public void setFont(Font font) < this.font = font; >> public class EntryPoint < public static void main(String[] args) < SwingUtilities.invokeLater(new Runnable() < public void run() < Something something = new Something(); WindowConfigurer windowConfigurer = new DefaultWindowConfigurer(); windowConfigurer.configure(something); something.setVisible(true); >>); > > public class Something extends JFrame < public Something() < // .. add buttons, controls, etc. >> 

The interface WindowConfigurer sets up a contract that implementations will «configure» a given JFrame. You can create a default implementation like I’ve done. The great part of abstracting that configuration is that you may want different configurations, and you may even want to change the configuration at runtime. For example what if your user is elderly? You may want to change the font to be bigger or use high contrast colors. So you might make an implementation of WindowConfigurer called WindowConfigurerHighContrast that does anything necessary to the JFrame to make it easy to read. This is the same kind of separation of concerns of HTML and CSS. You should read up on MVC pattern.

For every JFrame in your application, you’ll simply have to run configure() to get the JFrame in the same style as every other window.

Источник

Оцените статью