import javax.swing.JButton
import javax.swing.JFrame
public class c {
public static void main(String[] args) {
// 你懂的
JFrame jf = new JFrame()
// 你懂的
jf.setSize(300, 400)
// JFrame做为一个画板,他里面可以有控件,有控件就有控件布局的样式,样式为null可以理解为自由布局
jf.setLayout(null)
// JFrame的setVisible的默认值为false,值的意义是是否显示.
jf.setVisible(true)
// 按钮
JButton jb = new JButton("按钮")
// 添加按钮
jf.add(jb)
// 因为上面的布局样式,他设置了x50y80坐标和长100宽30
jb.setBounds(50, 80, 100, 30)
}
}
按照你的要求添加两个按钮的Java程序如下:
package com.sunshine.customerimport 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())
}
}
}
import java.awt.Component\x0d\x0aimport java.awt.event.ActionEvent\x0d\x0aimport java.awt.event.ActionListener\x0d\x0a\x0d\x0aimport javax.swing.*\x0d\x0aimport javax.swing.table.*\x0d\x0a\x0d\x0apublic class ButtonTable extends JFrame{\x0d\x0a\x0d\x0aprivate static final long serialVersionUID = 1L\x0d\x0aprivate JTable table = null\x0d\x0aprivate DefaultTableModel model=null\x0d\x0aprivate JScrollPane js=null \x0d\x0apublic ButtonTable(){\x0d\x0amodel = new DefaultTableModel(3,3)\x0d\x0atable = new JTable(model)\x0d\x0atable.getColumnModel().getColumn(1).setCellEditor(new MyRender())//设置编辑器\x0d\x0atable.getColumnModel().getColumn(1).setCellRenderer(new MyRender() )\x0d\x0ajs = new JScrollPane(table)\x0d\x0a\x0d\x0athis.add(js)\x0d\x0athis.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)\x0d\x0athis.setVisible(true)\x0d\x0athis.setSize(399, 300)\x0d\x0athis.setLocationRelativeTo(null)\x0d\x0a}\x0d\x0a\x0d\x0apublic static void main(String[] args) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0anew ButtonTable()\x0d\x0a}\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a//渲染 器 编辑器\x0d\x0aclass MyRender extends AbstractCellEditor implements TableCellRenderer,ActionListener, TableCellEditor{\x0d\x0a\x0d\x0aprivate static final long serialVersionUID = 1L\x0d\x0aprivate JButton button =null\x0d\x0apublic MyRender(){\x0d\x0abutton = new JButton("确定不?")\x0d\x0abutton.addActionListener(this)\x0d\x0a}\x0d\x0a\x0d\x0a@Override\x0d\x0apublic Object getCellEditorValue() {\x0d\x0a// TODO Auto-generated method stub\x0d\x0areturn null\x0d\x0a}\x0d\x0a\x0d\x0a@Override\x0d\x0apublic Component getTableCellRendererComponent(JTable table, Object value,\x0d\x0aboolean isSelected, boolean hasFocus, int row, int column) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0areturn button\x0d\x0a}\x0d\x0a\x0d\x0a@Override\x0d\x0apublic void actionPerformed(ActionEvent e) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0aJOptionPane.showMessageDialog(null, "渲染器学期", "消息", JOptionPane.OK_OPTION)\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a@Override\x0d\x0apublic Component getTableCellEditorComponent(JTable table, Object value,\x0d\x0aboolean isSelected, int row, int column) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0areturn button\x0d\x0a}\x0d\x0a\x0d\x0a}欢迎分享,转载请注明来源:内存溢出
评论列表(0条)