JAVA界面添加按钮

JAVA界面添加按钮,第1张

按照你的要求添加两个按钮的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())

  }

 }

}

java当中创建按钮,必须使用java swing来创建,实例如下:

JButton有个构造函数是JButton(Icon icon),就是用来创建带图标的按钮的。

可以这样用,如:

JButton jb = new JButton(new ImageIcon("images/myImage.gif"))

这样得到的就是带图标的按钮了。

import java.awt.Color

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JPanel

import javax.swing.SwingUtilities

public class TestWin extends JFrame implements ActionListener {

private JButton blackBtn=new JButton("Black")

private JButton whiteBtn=new JButton("White")

private JPanel pane=new JPanel()

 public TestWin() {

  this.blackBtn.addActionListener(this)

  this.whiteBtn.addActionListener(this)

  JPanel btnPane=new JPanel()

  btnPane.add(this.blackBtn)

  btnPane.add(this.whiteBtn)

  this.add(btnPane,"North")

  this.add(pane,"Center")

  

  this.setSize(800, 600)

  this.setLocationRelativeTo(null)

  this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)

  this.setVisible(true)

 }

 @Override

 public void actionPerformed(ActionEvent e) {

  Object source=e.getSource()

  if(source==this.blackBtn) {

   this.pane.setBackground(Color.BLACK)

  }else if(source==this.whiteBtn) {

   this.pane.setBackground(Color.WHITE)

  }

 }

 public static void main(String[] args) {

  SwingUtilities.invokeLater(() -> new TestWin())

 }

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11965882.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存