gpt4 book ai didi

java - 按钮光标不正确显示

转载 作者:行者123 更新时间:2023-11-30 07:00:40 27 4
gpt4 key购买 nike

我有带有 2 个 jpanel 的 jframe。我在第一个 jpanel 上有一些组件,在第二个上有按钮。当我在第一个面板上单击 jbuton(调用方法 removeAll)时,它会删除该 jpanel 中的所有组件(我调用方法 revalidate()、repaint()),然后我显示带有组件的第二个面板。长话短说,我删除第一个 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 时,按钮上的光标当然不会出现(“手形光标”)。

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

最佳答案

有关与 CardLayout 交换的最佳示例,请查看 CardLayout tutorial , 但这个例子显示交换 JPanels 后按钮的光标没有问题:

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com