可以自己定义一个Button
然后在自己定义他的状态不就可以了吗

解决方案 »

  1.   

    对JToggleButton反转按纽做文章!
      

  2.   

    可以自己定义button,也可以在程序中直接写
    JButton b1=new JButton();
    int clickTime=1;click事件函数中:
    clickFunction(){
       clickTime*=-1;
       if(clickTime==1)
           b1.setBorder(BorderFactory.createLoweredBevelBorder());
       else
           b1.setBorder(BorderFactory.createRaisedBevelBorder());
    }
      

  3.   

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    public class keytest extends JFrame implements KeyListener
    {
    private JTextField jt=new JTextField();
    public keytest()
    {
    Container ctp=getContentPane();
    ctp.setLayout(null);
    ctp.add(jt);
    jt.setBounds(20,20,100,30);
    setTitle("keytest");
    setSize(300,300);
    jt.addKeyListener(this);
    addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    }
    public void keyPressed(KeyEvent evt)
    {
    int keyCode =evt.getKeyCode();
    //jt.setText("");
    if (keyCode==KeyEvent.VK_RIGHT && evt.isShiftDown())
        {jt.setText("Shit+->");}
    if (keyCode==KeyEvent.VK_ENTER)  jt.setText(""); 
       //else {jt.setText(""); }
    }
    public void keyTyped(KeyEvent evt){}
    public void keyReleased(KeyEvent evt)
    {
    int keyCode =evt.getKeyCode();
    jt.setText("");
    if (keyCode==KeyEvent.VK_RIGHT && evt.isShiftDown())
        jt.setText("Shit+->");
       //else 
       
    }
    public static void main (String args[])
    {
    JFrame test =new keytest();
    test.setVisible(true);
    }
    }
      

  4.   

    你可以向下面的例子那样自己做button!
    /*
     * @(#)RoundButton.java 1.6 97/06/18 Jeff Dinkins
     *
     * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
     *
     */
    import java.applet.*;
    import java.lang.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;/**
     * RoundButton - a class that produces a lightweight button.
     *
     * Lightweight components can have "transparent" areas, meaning that
     * you can see the background of the container behind these areas.
     *
     */public class RoundButton extends Component {  ActionListener actionListener;     // Post action events to listeners
      String label;                      // The Button's text
      protected boolean pressed = false; // true if the button is detented.
      
        public static void main(String args[]){
            JFrame test=new JFrame();
            Container co=test.getContentPane();
            RoundButton te=new RoundButton();
            co.setLayout(new FlowLayout());
            test.setBackground(Color.black);
            co.add(te);
            test.setSize(300,300);
            test.setVisible(true);
            
            /*
            RoundButton te=new RoundButton();
            //te.pack();
            te.show();*/
       }
      
      /**
       * Constructs a RoundButton with no label.
       */
      public RoundButton() {
          this("");
      }  /**
       * Constructs a RoundButton with the specified label.
       * @param label the label of the button
       */
      public RoundButton(String label) {
          this.label = label;
          enableEvents(AWTEvent.MOUSE_EVENT_MASK);
      }  /**
       * gets the label
       * @see setLabel
       */
      public String getLabel() {
          return label;
      }
      
      /**
       * sets the label
       * @see getLabel
       */
      public void setLabel(String label) {
          this.label = label;
          invalidate();
          repaint();
      }
      
      /**
       * paints the RoundButton
       */
      public void paint(Graphics g) {
          int s = Math.min(getSize().width - 1, getSize().height - 1);
          
          // paint the interior of the button
          if(pressed) {
      g.setColor(getBackground().darker().darker());
          } else {
      g.setColor(getBackground());
          }
          g.fillArc(0, 0, s, s, 0, 360);
          
          // draw the perimeter of the button
          g.setColor(getBackground().darker().darker().darker());
          g.drawArc(0, 0, s, s, 0, 360);
          
          // draw the label centered in the button
          Font f = getFont();
          if(f != null) {
      FontMetrics fm = getFontMetrics(getFont());
      g.setColor(getForeground());
      g.drawString(label,
           s/2 - fm.stringWidth(label)/2,
           s/2 + fm.getMaxDescent());
          }
      }
      
      /**
       * The preferred size of the button. 
       */
      public Dimension getPreferredSize() {
          Font f = getFont();
          if(f != null) {
      FontMetrics fm = getFontMetrics(getFont());
      int max = Math.max(fm.stringWidth(label) + 40, fm.getHeight() + 40);
      return new Dimension(max, max);
          } else {
      return new Dimension(100, 100);
          }
      }
      
      /**
       * The minimum size of the button. 
       */
      public Dimension getMinimumSize() {
          return new Dimension(100, 100);
      }  /**
       * Adds the specified action listener to receive action events
       * from this button.
       * @param listener the action listener
       */
       public void addActionListener(ActionListener listener) {
           actionListener = AWTEventMulticaster.add(actionListener, listener);
           enableEvents(AWTEvent.MOUSE_EVENT_MASK);
       }
     
       /**
        * Removes the specified action listener so it no longer receives
        * action events from this button.
        * @param listener the action listener
        */
       public void removeActionListener(ActionListener listener) {
           actionListener = AWTEventMulticaster.remove(actionListener, listener);
       }  /**
       * Determine if click was inside round button.
       */
       public boolean contains(int x, int y) {
           int mx = getSize().width/2;
           int my = getSize().height/2;
           return (((mx-x)*(mx-x) + (my-y)*(my-y)) <= mx*mx);
       }
       
       /**
        * Paints the button and distribute an action event to all listeners.
        */
       public void processMouseEvent(MouseEvent e) {
           Graphics g;
           switch(e.getID()) {
              case MouseEvent.MOUSE_PRESSED:
        // render myself inverted....
        pressed = true;            // Repaint might flicker a bit. To avoid this, you can use
                // double buffering (see the Gauge example).
        repaint(); 
        break;
              case MouseEvent.MOUSE_RELEASED:
        if(actionListener != null) {
           actionListener.actionPerformed(new ActionEvent(
       this, ActionEvent.ACTION_PERFORMED, label));
        }
        // render myself normal again
        if(pressed == true) {
    pressed = false;                // Repaint might flicker a bit. To avoid this, you can use
                    // double buffering (see the Gauge example).
    repaint();
        }
        break;
              case MouseEvent.MOUSE_ENTERED:     break;
              case MouseEvent.MOUSE_EXITED:
        if(pressed == true) {
    // Cancel! Don't send action event.
    pressed = false;                // Repaint might flicker a bit. To avoid this, you can use
                    // double buffering (see the Gauge example).
    repaint(); // Note: for a more complete button implementation,
    // you wouldn't want to cancel at this point, but
    // rather detect when the mouse re-entered, and
    // re-highlight the button. There are a few state
    // issues that that you need to handle, which we leave
    // this an an excercise for the reader (I always
    // wanted to say that!)
        }
        break;
           }
           super.processMouseEvent(e);
       }}
      

  5.   

    JToggleButton具有这个功能。
    在swing中(就在JButton边上)