【问题标题】:Opening a new window on a button click in java在java中单击按钮打开一个新窗口
【发布时间】:2018-01-17 10:24:52
【问题描述】:

我刚开始在 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。

谢谢

【问题讨论】:

  • 您的具体问题是什么?
  • 我想让用户从第一个窗口中选择一个按钮,然后依次打开第二个窗口,他们可以在其中输入数据。
  • 你在实现这个时遇到了什么问题?我明白你想要什么,这是我不明白的地方。
  • 我不知道如何实现它。我可以创建单独的窗口,但如果有意义的话,不要将它们链接在一起。
  • 为什么你认为两者之间存在差异?

标签: java button dialog


【解决方案1】:

我建议创建一个新的 JFrame 并在按钮的动作监听器中将新 JFrame 的可见性设置为“true”。像这样:

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);
    }
}    

干杯。

【讨论】:

  • 您好,感谢您的回复。对不起,但我不确定你的意思是什么,你能更详细地解释一下吗?谢谢
  • 嗨,是的,没问题。所以基本上,如果你想要一个新窗口,你可以根据目的使用 JFrame 或 JDialog。要控制这些框架的可见性,请将可见性属性设置为 true 或 false。如果您对如何将 actionListener 添加到按钮有疑问,请参阅 java 文档中的以下示例:link
  • 好的,所以我现在明白我需要使用框架而不是窗口(对话框),但我正在努力将它们设置为两个单独的框架,然后在单击按钮后调用一个。我知道我需要在按钮上有一个动作监听器,但我再次不确定如何实现这一点。我尝试了一些东西,但每次它只是把所有东西都放在第一帧。
  • @AlexanderJamesStephens,请查看我的更新答案,以便更清楚地了解我想要展示的内容。这应该可以正常工作,如果没有,也许您应该让您的 Task1GUI 类扩展 JFrame 而不是 Frame。
  • 我很高兴朋友!如果您不介意,请将答案标记为正确答案,如果它对您有所帮助,以便其他人将来也可以从中受益。快乐编码! :)
【解决方案2】:

你有两个程序(2x main),虽然第二个(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");
                    }
                }
            });

【讨论】:

  • 哦,你有理由在一个类上使用“JFrame”(swing)而在另一个类上使用“Frame”(awt)吗?
  • 我已经更改了我的帖子,这是正确的吗?唯一的原因是我一直在关注各种教程来尝试理解。我不确定这可能是非常糟糕的技术。
【解决方案3】:

我是这样做的,如果你想试试

JButton btnNewButton_5 = new JButton("Sipariş Ver");

    btnNewButton_5.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            new Final().setVisible(true);
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 2015-09-09
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    相关资源
    最近更新 更多