Java点击按钮之后没反应
publicclassBook{privateJFramebook;privateDefaultTableModeltableModel;//表格模型对象intb;dat...
public class Book {
private JFrame book;
private DefaultTableModel tableModel; // 表格模型对象
int b;
database data1;
String[] columnNames;
String[][] bookdata;
private JTable table;
String id, name, sex, pnum, idcard, rnum, day, money;
public Book() {
data1 = new database();
book = new JFrame("预定信息");
book.setAlwaysOnTop(true);
book.setSize(new Dimension(600, 400));
book.setLocation(new Point(300, 150));
book.getContentPane().setLayout(null);
b = data1.getBookNum();
JLabel booknum = new JLabel("当前预定数:" + b);
booknum.setBounds(10, 10, 100, 34);
book.getContentPane().add(booknum);
columnNames = new String[] { "预定号", "姓名", "性别", "手机号", "身份证号", "房间号",
"天数", "保留时间", "金额", "备注", };
bookdata = data1.bookData();
tableModel = new DefaultTableModel(bookdata, columnNames);
table = new JTable(tableModel);
table.setForeground(new Color(106, 90, 205));
table.setGridColor(SystemColor.textHighlight);
// table.setAutoResizeMode(Autoscroll);
table.getTableHeader().setReorderingAllowed(false);
table.getTableHeader().setResizingAllowed(false);
table.setBounds(10, 43, 574, 257);
book.getContentPane().add(table);
// 表格渲染器,使文字居中
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
JScrollPane scrollPane = new JScrollPane(table);// 可滚动
table.setFillsViewportHeight(true);
scrollPane.setBounds(10, 42, 574, 258);
book.getContentPane().add(scrollPane);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int selectedRow = table.getSelectedRow(); // 获得选中行索引
Object oa = tableModel.getValueAt(selectedRow, 0);
Object ob = tableModel.getValueAt(selectedRow, 1);
id = oa.toString();
name = ob.toString();
}
});
JButton exit = new JButton("取消");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
book.dispose();
}
});
exit.setBounds(331, 325, 93, 23);
book.getContentPane().add(exit);
JButton btnNewButton = new JButton("入住");
//就是点击这个按钮之后没反应.
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(table.getSelectedRow()==-1){
JOptionPane.showMessageDialog(null, "请选择客户。");
}else{
int res=JOptionPane.showConfirmDialog(null, "该客户确认入住?");
}
}
});
btnNewButton.setBounds(134, 325, 93, 23);
book.getContentPane().add(btnNewButton);
book.setResizable(false);
book.setVisible(true);
} 展开
private JFrame book;
private DefaultTableModel tableModel; // 表格模型对象
int b;
database data1;
String[] columnNames;
String[][] bookdata;
private JTable table;
String id, name, sex, pnum, idcard, rnum, day, money;
public Book() {
data1 = new database();
book = new JFrame("预定信息");
book.setAlwaysOnTop(true);
book.setSize(new Dimension(600, 400));
book.setLocation(new Point(300, 150));
book.getContentPane().setLayout(null);
b = data1.getBookNum();
JLabel booknum = new JLabel("当前预定数:" + b);
booknum.setBounds(10, 10, 100, 34);
book.getContentPane().add(booknum);
columnNames = new String[] { "预定号", "姓名", "性别", "手机号", "身份证号", "房间号",
"天数", "保留时间", "金额", "备注", };
bookdata = data1.bookData();
tableModel = new DefaultTableModel(bookdata, columnNames);
table = new JTable(tableModel);
table.setForeground(new Color(106, 90, 205));
table.setGridColor(SystemColor.textHighlight);
// table.setAutoResizeMode(Autoscroll);
table.getTableHeader().setReorderingAllowed(false);
table.getTableHeader().setResizingAllowed(false);
table.setBounds(10, 43, 574, 257);
book.getContentPane().add(table);
// 表格渲染器,使文字居中
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setHorizontalAlignment(JLabel.CENTER);
JScrollPane scrollPane = new JScrollPane(table);// 可滚动
table.setFillsViewportHeight(true);
scrollPane.setBounds(10, 42, 574, 258);
book.getContentPane().add(scrollPane);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int selectedRow = table.getSelectedRow(); // 获得选中行索引
Object oa = tableModel.getValueAt(selectedRow, 0);
Object ob = tableModel.getValueAt(selectedRow, 1);
id = oa.toString();
name = ob.toString();
}
});
JButton exit = new JButton("取消");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
book.dispose();
}
});
exit.setBounds(331, 325, 93, 23);
book.getContentPane().add(exit);
JButton btnNewButton = new JButton("入住");
//就是点击这个按钮之后没反应.
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(table.getSelectedRow()==-1){
JOptionPane.showMessageDialog(null, "请选择客户。");
}else{
int res=JOptionPane.showConfirmDialog(null, "该客户确认入住?");
}
}
});
btnNewButton.setBounds(134, 325, 93, 23);
book.getContentPane().add(btnNewButton);
book.setResizable(false);
book.setVisible(true);
} 展开
1个回答
展开全部
因为你的JFrame book 设置成最上层,而JOptionPane 又没有设置parentComponent,所以被book覆盖住了,而且焦点此时又在对话框上,感觉程序卡住了
修改如下:
public void actionPerformed(ActionEvent arg0) {
if(table.getSelectedRow()==-1){
JOptionPane.showMessageDialog(book, "请选择客户。");
}else{
int res=JOptionPane.showConfirmDialog(book, "该客户确认入住?");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询