我试图制作一个目标管理器程序,并希望能够使用矩形边框移动的按钮进行放大和缩小,但是当我按下放大或缩小按钮时,它会保留原始程序它在哪里并在它上面创造一个新的。此外,按钮显示在我编码的位置,但它们也显示在屏幕顶部。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Screen extends JPanel implements Runnable{
/**
*
*/
private static final long serialVersionUID = 1L;
Frame frame;
Thread thread = new Thread(this);
public JButton one;
public JButton two;
public JButton zoomIn;
public JButton zoomOut;
public JButton next;
public JButton xv;
int buttonW = 100;
int buttonH = 50;
int zoomLevel = 5;
int fps;
public boolean running = true;
public Screen(Frame frame) {
this.frame = frame;
thread.start();
}
public void run() {
long lastFrame = System.currentTimeMillis();
int frames = 0;
running = true;
while(running){
repaint();
frames++;
if(System.currentTimeMillis() - 1000 >= lastFrame){
fps = frames;
frames = 0;
lastFrame = System.currentTimeMillis();
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void paintComponent(Graphics g){
Color bgc = new Color (192,192,192);
int screenW = this.getWidth();
int screenH = this.getHeight();
int centerH = screenH / 2;
int centerW = screenW / 2;
g.setColor(bgc);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
/*---------------------------button-stuff------------------------------ **/
Icon plus = new ImageIcon("res/buttons/plus.png");
Icon minus = new ImageIcon("res/buttons/minus.png");
/*---------------------------zoom buttons------------------------------**/
zoomOut = new JButton("", minus);
zoomOut.setBounds(screenW - 30, 32, 20, 20);
zoomOut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
buttonH = buttonH-10;
buttonW = buttonW-20;
zoomLevel = zoomLevel-1;
}
});
zoomIn = new JButton("", plus);
zoomIn.setBounds(screenW - 30, 10, 20, 20);
zoomIn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
buttonH = buttonH+10;
buttonW = buttonW+20;
zoomLevel = zoomLevel+1;
}
});
/*------------------------------------Next--------------------------------------------------------**/
next = new JButton("");
next.setBounds(centerW + ((buttonW / 2) - 5), centerH - 5, 10, 10);
next.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JButton oo = new JButton("new");
oo.setBounds(10,10,10,10);
add(oo);
}
});
xv = new JButton("");
add(xv);
xv.setBounds(centerW -10, centerH - ((buttonH / 2) + 10), 20, 20);
/*------------------------------Add Buttons----------------------------------------------------------**/
add(zoomOut);
add(zoomIn);
add(next);
g.setColor(Color.GREEN);
g.fillRect(centerW - (buttonW / 2), centerH - (buttonH / 2), buttonW, buttonH);
}
}