java中打开一个新窗口_Java单击按钮打开一个新窗口

这是您可以做的事情,对于这种情况,您有多个窗体或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.

因为你的程序输出在任何意义上都不是一种舒缓的体验. Atleast 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);

}

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java Swing库来创建GUI应用程序,其包括打开其他窗口的功能。 以下是一个示例代码,展示了如何在单击按钮打开一个窗口: ```java import javax.swing.*; import java.awt.event.*; public class MainWindow extends JFrame { public MainWindow() { // 设置主窗口属性 setTitle("主窗口"); setSize(300, 200); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); // 添加按钮到主窗口 JButton button = new JButton("打开新窗口"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 创建新窗口 ChildWindow child = new ChildWindow(); child.setVisible(true); } }); getContentPane().add(button); } public static void main(String[] args) { // 创建主窗口 MainWindow main = new MainWindow(); main.setVisible(true); } } class ChildWindow extends JFrame { public ChildWindow() { // 设置子窗口属性 setTitle("子窗口"); setSize(200, 150); setLocationRelativeTo(null); } } ``` 在上面的代码,我们创建了一个名为MainWindow的主窗口,在该窗口添加了一个按钮。当按钮单击时,会创建一个名为ChildWindow的子窗口,并将其设置为可见。 请注意,我们使用了ActionListener接口来处理按钮单击事件。该接口包含一个方法actionPerformed,该方法在按钮单击时自动调用。我们将创建新窗口的代码放在此方法。 在子窗口的构造函数,我们设置了子窗口的属性,例如标题和大小。您可以根据自己的需要添加其他属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值