8

在我的电脑前坐了大约 13 个小时,我觉得我的眼睛在流血。我找到了一个我喜欢的小 gui 编辑器,叫做 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% 的人都聪明。这东西伤了我的大脑。

4

2 回答 2

8

这是您可以做的事情,对于这种情况,Forms or Windows您可以做多种事情,您可以使用 aJPanel可以设置此CardLayoutLayoutManager,然后您可以将两个JPanels 添加到其中并使用提供的方法访问它们相同的。

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

尽管可以不使用布局管理器,但您应该尽可能使用布局管理器。布局管理器可以更轻松地适应依赖外观的组件外观、不同的字体大小、容器的不断变化的大小以及不同的语言环境。布局管理器也可以很容易地被其他容器以及其他程序重用。

因为你的程序的输出在任何意义上都不是一种舒缓的体验。至少 LayoutManager,可以让你的工作更轻松,因为你不需要为每个组件指定位置和大小。尝试浏览Layout Mangers 教程,并尽快熟悉它们。他们是真正的救星:-)

这是取自您的修改后的代码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);       
    }
}
于 2012-06-30T11:33:04.133 回答
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);
}
}
于 2012-06-30T10:19:50.117 回答