Java单击按钮打开一个新窗口

我坐在电脑前大约13个小时,我觉得我的眼睛正在流血。 我发现了一个我称之为GuiGenie的小编辑。 它非常适合用按钮和所有好东西创建窗口。 问题是我想点击我的第一个菜单中的一个按钮,然后打开我制作的另一个菜单。 我刚开始编程4周前,所以我是一个完整的菜鸟。 我有一种感觉,因为主要的方法弄乱了,但我不知道和13个小时坐在这里尝试数百万的东西让我发疯:)这是我到目前为止

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); } } 

如果有人可以帮助我会非常感激!! 我非常尊重你的优秀,因为如果你是这方面的职业选手,你可能比世界上99.9%更聪明。 这东西伤害了我的大脑。

这是你可以做的事情,对于这种情况,你有多个Forms or Windows ,你可以做的是使用一个JPanel ,可以将这个CardLayout设置为LayoutManager ,然后你可以添加两个JPanel并访问它们用同样的方法提供。

使用Absolute Positioning时不要使用setBounds()这实际上不是将组件放入父容器的正确方法。 而是使用setLocation(…)和setSize(…)方法。 考虑不要尽可能多地使用绝对定位。 从Java Docs获得的前一行支持的某些行如下:

虽然可以不使用布局管理器,但是如果可能的话,应该使用布局管理器。 布局管理器可以更轻松地调整依赖于外观的组件外观,不同的字体大小,容器的大小变化以及不同的区域设置。 布局管理器也可以被其他容器以及其他程序轻松地重用。

因为你的程序输出在任何意义上都不是一种舒缓的体验。 Atleast LayoutManager可以使您的工作更加轻松,因为您无需为每个组件指定位置和大小。 尝试浏览布局管理器教程 ,并尽快习惯它们。 他们是真正的救星:-)

以下是从您的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); } } 

这是myPanel类的代码,使用这个:

 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); } }