移动JPanel时遇到问题。
我有一个窗口400x600和一个JPanel(红色)100x100
public class RulettModel {
private int num;
public void Slide() {
num = num + 1;
if(num > 600) {
num = -100;
}
}
public int getNum() {
return num;
}
}
class RulettListener implements ActionListener{
Timer tm = new Timer(1, this);
@Override
public void actionPerformed(ActionEvent e) {
tm.start();
int y = 0;
theModel.Slide();
theView.setNewLocationOfPiros(theModel.getNum(), y);
}
}
void setNewLocationOfJpanel(int x, int y) {
JPanel1.setLocation(x,y);
}
此代码是我的代码的一部分,这里需要,这很好用我的JPanel从窗口滑出到右侧,当它超出窗口宽度时,它开始从-100回来,所以从左侧< / p>
我遇到的问题: 我希望我的JPanel从左侧进入,然后再在右侧完成。
所以,如果小组的一半已经出去,那么那一半应该已经出现在左边,所以我的小组应该一次在2个位置,这里有一半,有一半有可能吗?
任何其他提示都可以解决这个问题。
答案 0 :(得分:0)
我猜你需要2个JPanels。然后我会
public class RulettModel {
private int num1, num2;
public void Slide() {
num1 = num1 + 1;
if(num1 > 600) {
num2 = 500-num1;
}
}
public int getNum1() {
return num1;
}
public int getNum2() {
return num2;
}
}
并拥有setNewLocOfJPanel1(int, int)
和setNewLocOfJPanel2(int, int)
方法
答案 1 :(得分:0)
在编写下面的代码之前,我使用Microsoft Paint制作了7个不同颜色的矩形,60像素宽,90像素高。在哪里看到package SlidingTiles;
import ...;
public class Main
{
private static int timerDelay = 100;
private static Timer t = new Timer(timerDelay, new TimerActionListener());
private static JFrame jf = new JFrame();
private static JPanel jp = new JPanel();
private static String[] colors = { "blue", "brown", "green", "grey", "purple", "red", "yellow" };
private static JLabel[] tiles = new JLabel[7];
public static void main(String[] args)
{
for(int i=0; i<7; ++i)
{
tiles[i] = new JLabel(new ImageIcon("tiles/"+colors[i]+".jpg"));
tiles[i].setBounds(-120+60*i,0,60,90);
jf.add(tiles[i]);
}
jf.add(jp);
jf.setSize(186,120);
jf.setResizable(false);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.start();
}
public static class TimerActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
for(int i=0; i<7; ++i)
{
Point p = tiles[i].getLocation();
int newX = (int) p.getX()+5;
if(newX == 300)
newX = -120;
tiles[i].setLocation(newX, 0);
}
}
}
}
我只通过反复试验获得了这些值。我猜测我的timerDelay是一个合适的值。所有其他值应该是明显的原因。
所有这一切的关键在于,在TimerActionListener中,我只需要每个tile [i]的getLocation(),向x坐标添加5,当它达到300时将其重置为-120,然后是setLocation(int ,int)再次。
The JSON result you are getting is :
[{"$id":"1","id":8,"Medical_id":"a","Password":"a","Name":null,"Email":null,"gender":null,"Type":null}]
use for each to iterate through the results or
instead of this you can check the result like this :
var data = [{ "$id": "1", "id": 8, "Medical_id": "a", "Password": "a", "Name": null, "Email": null, "gender": null, "Type": null }]
alert(data[0].Medical_id);