按钮光标不会向右显示

时间:2015-05-20 20:28:57

标签: java swing button cursor

我有两个jpanels的jframe。我在第一个jpanel上有一些组件,在第二个按钮上有按钮。当我在第一个面板上单击jbuton(调用方法removeAll)时,它会删除该jpanel中的所有组件(我调用方法revalidate(),repaint()),然后我显示第二个面板包含组件。长话短说,我删除第一个jpanel显示第二个,第二个在jframe上首先显示,... 在jpanel之间切换工作正常,当我切换回第一个jpanel光标手型时按钮不会显示正常光标。手形光标类型工作第一次就好了,但当我切换回第一个jpanel时,它又回到了默认状态......

按钮的实现:

        button = new JButton(icon);
        button.setText("OK");
        button.setHorizontalTextPosition(SwingConstants.LEFT);
        button.setFocusPainted(false);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));

当我切换到第二个jpanel然后回到第一个jpanel时,按钮上的光标当然不会出现("手形光标")。

当我在帧上的jpanels之间切换时,我希望该按钮保持手形光标...

1 个答案:

答案 0 :(得分:4)

有关使用CardLayout进行交换的最佳示例,请检查CardLayout tutorial,但此示例显示交换JPanel后按钮的光标没有问题:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class TestHandCursor extends JPanel {
   private static final int PREF_W = 400;
   private static final int PREF_H = 300;

   // the cardlayout:
   private CardLayout cardLayout = new CardLayout();

   // the cardlayout using JPanel
   private JPanel cardHolderPanel = new JPanel(cardLayout);

   // the action that a button uses to swap cards
   private SwapCardAction swapCardAction = new SwapCardAction("Swap View");
   private JButton swapButton = new JButton(swapCardAction);

   public TestHandCursor() {
      // set up button's cursor
      swapButton.setFocusPainted(false);
      swapButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

      // dummy JPanels to add to the CardLayout-using JPanel
      JPanel panel1 = new JPanel();
      panel1.add(new JLabel("Panel 1"));
      panel1.add(new JButton("Button 1"));

      String[] items = { "Stormy Monday", "Tuesday's Just as Bad",
            "Wednesday's Worse", "Thursday's Oh So Sad" };
      JPanel panel2 = new JPanel();
      panel2.add(new JLabel("Panel 2"));
      panel2.add(new JComboBox<String>(items));

      JPanel panel3 = new JPanel();
      panel3.add(new JLabel("Panel 3"));

      // add above JPanels to the cardHolderPanel:
      cardHolderPanel.add(panel1, "panel 1");
      cardHolderPanel.add(panel2, "panel 2");
      cardHolderPanel.add(panel3, "panel 3");

      // panel to hold JButton
      JPanel bottomPanel = new JPanel();
      bottomPanel.add(swapButton);

      // add all to the main JPanel
      setLayout(new BorderLayout());
      add(cardHolderPanel, BorderLayout.CENTER);
      add(bottomPanel, BorderLayout.PAGE_END);
   }

   // let's make our GUI a little bigger
   @Override
   public Dimension getPreferredSize() {
      Dimension superSize = super.getPreferredSize();
      if (isPreferredSizeSet()) {
         return superSize;
      }

      int prefW = Math.max(superSize.width, PREF_W);
      int prefH = Math.max(superSize.height, PREF_H);
      return new Dimension(prefW, prefH);
   }

   // button Action to swap "cards" in our CardLayout
   private class SwapCardAction extends AbstractAction {
      public SwapCardAction(String name) {
         super(name);
         int mnemonic = (int) name.charAt(0);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         cardLayout.next(cardHolderPanel);
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("TestHandCursor");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new TestHandCursor());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}