Java Open a new window by clicking

We sat here at my computer for about 13 hours, and I think my eyes are bleeding. I found a little gui editor that I love, called GuiGenie. It is great for creating windows with buttons and all the good stuff. The problem is that I want to click on the button in my first menu and open it in my other menu. I am just starting to program 4 weeks ago, so I'm a complete noob. I have a feeling that he gets confused because of the basic methods, but I have no idea, and 13 hours of sitting here, trying millions of things, makes me go crazy :) that's 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); } } 

When the button is pressed, I want it to open this new window

 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 really appreciate it! I really respect you for being professionals, because if you are professionals in this, you are probably smarter than 99.9% of the world. This stuff hurts my brain.

+6
source share
2 answers

Here is what you can do for this situation, when you have several Forms or Windows , what you can do is use a JPanel that can have this CardLayout installed as a LayoutManager , and then you can add two JPanel to it and access them using the methods provided to them.

Do not use setBounds() when using Absolute Positioning , this is not quite the right way to place components in the parent container. Use setLocation (...) and setSize (...) instead . Consider not using Absolute Positioning as much as possible for you. Some of the lines in favor of the previously specified line taken from Java Docs are as follows:

Although you can do without a layout manager, you should use a layout manager, if at all possible. The layout manager makes it easy to adapt to the appearance of components, depending on the appearance, to different font sizes, container sizes, as well as different locales. Layout managers can also be easily used by other containers, as well as other programs.

Since the output of your program is really not a calming experience in any sense. Atleast LayoutManager can make this a lot easier for you, since you do not need to specify a position and size for each component. Try to go through the Layout Layout Tutorials and attach to them as soon as possible. These are real rescuers :-)

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); } } 
+8
source

Here is the code for the myPanel class, use this:

 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"); jcomp4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFrame frame = new JFrame ("MyPanel"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add (new MyPanel2()); frame.pack(); frame.setVisible (true); } }); //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); } } 
+4
source

Source: https://habr.com/ru/post/919384/


All Articles