java点击按钮调用另一个程序6_java中怎么实现在点击一个按钮的时候调用其中的一个方法?...

结个长和宽,点击按钮出现周长与面积,这个方法怎么添加?源代码importjavax.swing.*;importjava.awt.GridLayout;publicclassShuzu{publicstaticvoidmain(String[]args){JFramea=newJ...

结个长和宽,点击按钮出现周长与面积,这个方法怎么添加?

源代码

import javax.swing.*;import java.awt.GridLayout;public class Shuzu { public static void main(String[]args){ JFrame a=new JFrame(); a.setVisible(true); a.setTitle("java"); a.setSize(420,320); a.setLocation(200,200); a.setResizable(false); a.setLayout(new GridLayout(10,5)); JLabel b=new JLabel("请输入长方形的长:"); a.add(b); JTextField b1=new JTextField(50); a.add(b1); JLabel c=new JLabel("请输入长方形的宽:"); a.add(c); JTextField c1=new JTextField(50); a.add(c1); JLabel f=new JLabel(); a.add(f); JButton d=new JButton("自动运算"); a.add(d); JLabel e=new JLabel("长方形的周长为:"); a.add(e); JTextField e2=new JTextField(50); a.add(e2); JLabel e1=new JLabel("长方形的面积为:"); a.add(e1); JTextField e3=new JTextField(50); a.add(e3); a.setVisible(true); }}

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java的JFrame按钮调用一个窗体并在另一个窗体上添加数据保存到当前窗体,你可以使用以下步骤: 1. 创建一个新的JFrame或JDialog,这将是你要调用的第二个窗体。 2. 在你的第一个JFrame创建一个按钮。 3. 在按钮的ActionListener实例化你的第二个窗体,并将第一个窗体作为参数传递给第二个窗体。 4. 在第二个窗体添加数据,并将数据返回给第一个窗体。 5. 在第一个窗体的ActionListener获取第二个窗体返回的数据,并将其添加到第一个窗体。 下面是一些代码示例: ``` import javax.swing.*; import java.awt.event.*; public class MyFrame extends JFrame { private JTextField textField; public MyFrame() { textField = new JTextField(20); JButton button = new JButton("Open Second Frame"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { SecondFrame secondFrame = new SecondFrame(MyFrame.this); secondFrame.setVisible(true); } }); add(textField); add(button); pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); } public void setData(String data) { textField.setText(data); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new MyFrame().setVisible(true); } }); } } class SecondFrame extends JFrame { private MyFrame myFrame; private JTextField textField; public SecondFrame(MyFrame myFrame) { this.myFrame = myFrame; textField = new JTextField(20); JButton button = new JButton("Save and Close"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String data = textField.getText(); myFrame.setData(data); dispose(); } }); add(textField); add(button); pack(); setLocationRelativeTo(null); } } ``` 这段代码创建了一个JFrame,其包含一个文本框和一个按钮。当按钮点击时,它将实例化一个新的JFrame(SecondFrame),并将第一个窗体(MyFrame)作为参数传递给它。在第二个窗体,它包含一个文本框和一个保存按钮。当保存按钮点击时,它将获取文本框的数据,并将其返回给第一个窗体。在第一个窗体,它将获取第二个窗体返回的数据,并将其添加到文本框
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值