所以我想从一个类中其他类的对象调用一个类中的函数,有没有一种方法可以这样做,而不必在第二个类中创建第一个类的对象?在这种特殊情况下,我想再次从exitviewoutput类更改exitframe可见性,而不在exitviewinput类中创建对象,因为我觉得这样会产生一个无限循环。有关更多信息,请参阅以下代码:
退出框架:
package ExitView;
import javax.swing.JFrame;
import Project.GameFrame;
public class ExitFrame
{
private int width = 500;
private int height = 500;
JFrame frame = new JFrame();
public ExitFrame()
{
ExitViewOutput panel = new ExitViewOutput();
frame.setContentPane(panel);
panel.addMouseListener(new ExitViewInput(panel));
frame.setSize(width, height);
frame.setLocationRelativeTo(null);
frame.setVisible(false);
frame.setAlwaysOnTop(false);
frame.setFocusable(false);
frame.setUndecorated(true);
frame.setVisible(true);
}
public void changeVisibility(boolean visibility)
{
frame.setVisible(visibility);
frame.setAlwaysOnTop(visibility);
frame.setFocusable(visibility);
}
}
退出视图输出:
package ExitView;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.image.BufferedImage;
import HelpFunct.HelpingFunct;
public class ExitViewOutput extends JPanel
{
private boolean showBorders = false;
private HelpingFunct h = new HelpingFunct();
private BufferedImage pause = h.getImage("/pauze1.png");
private BufferedImage kthehuNeLoje = h.getImage("/KthehuNeLoje.png");
private BufferedImage shfaqKufijte = h.getImage("/ShfaqKufijte.png");
private BufferedImage dilNgaLoja = h.getImage("/DilNgaLoja1.png");
private BufferedImage tick = h.getImage("/Tick.png");
private BufferedImage cross = h.getImage("/Cross.png");
public void switchShowBorder()
{
showBorders = !showBorders;
this.repaint();
}
public void paintComponent(Graphics g)
{
g.setColor(new Color(126, 96, 69));
g.fillRect(0, 0, 500, 500);
g.setColor(new Color(144, 114, 87));
g.fillRect(20, 20, 460, 460);
g.setColor(new Color(126, 96, 69));
g.fillRect(30, 116, 299, 108);
g.fillRect(30, 234, 299, 108);
g.fillRect(30, 352, 299, 108);
g.fillRect(351, 234, 108, 108);
g.setColor(Color.black);
g.drawRect(0, 0, 499, 499);
g.drawRect(20, 20, 459, 459);
g.drawRect(30, 116, 300, 108);
g.drawRect(30, 234, 300, 108);
g.drawRect(30, 352, 300, 108);
g.drawRect(351, 234, 108, 108);
g.drawLine(0, 0, 20, 20);
g.drawLine(0, 500, 20, 479);
g.drawLine(500, 0, 479, 20);
g.drawLine(479, 479, 500, 500);
g.drawImage(pause, 30, 30, 300, 66, null);
g.drawImage(kthehuNeLoje, 30, 116, null);
g.drawImage(shfaqKufijte, 30, 234, null);
g.drawImage(dilNgaLoja, 30, 352, null);
if (showBorders)
g.drawImage(tick, 351, 234, null);
else
g.drawImage(cross, 351, 234, null);
}
}
ExitView输入:
package ExitView;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class ExitViewInput extends MouseAdapter
{
private ExitViewOutput framePaint;
ExitViewInput(ExitViewOutput framePaint)
{
super();
this.framePaint = framePaint;
}
public void mouseClicked(MouseEvent e)
{
if (((e.getX() > 30) && (e.getX() < 30 + 300)) &&
((e.getY() > 352) && (e.getY() < 352 + 108)))
System.exit(0);
else if (((e.getX() > 30) && (e.getX() < 30 + 300)) &&
((e.getY() > 116) && (e.getY() < 116 + 108)))
{
}
else if (((e.getX() > 30) && (e.getX() < 30 + 300)) &&
((e.getY() > 234) && (e.getY() < 234 + 108)))
{
framePaint.switchShowBorder();
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!