gpt4 book ai didi

Java Swing - 按钮不会显示,直到光标悬停在它上面

转载 作者:行者123 更新时间:2023-11-29 08:34:34 26 4
gpt4 key购买 nike

以前我只有 1 个带有鼠标事件的 Canvas ,“按钮”只是当光标在其框内单击时执行方法的坐标。

然后我决定不这样做,只是用 ImageIcons 实现了 JButtons,但这使得我无法将 JButtons 放在我的 Canvas 之上,所以我四处寻找并看到一些主题告诉我使用填满整个屏幕的 JLabel 和 ImageIcon。 (此外,我尝试使用 JPanel 和 PaintComponent 而不是 JLabel 和 ImageIcon 来做同样的事情)

我在 Reddit 上发布了这个,但显然它对其他人有用,但我无法找出为什么它对我不起作用。这是我从实际项目中截取的可行代码:

package hoverProblem;

import java.awt.Canvas;

public class ClientReddit extends Canvas {

public static void main(String args[]) {
ClientReddit client = new ClientReddit();
ClientWindow window = new ClientWindow();
}
}

这是窗口:

package hoverProblem;

import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ClientWindow {

// Attributes
private JFrame frame;
private static Dimension dm = new Dimension();
private String title;
private final Dimension BUTTONSIZE = new Dimension(100, 50);
private final Dimension MENUSIZE = new Dimension(600, 500);

// Main Menu
private JLabel mainMenuBG = new JLabel(new ImageIcon("graphics/gui/MainMenu.png"));

private ImageIcon loginButtonImg = new ImageIcon("graphics/gui/buttons/LoginButton.png");
private ImageIcon signUpButtonImg = new ImageIcon("graphics/gui/buttons/SignUpButton.png");
private ImageIcon optionsButtonImg = new ImageIcon("graphics/gui/buttons/OptionsButton.png");
private ImageIcon updateButtonImg = new ImageIcon("graphics/gui/buttons/UpdateButton.png");
private ImageIcon creditsButtonImg = new ImageIcon("graphics/gui/buttons/CreditsButton.png");

private JButton loginButton = new JButton(loginButtonImg);
private JButton signUpButton = new JButton(signUpButtonImg);
private JButton optionsButton = new JButton(optionsButtonImg);
private JButton updateButton = new JButton(updateButtonImg);
private JButton creditsButton = new JButton(creditsButtonImg);



// Borders count for some reason
private final int hOffset = 29, wOffset = 6;

public ClientWindow() {
frame = new JFrame();
frame.setLayout(null);

title = "Reddit";
dm.setSize(600 + wOffset, 500 + hOffset);

loginButton.setSize(BUTTONSIZE);
signUpButton.setSize(BUTTONSIZE);
optionsButton.setSize(BUTTONSIZE);
updateButton.setSize(BUTTONSIZE);
creditsButton.setSize(BUTTONSIZE);

loginButton.setLocation(20, 430);
signUpButton.setLocation(135, 430);
optionsButton.setLocation(250, 430);
updateButton.setLocation(365, 430);
creditsButton.setLocation(480, 430);

mainMenuBG.setSize(MENUSIZE);

frame.setTitle(title);
frame.setSize(dm);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setAlwaysOnTop(true);

frame.add(mainMenuBG);
frame.add(loginButton);
frame.add(signUpButton);
frame.add(optionsButton);
frame.add(updateButton);
frame.add(creditsButton);

frame.setVisible(true);
}
}

最佳答案

您错误地覆盖和放置了对象。如果您打算将 JLabel 用作背景图像并希望在其上放置组件,则将实际 add 组件提供给 JLabel,而不是 JFrame,最后仅添加 JLabel到 JFrame。您的另一个选择是在 JPanel 的 paintComponent 方法中绘制背景图像,然后将所有组件添加到其中。

其他问题包括您使用空布局以及设置大小和位置。虽然空布局和 setBounds() 对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,使用它们时遇到的困难就越严重。当 GUI 调整大小时,它们不会调整组件的大小,它们是增强或维护的皇家女巫,当放置在滚动 Pane 中时它们会完全失败,当在所有平台或与原始屏幕分辨率不同的屏幕分辨率上查看时,它们看起来很糟糕.

例如:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ClientPanel extends JPanel {
private static final String COMMON = "https://upload.wikimedia.org/wikipedia/commons/";
private static final String BACKGROUND = "0/0e/Farol_-_Prieto_Coussent.jpg";
private static final String[] BTN_PATHS = {
"thumb/0/09/HanDev.jpg/100px-HanDev.jpg",
"thumb/3/3f/SugababesInEntirety.png/100px-SugababesInEntirety.png",
"thumb/c/c5/GeorgesDanton.jpg/100px-GeorgesDanton.jpg",
"thumb/5/54/Written_on_the_wind8.jpg/100px-Written_on_the_wind8.jpg",
"thumb/6/6d/COP_20000_anverso_%281996-2016%29.jpg/100px-COP_20000_anverso_%281996-2016%29.jpg"
};
private Image background = null;

public ClientPanel() throws IOException {
URL imgUrl = new URL(COMMON + BACKGROUND);
background = ImageIO.read(imgUrl);

JPanel btnPanel = new JPanel(new GridLayout(1, 0, 15, 0));
btnPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
btnPanel.setOpaque(false);
for (String btnPath : BTN_PATHS) {
String imgPath = COMMON + btnPath;
imgUrl = new URL(imgPath);
Image img = ImageIO.read(imgUrl);
Icon icon = new ImageIcon(img);
JButton btn = new JButton(icon);
JPanel wrapperPanel = new JPanel();
wrapperPanel.setOpaque(false);
wrapperPanel.add(btn);
btnPanel.add(wrapperPanel);
}
setLayout(new BorderLayout());
add(btnPanel, BorderLayout.PAGE_END);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
g.drawImage(background, 0, 0, this);
}
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet() || background == null) {
return super.getPreferredSize();
}
int prefW = background.getWidth(this);
int prefH = background.getHeight(this);
return new Dimension(prefW, prefH);
}


private static void createAndShowGui() {
ClientPanel mainPanel = null;
try {
mainPanel = new ClientPanel();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}

JFrame frame = new JFrame("Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

关于Java Swing - 按钮不会显示,直到光标悬停在它上面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45227131/

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