gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:51:16 26 4
gpt4 key购买 nike

我在电脑前坐了大约 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 或 Windows,您可以做的是使用 JPanel,它可以设置 CardLayout因为它是 LayoutManager,然后您可以向其中添加两个 JPanel 并使用其提供的方法访问它们。

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

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.

因为你的程序的输出在任何意义上都不是一种舒缓的体验。至少 LayoutManager 可以使您的工作更轻松,因为您不必为每个组件指定位置和大小。尝试遍历 Layout Mangers Tutorials ,并尽快适应它们。他们是真正的救星:-)

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

关于Java 通过单击按钮打开一个新窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11273267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com