其实我是java新手我想知道如何链接两个帧。我制作了两个不同的java文件,看起来很简单。但到目前为止我无法解决。它应该通过单击Enter.java中的Next Button打开欢迎框架。有人能帮助我吗?
这是我的代码:
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);
}
}
答案 0 :(得分:0)
如果您需要从Welcome
按钮调用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 方法
答案 1 :(得分:0)
首先
ActionListener
添加到JButton
JFrame
的属性。main()
中不需要welcome.java
,只有`构造函数才会这样做
它。Enter.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);
}
}