gpt4 book ai didi

java - 被paintComponent隐藏的JButtons

转载 作者:行者123 更新时间:2023-12-01 18:42:36 25 4
gpt4 key购买 nike

我有两个屏幕,它们都扩展了 GameScreen,后者又扩展了 JPanel。我的 NavigationFrame 类正确执行,但我的 InternalSensorsFrame 在按钮上绘制灰色背景矩形,以便按钮隐藏,并且当您将鼠标悬停在按钮上时,按钮只会短暂闪烁可见。这是我的代码。谁能告诉我为什么一个示例有效而另一个示例无效,以及如何修复它?

public class NavigationFrame extends GameScreen {

...
public NavigationFrame() {
//super(parent);
addKeyListener(new TAdapter());
//background
img = new ImageIcon(Home.resources.getClass().getResource(imageName));
bg = new BufferedImage(
img.getIconWidth(),
img.getIconHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
Graphics gg = bg.createGraphics();
img.paintIcon(null, gg, 0,0);
gg.dispose();
RenderingHints rh =
new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);

gameFont = new Font("Helvetica", Font.BOLD, 14);

//Set Viewscreen to blockmapview
if (viewScreen == null){
setViewScreen(new ScrollShipScreen(this, 0));
}
viewScreen.setBounds(150, 50, 500, 500);
add(viewScreen);

//Buttons
btnMainComputer = new JButton("Main Computer");
btnMainComputer.setBounds(140, 560, 120, 23);
btnMainComputer.addActionListener(this);
add(btnMainComputer);

btnInternalSensors = new JButton("Internal Sensors");
btnInternalSensors.setBounds(330, 560, 130, 23);
btnInternalSensors.addActionListener(this);
add(btnInternalSensors);

btnNavigation = new JButton("Navigation");
btnNavigation.setBounds(550, 560, 110, 23);
btnNavigation.addActionListener(this);
add(btnNavigation);


}

@Override
public void paintComponent(Graphics g) {
//background
g.setColor(Color.decode("#666665"));
Graphics2D g2d = (Graphics2D) g;
g2d.fillRect(0, 0, 800, 600);
g2d.drawImage(bg, 0, 0,img.getIconWidth(), img.getIconHeight(), this);

//text setup
g.setColor(Color.white);
g.setFont(gameFont);


//Write the strings
//left bar
g.drawString(location, 10,60);
....

}



//Button Presses
public void actionPerformed(ActionEvent ae) {
JButton o = (JButton) ae.getSource();
....
else if(o == btnMainComputer){
Container parent = getParent();
parent.remove(Home.getCurrentScreen());
Home.setCurrentScreen(new MainComputerFrame());
parent.add(Home.getCurrentScreen());
//System.out.println("Main Comp");
}
else if(o == btnInternalSensors){
Container parent = getParent();
parent.remove(Home.getCurrentScreen());
Home.setCurrentScreen(new InternalSensorsFrame());
parent.add(Home.getCurrentScreen());
//System.out.println("Sensors");

}
else if(o == btnNavigation){
//Does nothing on a navigation Frame
// remove(Home.getCurrentScreen());
// Home.setCurrentScreen(new NavigationFrame());
// Home.frame.add(Home.getCurrentScreen());
}

this.requestFocus();
}

}

这是非 worker 类(Class):

public class InternalSensorsFrame extends GameScreen {
private String imageName = "textures/interface/View Screen Frame Overlay.png";



public InternalSensorsFrame(){
//super(parent);
addKeyListener(new TAdapter());

//background
img = new ImageIcon(Home.resources.getClass().getResource(imageName));
bg = new BufferedImage(
img.getIconWidth(),
img.getIconHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
Graphics gg = bg.createGraphics();
img.paintIcon(null, gg, 0,0);
gg.dispose();
RenderingHints rh =
new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);

//Add buttons
btnMainComputer = new JButton("Main Computer");
btnMainComputer.setBounds(140, 560, 120, 23);
btnMainComputer.addActionListener(this);
add(btnMainComputer);

btnInternalSensors = new JButton("Internal Sensors");
btnInternalSensors.setBounds(330, 560, 130, 23);
btnInternalSensors.addActionListener(this);
add(btnInternalSensors);

btnNavigation = new JButton("Navigation");
btnNavigation.setBounds(550, 560, 110, 23);
btnNavigation.addActionListener(this);
add(btnNavigation);

}
@Override
public void paintComponent(Graphics g) {
//super.paint(g);
g.setColor(Color.decode("#666665"));
Graphics2D g2d = (Graphics2D) g;
g2d.fillRect(0, 0, 800, 600);
g2d.drawImage(bg, 0, 0,img.getIconWidth(), img.getIconHeight(), this);
g2d.translate(150, 50);
Home.ship.shipLayout.paintComponent(g2d);
// ArrayList<CrewMan> crew = Home.ship.crew;
// for (int i=0; i<crew.size(); i++){
// crew.get(i).paint(g2d);
// }
g.setColor(Color.white);
g.drawString("teststring", 10,60);
//super.paint(g);

//Toolkit.getDefaultToolkit().sync();
// g.dispose();
}

//Button Presses
public void actionPerformed(ActionEvent ae) {

JButton o = (JButton) ae.getSource();

if(o == btnMainComputer){
Container parent = getParent();
parent.remove(Home.getCurrentScreen());
Home.setCurrentScreen(new MainComputerFrame());
parent.add(Home.getCurrentScreen());
//System.out.println("Main Comp");
}
else if(o == btnInternalSensors){
Container parent = getParent();
parent.remove(Home.getCurrentScreen());
Home.setCurrentScreen(new InternalSensorsFrame());
parent.add(Home.getCurrentScreen());
//System.out.println("Sensors");

}
else if(o == btnNavigation){
Container parent = getParent();
parent.remove(Home.getCurrentScreen());
Home.setCurrentScreen(new NavigationFrame());
parent.add(Home.getCurrentScreen());
}

this.requestFocus();
}
}

最佳答案

也许这些建议不能解决您的问题,但可能会有所帮助。

1)

当您重写此方法时,调用 super.paintComponent(g)

@Override
public void paintComponent(Graphics g) {
//background
super.paintComponent(g);
g.setColor(Color.decode("#666665"));
Graphics2D g2d = (Graphics2D) g;
g2d.fillRect(0, 0, 800, 600);
g2d.drawImage(bg, 0, ...

2) requestFocus()

Note that the use of this method is discouraged because its behavior is platform dependent. Instead we recommend the use of requestFocusInWindow().

3)为您的类使用正确的名称,您说您的类是一个 Frame,但实际上是一个 JPanel。

4)个人建议不要使用太多具体的继承,因为它很危险且难以维护,而且你的继承树级别太高,相反你可以使用这样的东西。

示例:

public class NavigationComponent {

private GameScreen gameScreen;

public NavigationComponent(){
gameScreen = new GameScreen(){
@override
public paintComponent(Graphics g){
// code here
}

};
}

}

5) 您正在将 keyListener 添加到 JPanel。要完成这项工作,您必须使您的对象可聚焦并处于焦点位置。 Swing 方式是使用 KeyBinding 来代替 keyListener。 Swing 被设计为与 keyBindings 一起使用。 How to use keybindings

关于java - 被paintComponent隐藏的JButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19308981/

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