我正在爲會計學生創建一個遊戲,讓他們瞭解更多關於資產,負債和所有者權益的信息。我已經爲程序創建了圖形,但遺憾的是無法弄清楚如何將詞彙單詞編碼爲可拖動到每個帳戶列的可移動對象。我希望能夠將單詞拖到每個部分,然後使用複選標記按鈕檢查答案以查看帳戶是否全部正確。我會很感激任何幫助,因爲我嘗試過創建這個程序。我的按鈕被設置爲默認位置,並且無法更改其位於的位置,但我也遇到了問題。我很感謝任何幫助,並將代碼放在下面,這樣可以更容易地理解問題。如何在Java中創建可拖動的對象?
import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class FinalProjectst extends Applet
{
private int xCoord, yCoord;
private boolean mouseDown;
public void init() {
setSize(1000,650);
repaint();
//Check Button\\
Button b = new Button();
b.setBackground(Color.green);
b.setBounds(475,525,50,50);
b.setSize(50,50);
b.setFont(new Font("TimesRoman",Font.BOLD,40));
b.setLabel("\u2713");
add(b);
//Reset Button\\
Button c = new Button();
c.setBackground(Color.red);
c.setBounds(475,600,50,50);
c.setSize(50,50);
c.setFont(new Font("TimesRoman",Font.BOLD,40));
c.setLabel("X");
add(c);
}
public void paint(Graphics g)
{
//Assets Box\\
g.setColor(Color.green);
g.fillRect(0,0,333,500);
g.setColor(Color.white);
g.setFont(new Font("TimesRoman",Font.PLAIN,54));
g.drawString("Assets",90,60);
//Liabilities Box\\
g.setColor(Color.red);
g.fillRect(333,0,667,500);
g.setColor(Color.white);
g.setFont(new Font("TimesRoman",Font.PLAIN,54));
g.drawString("Liabilities",380,60);
//Owner's Equity Box\\
g.setColor(Color.blue);
g.fillRect(668,0,338,500);
g.setColor(Color.white);
g.setFont(new Font("TimesRoman",Font.PLAIN,54));
g.drawString("Owner's Equity",670,60);
//Word Bank Box\\
g.setColor(Color.black);
g.fillRect(0,500,1005,175);
//T-Chart Boundaries\\
g.setColor(Color.black);
g.drawLine(0,75,1005,75);
g.drawLine(333,0,333,500);
g.drawLine(667,0,667,500);
g.drawLine(1005,0,1005,674);
g.drawLine(0,650,1005,650);
g.drawLine(0,500,1005,600);
//Rent Expense Box\\
g.setColor(Color.white);
g.fillRect(25,525,125,50);
g.setColor(Color.black);
g.setFont(new Font("TimesRoman",Font.PLAIN,22));
g.drawString("Rent Expense",30,555);
//Retained Earnings\\
g.setColor(Color.white);
g.fillRect(25,600,125,50);
g.setColor(Color.black);
g.setFont(new Font("TimesRoman",Font.PLAIN,22));
g.drawString("Retained",50,620);
g.drawString("Earnings",50,645);
//Common Stock\\
g.setColor(Color.white);
g.fillRect(175,525,125,50);
g.setColor(Color.black);
g.setFont(new Font("TimesRoman",Font.PLAIN,22));
g.drawString("Common",195,545);
g.drawString("Stock",210,570);
//Accounts Receivable\\
g.setColor(Color.white);
g.fillRect(175,600,125,50);
g.setColor(Color.black);
g.setFont(new Font("TimesRoman",Font.PLAIN,22));
g.drawString("Accounts",195,620);
g.drawString("Receivable",190,645);
//Dividends\\
g.setColor(Color.white);
g.fillRect(325,525,125,50);
g.setColor(Color.black);
g.setFont(new Font("TimesRoman",Font.PLAIN,22));
g.drawString("Dividends",343,558);
//Drawing\\
g.setColor(Color.white);
g.fillRect(325,600,125,50);
g.setColor(Color.black);
g.setFont(new Font("TimesRoman",Font.PLAIN,22));
g.drawString("Drawing",350,633);
//Cash\\
g.setColor(Color.white);
g.fillRect(550,525,125,50);
g.setColor(Color.black);
g.setFont(new Font("TimesRoman",Font.PLAIN,22));
g.drawString("Cash",590,558);
//Miscellaneous Expense\\
g.setColor(Color.white);
g.fillRect(550,600,125,50);
g.setColor(Color.black);
g.setFont(new Font("TimesRoman",Font.PLAIN,22));
g.drawString("Miscellaneous",550,620);
g.drawString("Expense",575,642);
//Tax Payable\\
g.setColor(Color.white);
g.fillRect(700,525,125,50);
g.setColor(Color.black);
g.setFont(new Font("TimesRoman",Font.PLAIN,22));
g.drawString("Tax",740,545);
g.drawString("Payable",725,565);
//Equipment\\
g.setColor(Color.white);
g.fillRect(700,600,125,50);
g.setColor(Color.black);
g.setFont(new Font("TimesRoman",Font.PLAIN,22));
g.drawString("Equipment",715,632);
//Prepaid Insurance\\
g.setColor(Color.white);
g.fillRect(850,525,125,50);
g.setColor(Color.black);
g.setFont(new Font("TimesRoman",Font.PLAIN,22));
g.drawString("Prepaid",877,545);
g.drawString("Insurance",870,565);
//Accounts Payable\\
g.setColor(Color.white);
g.fillRect(850,600,125,50);
g.setColor(Color.black);
g.setFont(new Font("TimesRoman",Font.PLAIN,22));
g.drawString("Accounts",870,620);
g.drawString("Payable",878,642);
}
public boolean mouseDown(Event e, int x, int y)
{
xCoord = x;
yCoord = y;
if((xCoord >= 475 && xCoord <= 525)&&(yCoord >=600 && yCoord <= 650)) {
mouseDown = true;
}
//repaint();
mouseDown = false;
return true;
}
}
我剛剛注意到你是如何嘗試使用按鈕,所以這個答案是半過時的。但是如果你想使用完全圖形化的方法而不是使用擺動組件,你仍然可以使用這個邏輯。使用swing組件的問題是它們服從佈局管理器,並且通常不會坐在彼此之上,而當您使用圖形疊加層時,您可以執行任何想要的操作。移動按鈕請參閱:http://stackoverflow.com/a/11814384/1270000 – sorifiend