我刚刚开始在java中使用接口,我只需要一个窗口就可以做我需要的东西。但我现在想要将两个窗口链接在一起,例如。
第1帧打开。 用户选择button1(输入数据)。 第2帧打开,以便用户输入数据。
第1帧的代码:
import javax.swing.*;
public class Task_3 extends JFrame {
private Button btn1, btn2, btn3;
public Task_3(){
setLayout(new FlowLayout());
btn1 = new Button("Enter data");
add(btn1);
btn2 = new Button("Check who is going");
add(btn2);
btn3 = new Button("View costs");
add(btn3);
setTitle("Event Costs");
setSize(280, 150);
setVisible(true);
// close the window
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0); //calling the method is a must
}
});
}
public static void main(String[] args){
new Task_3();
}
}
第2帧的代码:
import java.awt.*;
import java.awt.event.*;
public class Task1GUI extends Frame implements ActionListener {
private Label lblInput;
private Label lblOutput;
private TextField tfInput;
private TextField tfOutput;
private int sum = 0;
public Task1GUI(){
setLayout( new FlowLayout());
lblInput = new Label("Enter number of students: ");
add(lblInput);
tfInput = new TextField(5);
add(tfInput);
tfInput.addActionListener(this);
lblOutput = new Label("The cost per student is: ");
add(lblOutput);
tfOutput = new TextField(20);
tfOutput.setEditable(false);
add(tfOutput);
setTitle("Task1GUI");
setSize(350, 120);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0); //calling the method is a must
}
});
}
public static void main(String[] args){
new Task1GUI();
}
@Override
public void actionPerformed(ActionEvent evt){
int numOfStudents = Integer.parseInt(tfInput.getText());
int coachCost = 550;
int entrycost = 30;
int totalcost;
int numFree;
int Discount;
int costPerPerson;
if(numOfStudents<45){
totalcost = coachCost+(numOfStudents*30);
numFree = numOfStudents/10;
Discount = numFree*30;
costPerPerson = (totalcost-Discount)/numOfStudents;
tfInput.setText("");
tfOutput.setText(costPerPerson+"");
}
else{
tfOutput.setText("Too mant students entered");
}
}
}
基本上我想帮助将这两个程序链接在一起,以便用户可以打开第一帧选择他们想要做的动作。
直到最近我一直在使用控制台,所以如果我的代码不完美我会道歉但我真的很感激任何帮助。
如果这有帮助的话,我最终希望这个程序https://repl.it/repls/FondAptXeme有一个GUI。
谢谢
答案 0 :(得分:2)
我建议您创建一个新的JFrame并将新JFrame的可见性设置为&#34; true&#34;在按钮的动作监听器中。像这样:
private JFrame secondFrame = new JFrame("My 2nd Window!");
然后在你的button1动作监听器中执行:
secondFrame.setVisible(true);
编辑:
import javax.swing.*;
public class Task_3 extends JFrame {
private Button btn1, btn2, btn3;
private Task1GUI task1Gui = new Task1GUI();
public Task_3(){
setLayout(new FlowLayout());
btn1 = new Button("Enter data");
btn1.addActionListener(this); //this refers to your current frame
add(btn1);
btn2 = new Button("Check who is going");
add(btn2);
btn3 = new Button("View costs");
add(btn3);
setTitle("Event Costs");
setSize(280, 150);
setVisible(true);
// close the window
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0); //calling the method is a must
}
});
}
public static void main(String[] args){
new Task_3();
}
public void actionPerformed(ActionEvent e) {
task1Gui.setVisible(true);
}
}
干杯。
答案 1 :(得分:0)
你有两个程序(2x主程序),但第二个程序(Task1Gui)实际上应该只是你第一堂课中的一个对象。
在Task3中声明Task1Gui并向其中一个按钮添加actionListener
(在这种情况下为btn1):
private Task1Gui enterDataFrame;
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
enterDataFrame= new Task1Gui();
}
});
确保更改Task1Gui的main方法并更改windowListener,以便只关闭和处理窗口,而不是整个程序:
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
dispose();
}
});
另外,我会将tfInput文本字段的actionListener更改为匿名内部类型:
tfInput.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int numOfStudents = Integer.parseInt(tfInput.getText());
int coachCost = 550;
int entrycost = 30;
int totalcost;
int numFree;
int Discount;
int costPerPerson;
if(numOfStudents < 45){
totalcost = coachCost + (numOfStudents * 30);
numFree = numOfStudents / 10;
Discount = numFree * 30;
costPerPerson = (totalcost-Discount)/numOfStudents;
tfInput.setText("");
tfOutput.setText(costPerPerson + "");
}
else {
tfOutput.setText("Too mant students entered");
}
}
});
答案 2 :(得分:0)
我是这样做的,如果需要,可以尝试
JButton btnNewButton_5 =新的JButton(“SiparişVer”);
setTheme(selectedTheme.theme);