0

其实我是java新手我想知道如何链接两个框架。我做了两个不同的java文件,看起来很简单。但直到现在我都无法解决它。它应该通过单击 Enter.java 中的 Next 按钮打开欢迎框架。有谁能够帮我?

这是我的代码:

Enter.java

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Enter extends JFrame {

public Enter() {
// TODO Auto-generated constructor stub

setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));

add(new JLabel("Name"));
add(new JTextField(8));
add(new JLabel("Address"));
add(new JTextField(15));
add(new JLabel("Surname"));
add(new JTextField(8));
add(new JLabel("Phone"));
add(new JTextField(8));
add(new JButton("Next"));
}


/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
    Enter myProg = new Enter();
myProg.setTitle("Ceng 344 - Lab 7");
myProg.setSize(450, 400);
myProg.setLocationRelativeTo(null);
myProg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myProg.setVisible(true);
}

}

Welcome.java
 import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class welcome extends JFrame {

public welcome() {
// TODO Auto-generated constructor stub

setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));

add(new JLabel("Welcome"));
}


/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
welcome myProg = new welcome();
myProg.setTitle("Ceng 344 - Lab 7");
myProg.setSize(450, 400);
myProg.setLocationRelativeTo(null);
myProg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myProg.setVisible(true);
}

}
4

2 回答 2

0

如果你需要Welcome从Button调用Frame Next,这样你就可以给这个按钮添加动作。

尝试这个:

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Main extends JFrame {

    public Main() {
// TODO Auto-generated constructor stub

        setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));

        add(new JLabel("Name"));
        add(new JTextField(8));
        add(new JLabel("Address"));
        add(new JTextField(15));
        add(new JLabel("Surname"));
        add(new JTextField(8));
        add(new JLabel("Phone"));
        add(new JTextField(8));
        JButton next = new JButton("Next");
        add(next);
        next.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NextActionPerformed(evt);
            }

            private void NextActionPerformed(ActionEvent evt) {
                welcome myProg = new welcome();
                myProg.setTitle("Ceng 344 - Lab 7");
                myProg.setSize(450, 400);
                myProg.setLocationRelativeTo(null);
                myProg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                myProg.setVisible(true);
            }
        });



    }

    /**
     * @param args
     */
    public static void main(String[] args) {
// TODO Auto-generated method stub
        Main myProg = new Main();
        myProg.setTitle("Ceng 344 - Lab 7");
        myProg.setSize(450, 400);
        myProg.setLocationRelativeTo(null);
        myProg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myProg.setVisible(true);
    }
}

class welcome extends JFrame {

    public welcome() {
// TODO Auto-generated constructor stub

        setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
        add(new JLabel("Welcome"));
    }
    /**
     * @param args
     */
}

我删除了 Welcome 的 Main 类,并将其内容添加到NextActionPerformed方法

于 2013-03-06T08:10:27.150 回答
0

首先

  1. 您需要添加ActionListenerJButton
  2. 您可以设置JFrame内部构造函数的属性。
  3. 你不需要main()in welcome.java,只有 `constructor 会做。

输入.java

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Enter extends JFrame implements ActionListener {
public Enter(){
    setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
    add(new JLabel("Name"));
    add(new JTextField(8));
    add(new JLabel("Address"));
    add(new JTextField(15));
    add(new JLabel("Surname"));
    add(new JTextField(8));
    add(new JLabel("Phone"));
    add(new JTextField(8));
    JButton next = new JButton("Next");
    next.addActionListener(this);
    add(next);
}

public void actionPerformed(ActionEvent ae){
    //if your frame contains more than one button then you can check which button is clicked using ae.getSource() or using ae.getActionCommand()
    new Welcome();
}
public static void main(String[] args) {
    Enter myProg = new Enter();
    myProg.setTitle("Ceng 344 - Lab 7");
    myProg.setSize(450, 400);
    myProg.setLocationRelativeTo(null);
    myProg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myProg.setVisible(true);
}
}

//欢迎

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Welcome extends JFrame {
    public Welcome() {
        setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));
        add(new JLabel("Welcome"));
        setTitle("Ceng 344 - Lab 7");
        setSize(450, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
}
于 2013-03-06T08:19:41.350 回答