我有带有 2 个 jpanels 的 jframe。我在第一个 jpanel 上有一些组件,在第二个 jpanel 上有一些按钮。当我单击第一个面板上的 jbuton(调用方法removeAll)时,它会删除该 jpanel 中的所有组件(我调用方法 revalidate()、repaint()),然后我显示带有组件的第二个面板。长话短说,我删除第一个 jpanel 以显示第二个,然后删除第二个 jpanel 以在 jframe 上显示第一个,... 在 jpanels 之间切换工作正常,当我切换回第一个 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 之间切换时,我希望该按钮保持光标的手型...
最佳答案
有关使用 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();
}
});
}
}
关于java - 按钮光标显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30359770/