java的通过点击一个按钮打开一个新窗口 [英] Java Open a new window by clicking a button

查看:143
本文介绍了java的通过点击一个按钮打开一个新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直坐在这里,在我的电脑约13小时,我想我的眼睛都在流血。
我发现了一个小的GUI编辑器我爱称为GuiGenie。
它的工作原理完美的创建与按钮和所有的好东西窗口。
问题是我要点击我的第一个菜单按钮,并把它打开我的其他菜单我的。
我刚开始编程4个星期前,所以我一个完整的小白。
我有一种感觉,它搞乱,因为主要方法了,但我不知道和13几个小时坐在这里试图以百万计的事情是让我发疯了:)
以下是我走到这一步

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

当按钮被pressed,我希望它打开这个新窗口

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

如果有人可以帮助我将AP preciate大大!
我有很多关于你的优点就在那里,因为如果你在这一个职业球员,你可能是世界上聪明的99.9%以上。
这东西伤害了我的大脑。

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.

推荐答案

下面是你可以做的,对于这种情况,你有多个表格或Windows 你可以做的是使用的JP​​anel 它可以有这样的 CardLayout设定,因为它是的LayoutManager ,然后你可以添加两个的JP​​anel s到它,并与由相同的所提供的方法进行访问。

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 JPanels to it and access them with the methods provided by the same.

不要使用的setBounds()时使用 绝对定位这实在是不把组件的正确方法到父容器中。而是使用的setLocation( ...)并的的setSize(...)方法。考虑不使用绝对定位,尽可能地为您服务。赞成从Java文档采取之前所说线一定范围如下:

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.

由于你的程序的输出是真的没有任何意义舒缓体验。 ATLEAST的LayoutManager,可以使这项工作很多更容易为你,因为你不必为指定位置和大小为每个组件。试图通过布局曼格斯教程行走,并且习惯于给他们尽快地。他们是真正的生活储蓄: - )

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 :-)

下面是从你的 SOURCE code截取的修改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);       
    }
}

这篇关于java的通过点击一个按钮打开一个新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆