但是可以通过addWindowListener侦听到此事件,也稿宽可以重写窗口的processWindowEvent方法,拦截关闭事件。
如果一定要自己实现点击叉叉的功能,可以使用setUndecorated(true)方法去掉窗口默认的标题栏,然后再自己画一个标题栏闹历。
答: 可棚蔽桐以给文本域JTextArea添加鼠标点击事件
效果图
参考代码
import java.awt.event.*import javax.swing.*
public class JTFDemo extends JFrame {
public JTFDemo() {
JTextArea jtf1 = new JTextArea()//创建文本域JTextArea
//给文本域添加事件
jtf1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {//当点击文本域时..d出对话框
JOptionPane.showMessageDialog(null, "点击了文本域")
}
})
add(jtf1)
setTitle("测试Demo")// 标题
setSize(280, 180)// 窗口大小
setLocationRelativeTo(null)// 并亏窗口居中链坦
setDefaultCloseOperation(EXIT_ON_CLOSE)// 窗口点击关闭时,退出程序
setVisible(true)// 窗口可见
}
public static void main(String[] args) {
new JTFDemo()
}
}
import java.awt.FlowLayoutimport java.awt.event.ActionEvent
import java.awt.event.ActionListener
import javax.swing.JButton
import javax.swing.JDialog
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JOptionPane
import javax.swing.JPanel
import javax.swing.JScrollPane
import javax.swing.JTextArea
import javax.swing.JTextField
public class MainFrame {
private JFrame frame = new JFrame()
private JButton add = new JButton("添加")
private JButton del = new JButton("删除信宴洞")
private JButton update = new JButton("修改")
private JPanel panel = new JPanel()
private JTextArea message = new JTextArea(10, 20)
private JScrollPane message_pane = new JScrollPane(message)
private JTextField name = null
private JTextField password = null
private JScrollPane update_message_pane = null
private JTextArea update_message = null
public static void main(String[] args) {
MainFrame mf = new MainFrame()
mf.init()
mf.addActionListener()
}
/祥咐/ 向文本域添加值
private class Enroll_BtnAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (!name.getText().equals("") &&!password.getText().equals("")) {
message.append("姓名:" + name.getText() + " " + "密码:"
+ password.getText())
} else {
System.out.println("用户名密码为空")
}
}
}
private class Update_BtnAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
message.setText(update_message.getText())
}
}
// 为按钮添加事件
public void addActionListener() {
ActionListener add_action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog enroll_dialog = new JDialog(frame, "滑枯注册", true)
JLabel name_t = new JLabel("姓名")
name = new JTextField(10)
JLabel password_t = new JLabel("密码")
password = new JTextField(10)
JButton enroll_ok = new JButton("确定")
enroll_dialog.setLayout(new FlowLayout())
enroll_dialog.add(name_t)
enroll_dialog.add(name)
enroll_dialog.add(password_t)
enroll_dialog.add(password)
enroll_dialog.add(enroll_ok)
enroll_dialog.setBounds(20, 10, 180, 350)
enroll_ok.addActionListener(new Enroll_BtnAction())
enroll_dialog.setVisible(true)
}
}
// 清空文本域
ActionListener del_action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
message.setText("")
}
}
ActionListener update_action = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog update_dialog = new JDialog(frame, "修改", true)
update_message= new JTextArea(18,20)
update_message_pane= new JScrollPane(update_message)
JButton update_ok = new JButton("确定")
update_message.setText(message.getText())
update_message.setWrapStyleWord(true)
update_dialog.add(update_message_pane)
update_dialog.add(update_ok)
update_dialog.setLayout(new FlowLayout())
update_dialog.setBounds(600, 100, 240, 400)
update_ok.addActionListener(new Update_BtnAction())
update_dialog.setVisible(true)
}
}
add.addActionListener(add_action)
del.addActionListener(del_action)
update.addActionListener(update_action)
}
// 控件初始化
public void init() {
frame.setBounds(100, 100, 400, 400)
panel.setLayout(new FlowLayout())
panel.add(add)
panel.add(del)
panel.add(update)
panel.add(message_pane)
message.setEditable(false)
frame.add(panel)
frame.setVisible(true)
frame.setDefaultCloseOperation(3)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)