java实现确认按钮反馈_java – 如何检查JButton是否被按下?如果is...

如何检查JButton是否被按下?我知道有一个方法,它的名字是“isEnabled”

所以我尝试编写一个代码进行测试.

>此代码有2个Jbuttons,分别是“Add”按钮和“Checkout”按钮.

>按下“添加”按钮后按“添加按钮”按钮显示“按下”按钮但是如果在按下“结帐”按钮之前未按下“添加”按钮,代码将显示“代码将显示”,代码将显示“未按下添加按钮”消息.

这里的代码:

final JButton btnAdd = new JButton("Add");

btnAdd.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

}

});

panel.add(btnAdd);

JButton btnConfirm = new JButton("Check Out");

btnConfirm.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (btnAdd.isEnabled()) {

System.out.println("Add Button is pressed");

}

if (!btnAdd.isEnabled()) {

System.out.println("Add Button is not pressed");

}

}

});

当我运行此代码时,代码只提供“按下添加按钮”,虽然我没有按“添加”按钮.为什么会这样发生?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java语言与面向对象程序设计形考4-0001 java语言与面向对象程序设计形考4-0001全文共12页,当前为第1页。java语言与面向对象程序设计形考4-0001全文共12页,当前为第1页。java语言与面向对象程序设计形考4-0001 java语言与面向对象程序设计形考4-0001全文共12页,当前为第1页。 java语言与面向对象程序设计形考4-0001全文共12页,当前为第1页。 试卷总分:100 单选题(共15题,共45分) 开始说明: 结束说明: 1.(3分) 以下哪个是serverSocket类用 接收来自客户端请求的方法? A、accept() B、getOutputStream() C、receive() D、get() 2.(3分) java语言与面向对象程序设计形考4-0001全文共12页,当前为第2页。java语言与面向对象程序设计形考4-0001全文共12页,当前为第2页。以下哪个方法是DatagramSocket类用于发送数据的方法? A、receive() B、accept() C、set() D、send() java语言与面向对象程序设计形考4-0001全文共12页,当前为第2页。 java语言与面向对象程序设计形考4-0001全文共12页,当前为第2页。 3.(3分) 每一个GUI程序中必须包含一个什么组件? A、按钮 B、标签 C、菜单 D、容器 4.(3分) 以下对File类的public boolean isFile()方法的描述,哪个是正确的? A、判断该File对象所对应的是否是文件 B、判断该File对象所对应的是否是目录 C、返回文件的最后修改时间 D、在当前目录下生成指定的目录 5.(3分) java语言与面向对象程序设计形考4-0001全文共12页,当前为第3页。java语言与面向对象程序设计形考4-0001全文共12页,当前为第3页。下面四个组件中哪一个不是component的子类? A、Button B、Dialog C、Label D、MenuBar java语言与面向对象程序设计形考4-0001全文共12页,当前为第3页。 java语言与面向对象程序设计形考4-0001全文共12页,当前为第3页。 6.(3分) 以下哪个类用于实现TCP通信的客户端程序? A、ServerSocket B、Socket C、Client D、server 7.(3分) Java中,图形用户界面的简称是? A、AWT B、GUI C、GIU D、Graph 8.(3分) java语言与面向对象程序设计形考4-0001全文共12页,当前为第4页。java语言与面向对象程序设计形考4-0001全文共12页,当前为第4页。以下哪些不属于InputStream类的方法? A、int read(byte[] b) B、void flush() C、void close() D、int read() java语言与面向对象程序设计形考4-0001全文共12页,当前为第4页。 java语言与面向对象程序设计形考4-0001全文共12页,当前为第4页。 9.(3分) 下面四对AWT和Swing对应组件中,错误的是? A、Button和JButton B、Dia1og和JDialog C、MenuBar和JMenuBar D、ComboBox和JComboBox 10.(3分) 以下哪个负责监听事件源上发生的事件,并对各种事件做出响应? A、事件对象 B、事件处理器 C、事件监听器 D、事件适配器 11.(3分) java语言与面向对象程序设计形考4-0001全文共12页,当前为第5页。java语言与面向对象程序设计形考4-0001全文共12页,当前为第5页。当鼠标按键被释放时,会调用以下哪个事件处理器方法? A、mouseReleased() B、mouseUP() C、mouseOff() D、mouseLetGo() java语言与面向对象程序设计形考4-0001全文共12页,当前为第5页。 java语言与面向对象程序设计形考4-0001全文共12页,当前为第5页。 12.(3分) 使用UDP协议通信时,需要使用哪个类把要发送的数据打包? A、Socket B、DatagramSocket C、DatagramPacket D、ServerSocket 13.(3分) 以下选项中,哪个是File类delete()方法返回值的类型? A、boolean B、int C、String D、Integer java语言与面向对象程序设计形考4-0001全文共12页,当前为第6页。java语言与面向对象程序设计形考4-0001全文共12页,当前为第6页。14.(3分) java语言与面向对象程序设计
Java拼图游戏面向对象课程设计报告 ———————————————————————————————— 作者: ———————————————————————————————— 日期: "姓 名 " "学 号 "20137045 "班 级 " " "Name " "Student No." "Class " " "代码总行数"222 "项目名称 " " "Code Lines" "Item " " "1.实验目的 " "本次课程设计旨在通过一个完整项目的开发,巩固面向对象程序设计、软件工程、 " "数据库技术等课程的相关知识,加深学生对Java语言的理解,尤其是对面向对象思" "想、Java编码规范、JDBC访问数据库的理解,使学生进一步掌握环境构建、代码编 " "写、文档阅读与理解、文档编写、软件测试、发行与部署等技能,进一步提高学生 " "的学习能力、理解能力、表达能力及沟通能力. " "2.实验内容 " "本次课程设计选定的实验项目是使用JAVA语言编写一个拼图游戏并实现其基本功能" "。不用自己手动切割图片,再进行加载.而是应用类方法在内存中进行图片切割和 " "加载,方便了用户的使用。 " "利用了Graphics中的public abstract boolean drawImage()方法把 img 中由 " "(sx1, sy1)(sx2, sy2)指定的矩形区域画到 observer 中由(dx1, dy1)(dx2, " "dy2)指定的矩形区域内进行构思拼图游戏的具体实现。 " "导入了一个可播放背景音乐的jar包,在玩游戏时可以欣赏背景音乐。 " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "3.类图 " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "4.运行界面 " " " "图1、游戏初始界面 " " " "图2、游戏运行界面 " " " " " " " "图3、拼图成功界面 " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "5.关键代码(要求在类的定义以及方法定义的前面给出相关描述信息) " "//定义一个Pintu类 " "public class Jpgsaw extends JFrame{ " "private Image image, buff; //原始图片,缓存图片 " "private int[][] map = {{0, 1, 2}, " "{3, 4, 5}, " "{6, 7, 8} " "}; //定义数组map,给分割后的图片标号 " "private Point point = new Point(2, 2); //记录第九块图片位置 " "private int sx; //分割后每一个图片的宽度 " "private int sy; //分割后每一个图片的高度 " "private Canvas canvas; " "//加载图片容器,定义可以显式定位子元素使用坐标是相对 Canvas " "区域的区域,绘制图形 " "private Graphics gs; //gs画出Canvas图片坐标 " "private Graphics gb; //gb画出buff图像 " "private boolean isRunning = false; //游戏是否正在进行 " "private JButton start = new JButton("开始新的游戏"); // 按钮1 " "private JButton show = new JButton("显示正确图片"); //按钮2 " "private JTextArea showStep = new JTextArea("显示步骤");//设置步骤显示 " "private JPanel panel = new JPanel(); " "//一般轻量级面板容器,装在上面2个按钮 " "private int steps = 0; // 记录移动的步骤 " "public Jpgsaw(String title) { //构造方法,初始化变量 " "super(title);//调用了当前类Jpgsaw的父类JFrame的构造方法 " "try { //异常抛出 " "image = ImageIO.read(new File("gg。jpg")); /
package cn.tedu.chart2; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.JTextField; /** * 客户端窗体 */ public class ClientFrame extends JFrame implements ActionListener, KeyListener { JButton but; JTextArea message; JTextField text; Socket s; ClientThread client; public static void main(String[] args) { new ClientFrame(); } // 在构造函数中对窗体进行初始化 public ClientFrame() { // 取消JFrame的布局 this.setLayout(null); this.setTitle("客户端"); this.setBounds(100, 20, 400, 400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 聊天记录 --- JTextArea message = new JTextArea(); message.setBounds(10, 10, 360, 200); // 设置聊天记录不能修改 message.setEditable(false); this.add(message); // 信息发送 JTextField JButton text = new JTextField(); text.setBounds(10, 260, 250, 40); text.addKeyListener(this); this.add(text); but = new JButton("发送"); but.setBounds(280, 260, 100, 40); // 给but按钮添加动作监听 but.addActionListener(this); this.add(but); this.setVisible(true); // 和服务器取得联系 try { s = new Socket("127.0.0.1", 65000); client = new ClientThread(s, message); client.start(); } catch (Exception e) { e.printStackTrace(); } } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == but) { if (!s.isClosed()) { String data = text.getText(); if (!"".equals(data)) { // 向服务器发送信息 client.send(data); // 清空文本框 text.setText(null); } }else { //在关闭状态下点击发送按钮 JOptionPane.showMessageDialog(null, "Socket连接已关闭,不能发送信息!", "提示", JOptionPane.ERROR_MESSAGE); } } } public void keyTyped(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_ENTER) { if (!s.isClosed()) { String data = text.getText(); if (!"".equals(data)) { // 向服务器发送信息 client.send(data); // 清空文本框 text.setText(null); } }else { //在关闭状态下点击发送按钮 JOptionPane.showMessageDialog(null, "Socket连接已关闭,不能发送信息!", "提示", JOptionPane.ERROR_MESSAGE); } } } public void keyReleased(KeyEvent e) {} }
使用 AbstractTableModel 构建Table 在表格中添加JButton按钮,之前在网上找了2天没有找到好用的程序,最终终于找到一个好用的例子。 不要使,我退你们分。。 sing the Swing JTable class can quickly become a sticky business when you want to customize it to your specific needs. First you must become familiar with how the JTable class is organized. Individual cells are rendered by TableCellRenderer implementations. The table contents are represented by an implementation of the TableModel interface. By default, JTable uses DefaultTableCellRenderer to draw its cells. DefaultTableCellRenderer recognizes a few primitive types, rendering them as strings, and can even display Boolean types as checkboxes. But it defaults to displaying the value returned by toString() for types it does not specifically handle. You have to provide your own TableCellRenderer implementation if you want to display buttons in a JTable. The TableCellRenderer interface contains only one method, getTableCellRendererComponent(...), which returns a java.awt.Component that knows how to draw the contents of a specific cell. Usually, getTableCellRendererComponent() will return the same component for every cell of a column, to avoid the unnecessary use of extra memory. But when the contents of a cell is itself a component, it is all right to return that component as the renderer. Therefore, the first step towards having JButtons display correctly in a JTable is to create a TableCellRenderer implementation that returns the JButton contained in the cell being rendered. In the accompanying code listing, JTableButtonRenderer demonstrates how to do this. Even after creating a custom TableCellRenderer, you're still not done. The TableModel associated with a given JTable does not only keep track of the contents of each cell, but it also keeps track of the class of data stored in each column. DefaultTableModel is designed to work with DefaultTableCellRenderer and will return java.lang.String.class for columns containing data types that it does not specifically handle. The exact method that does this is getColumnClass(int column). Your second step is to create a TableModel implementation that returns JButton.class for cells that contain JButtons. JTableButtonModel shows one way to do this. It just returns the result of getClass() for each piece of cell data. At this point, you're almost done, but not quite. What's the use of putting a JButton in a JTable if you can't press the darn thing? By default, JTable will not forward mouse events to components contained in its cells. If you want to be able to press the buttons you add to JTable, you have to create your own MouseListener that forwards events to the JButton cells. JTableButtonMouseListener demonstrates how you could do this.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值