如何给一个JFrame上的一个按钮添加一个监听,点击按钮之后可以改变另一个frame上一个JTextField中的文本内

如何给一个JFrame上的一个按钮添加一个监听,点击按钮之后可以改变另一个frame上一个JTextField中的文本内,第1张

给你代码,点完show Frmae 以后,不断的点change试试

总得来说,你想改变另一个对象中的值,就需要有另一个对象的句柄(也就是实例)

这里就是frame

再有一个,就是你得有另一个对象的JTextField 的访问权限。

如果另一个对象的JTextField 是privete的,你就要有相应的get/set方法,如果这些都没有,你别想改变了。就是这样。

因为是你在做程序,这些访问关系不还是你说了算吗。

import javax.swing.JFrame

import javax.swing.JButton

import javax.swing.JTextField

import java.awt.event.ActionListener

import java.awt.event.ActionEvent

public class App1 extends JFrame {

private MyFrame frame = null

private String[] mesg = { "hello", "welcome", "wait", "all" }

private int index = 0

public App1() {

getContentPane().setLayout(null)

frame = new MyFrame()

JButton btnNewButton = new JButton("change")

btnNewButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (index == mesg.length) {

index = 0

}

frame.text.setText(mesg[index++])

}

})

btnNewButton.setBounds(12, 62, 116, 21)

getContentPane().add(btnNewButton)

JButton btnNewButton_1 = new JButton("show Frame")

btnNewButton_1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

frame.setVisible(true)

}

})

btnNewButton_1.setBounds(12, 26, 116, 21)

getContentPane().add(btnNewButton_1)

setDefaultCloseOperation(EXIT_ON_CLOSE)

setLocationRelativeTo(null)

setSize(400, 300)

setResizable(false)

setVisible(true)

}

public static void main(String[] args) {

new App1()

}

}

class MyFrame extends JFrame {

JTextField text

public MyFrame() {

getContentPane().setLayout(null)

text = new JTextField()

text.setBounds(12, 25, 91, 21)

getContentPane().add(text)

setDefaultCloseOperation(EXIT_ON_CLOSE)

setSize(200, 200)

setResizable(false)

}

}

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.event.WindowAdapter

import java.awt.event.WindowEvent

import javax.swing.JButton

import javax.swing.JDialog

import javax.swing.JFrame

public class jishiben {

static JFrame frm = new JFrame()

static JDialog jdia = new JDialog(frm)

static JButton but = new JButton("OK")

public static void main(String[] args) {

frm.setVisible(true)

frm.setBounds(100, 100, 200, 200)

hello hello = new hello()

frm.addWindowListener(hello)

frm.addWindowFocusListener(hello)//添加WindowFocusListener

frm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)//改为点X不做任何 *** 作

jdia.setBounds(50, 50, 100, 100)

jdia.add(but)

but.addActionListener(new ButtonAction())//添加点OK,关闭程序

}

private static final class ButtonAction implements ActionListener {

@Override

public void actionPerformed(ActionEvent arg0) {

System.exit(0)

}

}

static class hello extends WindowAdapter {

public void windowClosing(WindowEvent e) {

jdia.setVisible(true)

}

public void windowLostFocus(WindowEvent e) {

frm.setTitle("lost")

}

}

}

//你没有仔细研究,Listener和Adapter的关系,并不是一一对应关系,如WindowAdapter对应WindowFocusListener, WindowListener, WindowStateListener, EventListener。

WindowListener并不会监听windowLostFocus事件。

//第二个问题:

frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

这句话等于说一点X,就关闭程序,所以才显示不出确认对话框。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/8010429.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-12
下一篇2023-04-12

发表评论

登录后才能评论

评论列表(0条)

    保存