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

时间:2012-06-30 10:11:28

标签: java swing button

坐在我的电脑前大约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%的世界更聪明。 这些东西伤害了我的大脑。

2 个答案:

答案 0 :(得分:8)

您可以执行此操作,对于这种情况,您有多个Forms or Windows,您可以执行的操作是使用JPanel设置此CardLayout LayoutManager JPanel 1}}然后你可以添加两个setBounds()并使用它们提供的方法访问它们。

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

  

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

由于您的程序输出在任何意义上都不是一种舒缓的体验。 Atleast LayoutManager可以使您的工作更加轻松,因为您无需为每个组件指定位置和大小。尝试走过Layout Mangers Tutorials,并尽快习惯它们。他们是真正的生活储蓄者: - )

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

中修改的代码
{{1}}

答案 1 :(得分:5)

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