我正在尝试用Java OOP构建一个小程序,该程序将显示一个椭圆形,将其位置更改为鼠标单击坐标。但是,由于我是初学者,所以我不知道如何完成它。
有人知道我的程序出了什么问题吗?
public class OvalWindow {
JFrame window;
int windowWidth = 500;
int windowHeight = 500;
OvalPanel panel;
public void run() {
//Set a window
window = new JFrame("Oval moving");
window.setVisible(true);
window.setSize(windowWidth, windowHeight);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create a panel and add it to the window
panel = new OvalPanel();
window.setContentPane(panel);
//Change the location on mouse input
panel.addMouseListener(new OvalMouseClick(panel));
}
public static void main(String[] args) {
OvalWindow oWndow = new OvalWindow();
oWndow.run();
}
}
文件:Oval.java 创建一个椭圆形的类
public class Oval {
private int diameter;
private int x;
private int y;
private Color color;
public Oval(int x, int y, int diameter, Color color) {
this.x = x;
this.y = y;
this.diameter = diameter;
this.color = color;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getDiameter() {
return diameter;
}
public Color getColor() {
return color;
}
/*
* Draw the oval
*/
public void draw(Graphics g) {
g.fillOval(x, y, diameter, diameter);
g.setColor(color);
}
}
文件:OvalPanel.java 此类将椭圆形对象添加到屏幕。 此外,设置椭圆的初始位置
public class OvalPanel extends JPanel{
Oval oval;
public void addOval(Oval oval) {
oval = new Oval(100, 100, 50, Color.BLACK);
this.repaint();
}
public void paintComponent(Graphics g){
oval.draw(g);
}
}
文件:OvalMouseClick.java 此类在屏幕上与鼠标单击坐标相同的位置处添加了一个新的椭圆形。
public class OvalMouseClick extends MouseAdapter{
OvalPanel panel;
public OvalMouseClick(OvalPanel panel) {
this.panel = panel;
}
@Override
public void mouseClicked(MouseEvent e) {
panel.addOval(new Oval(e.getX(), e.getY(), 50, Color.BLUE));
}
}
答案 0 :(得分:0)
编写一个自定义View root = inflater.inflate(R.layout.fragment_home, container, false);
Button profile =(Button)root.findViewById(R.id.button1);
profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplication(),"Sürücüler",Toast.LENGTH_SHORT).show();
}
});
//similarly for other buttons
return root;
方法,使您的paintComponent()
实际上显示某些内容。如果您向其中添加了组件,它们将自动显示,但是您的OvalPanel
类不是一个组件(在UI方面),因此您需要显式编写代码以显示它们。
Oval
我还建议您阅读https://docs.oracle.com/javase/tutorial/uiswing/上的Swing跟踪记录,因为您不能只是“猜测”事物的工作方式。摇摆很复杂,您需要了解它的作用。然后您将意识到@Override
public void paintComponent(Graphics g) {
for(Oval o : ovals)
o.draw(g);
}
和draw(Graphics g)
不仅仅是具有不同名称的类似方法。
答案 1 :(得分:0)
万一有人在寻找答案。这是我的解决方案:
我已经修改了文件:OvalWindow.java
public class OvalWindow {
JFrame window;
OvalPanel panel;
int windowWidth = 500;
int windowHeight = 500;
/**
* Run the program
*/
public void run() {
// Set a window
window = new JFrame("Oval moving");
window.setVisible(true);
window.setSize(windowWidth, windowHeight);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a panel and add it to the window
panel = new OvalPanel();
window.add(panel, BorderLayout.CENTER);
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
panel.moveToCoord(e);
}
});
}
public static void main(String[] args) {
OvalWindow oWndow = new OvalWindow();
oWndow.run();
}
}
我还大大修改了文件:OvalPanel.java
public class OvalPanel extends JPanel {
Oval oval;
int x = 50;
int y = 10;
int diameter = 50;
Color color = Color.BLUE;
public void moveToCoord(MouseEvent e) {
x = e.getX() - diameter / 2;
y = e.getY() - diameter / 2;
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
oval = new Oval(x, y, diameter, color);
oval.draw(g);
}
}
在文件上:Oval.java只是一个小小的变化:
public void draw(Graphics g) {
g.setColor(color);
g.fillOval(x, y, diameter, diameter);
}
文件:OvalMouseClick.java被删除。
答案 2 :(得分:-1)
让“ OvalMouseClick”类实现接口MouseListener。如果您想在鼠标移动时做点什么,只需让该类还实现“ MouseMotionListener”接口即可。对于MouseWheel,您可以使用“ MouseWheelListener”界面。