JButton() 仅在鼠标悬停时起作用
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.imageio.*;
import java.lang.*;
import java.io.*;
import javax.swing.*;
public class MainClass extends Component{
private Image bg;
private ImageIcon newgame;
private ImageIcon quit;
private ImageIcon options;
private JButton bquit;
private JButton boptions;
private JButton bnewgame;
private static Container pane; //Container
public void loadImage() {
try {
bg=ImageIO.read(new File("bg1.png"));
} catch (Exception e) {
}
if(bg!=null)
repaint();
}
public void paint(Graphics g) {
g.drawImage(bg,0,0,null);
}
public void imageButtons(JFrame f) {
try {
quit= new ImageIcon("quit.png");
options=new ImageIcon("options.png");
newgame= new ImageIcon("newgame.png");
}catch(Exception e){}
bnewgame= new JButton(newgame);
boptions= new JButton(options);
bquit= new JButton(quit);
bnewgame.setBounds(150,100,400,89);
boptions.setBounds(150,200,400,89);
bquit.setBounds(150,300,400,89);
pane.add(bquit);
pane.add(boptions);
pane.add(bnewgame);
}
public static void main(String args[]) {
MainClass o=new MainClass();
FullScreen fs=new FullScreen();
JFrame f1=new JFrame("TITLE");
pane=f1.getContentPane();
fs.fullScreenIt(f1);
pane.add(o);
f1.setVisible(true);
o.loadImage();
o.imageButtons(f1);
}
}
Fullscreen 是另一个提供全屏框架的类。 JButton 上有 ImageIcon。 bg1.png 是背景图片 问题是这些 JButton 仅当鼠标悬停时才可见,否则它们不会出现。
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.imageio.*;
import java.lang.*;
import java.io.*;
import javax.swing.*;
public class MainClass extends Component{
private Image bg;
private ImageIcon newgame;
private ImageIcon quit;
private ImageIcon options;
private JButton bquit;
private JButton boptions;
private JButton bnewgame;
private static Container pane; //Container
public void loadImage() {
try {
bg=ImageIO.read(new File("bg1.png"));
} catch (Exception e) {
}
if(bg!=null)
repaint();
}
public void paint(Graphics g) {
g.drawImage(bg,0,0,null);
}
public void imageButtons(JFrame f) {
try {
quit= new ImageIcon("quit.png");
options=new ImageIcon("options.png");
newgame= new ImageIcon("newgame.png");
}catch(Exception e){}
bnewgame= new JButton(newgame);
boptions= new JButton(options);
bquit= new JButton(quit);
bnewgame.setBounds(150,100,400,89);
boptions.setBounds(150,200,400,89);
bquit.setBounds(150,300,400,89);
pane.add(bquit);
pane.add(boptions);
pane.add(bnewgame);
}
public static void main(String args[]) {
MainClass o=new MainClass();
FullScreen fs=new FullScreen();
JFrame f1=new JFrame("TITLE");
pane=f1.getContentPane();
fs.fullScreenIt(f1);
pane.add(o);
f1.setVisible(true);
o.loadImage();
o.imageButtons(f1);
}
}
The Fullscreen is another class which gives a full screen Frame.
JButton have ImageIcon on them. bg1.png is a background image
Problem is these JButton become visible only when mouse hovers else they do not appear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您尝试将具有绝对边界的 JButton 添加到使用非空布局管理器的容器中时,您可能会遇到布局问题。建议
pack()
后调用
仅在将所有组件添加到 GUI 后才再次调用两者。setVisible(true)
>打包()Likely you have a layout problem where you're trying to add JButtons with absolute bounds into a container that uses a non-null layout manager. Suggestions
pack()
on your JFrame after adding all componentssetVisible(true)
after callingpack()
and again call both only after adding all components to your GUI.添加按钮后,看起来您永远不会重新绘制。
添加它们后我会在其中添加重绘。
It looks like you're never repainting after you add your buttons.
I would add a repaint in there after you add them.
刚刚遇到了类似的问题...
我相信该故障是由覆盖 Paint() 方法引起的。
默认的paint()方法会自动在所有组件上调用repaint(),但是通过覆盖paint()方法,组件将停止被重新绘制。
因此,解决方案是在重写的paint()方法中的所有组件上调用repaint()。
为我工作,希望对其他人也有用;)..
Just had a similar problem...
I believe that the glitch is caused by overriding the paint() method.
Default paint() method automatically calls repaint() on all component, but by overriding the paint() method, components stop being repainted.
So, the solution is to call repaint() on all components int the overriden paint() method.
Worked for me, hope it'll work for others ;)..
将 Icon/ImageIcon 直接添加到 JButton 而不是 AWT 的
paint()
或Swing JComponents 的paintComponent()
构造函数
rel="noreferrer">JButton(Icon)从代码中
add Icon/ImageIcon directly to the JButton instead of
paint()
for AWT orpaintComponent()
for Swing JComponentsContructor JButton(Icon) knows Icon or ImageIcon
from code