永发信息网

JAVA界面添加按钮

答案:3  悬赏:40  手机版
解决时间 2021-02-25 06:41
  • 提问者网友:末路
  • 2021-02-24 08:15
在下面添加两个按钮 一个是添加房间 一个是删除房间
package com.sunshine.customer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JTableDemo extends JFrame {
private JPanel topPanel;
private JTable table;

public JTableDemo(){
setTitle( "Simple JTable Application" );
setSize(560, 400 );
setBackground( Color.gray );

topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );

String cols[] = {
"房间号",
"是否预定",
"房间价格",
"房间类型",
"房间状态",
"卫生情况",
"剩余时间"

};
String rowData[][] = {
{ "", "", "","","","",""},
{ "", "", "","","","",""},
{ "", "", "","","","",""},
{ "", "", "","","","",""},
{ "", "", "","","","",""},
{ "", "", "","","","",""},
{ "", "", "","","","",""},
{ "", "", "","","","",""},
{ "", "", "","","","",""},
{ "", "", "","","","",""},
{ "", "", "","","","",""},
{ "", "", "","","","",""},
{ "", "", "","","","",""},
};

table = new JTable(rowData, cols);
JScrollPane scrollPane = new JScrollPane(table);
topPanel.add( scrollPane, BorderLayout.CENTER );
}

public static void main( String args[] ) {
JTableDemo mainFrame = new JTableDemo();
mainFrame.setVisible( true );
}

}
最佳答案
  • 五星知识达人网友:封刀令
  • 2021-02-24 08:47
按照你的要求添加两个按钮的Java程序如下:
package com.sunshine.customer;
 import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 
import javax.swing.table.DefaultTableModel;
public class JTableDemo extends JFrame implements ActionListener{    
 private JPanel topPanel;    
 private JTable table;
 private JButton addRoom=new JButton("添加房间");
 private JButton removeRoom=new JButton("删除房间");
 private DefaultTableModel dtm;
 public JTableDemo(){        
  setTitle( "Simple JTable Application" );        
  setSize(560, 400 );        
  setBackground( Color.gray );         
  topPanel = new JPanel();
  topPanel.setLayout( new BorderLayout() );        
  getContentPane().add( topPanel );         
  String cols[] = {"房间号","是否预定","房间价格","房间类型","房间状态","卫生情况","剩余时间"};        
  String rowData[][] = {{ "", "", "","","","",""},
    { "", "", "","","","",""},{ "", "", "","","","",""},{ "", "", "","","","",""},
    { "", "", "","","","",""},{ "", "", "","","","",""},{ "", "", "","","","",""},            
    { "", "", "","","","",""},{ "", "", "","","","",""},{ "", "", "","","","",""},
    { "", "", "","","","",""},{ "", "", "","","","",""},{ "", "", "","","","",""},};
  dtm=new DefaultTableModel();
  dtm.setDataVector(rowData, cols);
  table = new JTable(dtm);        
  JScrollPane scrollPane = new JScrollPane(table);        
  topPanel.add( scrollPane, BorderLayout.CENTER );
  addRoom.addActionListener(this);
  removeRoom.addActionListener(this);
  JPanel p=new JPanel();
  p.add(addRoom);
  p.add(removeRoom);
  topPanel.add( p,BorderLayout.SOUTH );
 }     
 public static void main( String args[] ) {        
  JTableDemo mainFrame = new JTableDemo();        
  mainFrame.setVisible( true );    
 }
 @Override
 public void actionPerformed(ActionEvent ae) {
  if(ae.getSource()==addRoom){
   String row[]={ "", "", "","","","",""};
   dtm.addRow(row);
  }
  if(ae.getSource()==removeRoom){
   dtm.removeRow(table.getSelectedRow());
  }
 }
}
全部回答
  • 1楼网友:纵马山川剑自提
  • 2021-02-24 10:05
考,这么原始,不会~~~
  • 2楼网友:夜风逐马
  • 2021-02-24 09:14
吗急急急急急急急急急急急急急急急急急急
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯