最近,我一直在互联网上试图弄清楚如何为动画和按钮动作监听器实现摇摆计时器。我真的很抱歉,我问了一个可能已经在堆栈溢出上发布过一次的问题,但这些帖子都没有真正回答我如何为不同的代码实现实现swing定时器。我希望你能帮我实现一个摆动计时器,谢谢!到目前为止,GUI工作正常,按钮也正确添加。
这是我的Frame类
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.JPanel;
import javax.swing.JFrame;
import static java.lang.System.*;
import java.awt.event.*;
import java.awt.Graphics.*;
public class Main
{
private static JFrame f;
public static void main(String[] args)
{
Main m = new Main();
}
public Main()
{
f = new JFrame("Tic-Tac-Toe");
f.setExtendedState(Frame.MAXIMIZED_BOTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MainPanel());
f.setVisible(true);
}
}
这是我的Panel类
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.JPanel;
import javax.swing.JFrame;
import static java.lang.System.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.Graphics.*;
public class MainPanel extends JPanel
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double w = screenSize.getWidth();
double h = screenSize.getHeight();
int width = (int)w;
int height = (int)h;
static boolean drawX = false;
static int counter = 0;
int width1a = width/2 - 300;
int width2a = width/2 - 100;
int width3a = width/2 + 100;
int width4a = width/2 + 300;
int height1from = (int)height - 100;
int height1to = (int)height - (int)(height/1.05);
int height1a = height/2 - 150;
int height2a = height/2 + 75;
int width1from = width1a - 100;
int width1to = width4a + 100;
int aveWidth12 = (width1a + width2a)/2;
int aveWidth23 = (width2a + width3a)/2;
int aveWidth34 = (width3a + width4a)/2;
int aveHeight12 = (height1a + height2a)/2;
public MainPanel()
{
JLabel label1 = new JLabel("LET'S PLAY TIC-TAC-TOE!", JLabel.RIGHT);
setLayout(new FlowLayout());
add(label1);
setLayout(null);
JButton c = new JButton();
JButton tl = new JButton();
JButton tr = new JButton();
JButton ml = new JButton();
JButton mr = new JButton();
JButton bl = new JButton();
JButton br = new JButton();
JButton tc = new JButton();
JButton bc = new JButton();
tl.setBounds(aveWidth12-55,(height1a - 150),100,100);
ml.setBounds(aveWidth12-55,aveHeight12 - 25,100,100);
bl.setBounds(aveWidth12-55,height2a + 75,100,100);
tr.setBounds(aveWidth34-55,(height1a - 150),100,100);
mr.setBounds(aveWidth34-55,aveHeight12 - 25,100,100);
br.setBounds(aveWidth34-55,height2a + 75,100,100);
c.setBounds(aveWidth23-55,aveHeight12 - 25,100,100);
tc.setBounds(aveWidth23-55,(height1a - 150),100,100);
bc.setBounds(aveWidth23-55,height2a + 75 ,100,100);
add(c);
add(tl);
add(tr);
add(ml);
add(mr);
add(bl);
add(br);
add(tc);
add(bc);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10));
g.setColor(Color.black);
g2.drawLine(width1a, height1from, width1a, height1to);
g2.drawLine(width2a, height1from, width2a, height1to);
g2.drawLine(width3a, height1from, width3a, height1to);
g2.drawLine(width4a, height1from, width4a, height1to);
g2.drawLine(width1from, height1a, width1to, height1a);
g2.drawLine(width1from, height2a, width1to, height2a);
}
}
编辑:这是我未经审查的JPANEL CLASS
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.JPanel;
import javax.swing.JFrame;
import static java.lang.System.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.Graphics.*;
public class MainPanel extends JPanel
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double w = screenSize.getWidth();
double h = screenSize.getHeight();
int width = (int)w;
int height = (int)h;
static boolean drawX = false;
static int counter = 0;
int width1a = width/2 - 300;
int width2a = width/2 - 100;
int width3a = width/2 + 100;
int width4a = width/2 + 300;
int height1from = (int)height - 100;
int height1to = (int)height - (int)(height/1.05);
int height1a = height/2 - 150;
int height2a = height/2 + 75;
int width1from = width1a - 100;
int width1to = width4a + 100;
int aveWidth12 = (width1a + width2a)/2;
int aveWidth23 = (width2a + width3a)/2;
int aveWidth34 = (width3a + width4a)/2;
int aveHeight12 = (height1a + height2a)/2;
Timer timer = new Timer(1000, new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
counter++;
repaint();
if(counter == 5)
{
drawX = true;
}
}
});
public MainPanel()
{
JLabel label1 = new JLabel("LET'S PLAY TIC-TAC-TOE!", JLabel.RIGHT);
setLayout(new FlowLayout());
add(label1);
setLayout(null);
JButton c = new JButton();
JButton tl = new JButton();
JButton tr = new JButton();
JButton ml = new JButton();
JButton mr = new JButton();
JButton bl = new JButton();
JButton br = new JButton();
JButton tc = new JButton();
JButton bc = new JButton();
tl.setBounds(aveWidth12-55,(height1a - 150),100,100);
ml.setBounds(aveWidth12-55,aveHeight12 - 25,100,100);
bl.setBounds(aveWidth12-55,height2a + 75,100,100);
tr.setBounds(aveWidth34-55,(height1a - 150),100,100);
mr.setBounds(aveWidth34-55,aveHeight12 - 25,100,100);
br.setBounds(aveWidth34-55,height2a + 75,100,100);
c.setBounds(aveWidth23-55,aveHeight12 - 25,100,100);
tc.setBounds(aveWidth23-55,(height1a - 150),100,100);
bc.setBounds(aveWidth23-55,height2a + 75 ,100,100);
add(c);
add(tl);
add(tr);
add(ml);
add(mr);
add(bl);
add(br);
add(tc);
add(bc);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10));
g.setColor(Color.black);
g2.drawLine(width1a, height1from, width1a, height1to);
g2.drawLine(width2a, height1from, width2a, height1to);
g2.drawLine(width3a, height1from, width3a, height1to);
g2.drawLine(width4a, height1from, width4a, height1to);
g2.drawLine(width1from, height1a, width1to, height1a);
g2.drawLine(width1from, height2a, width1to, height2a);
if(drawX)
{
g2.drawLine(aveWidth23-55,aveHeight12 - 25,aveWidth23 + 45,aveHeight12 +75);
}
}
}
这就是我尝试使用计时器的方式,但是当计数器应该达到5时,没有X或任何东西超过按钮。对不起,如果对这个问题有任何疑惑,或者它是重复的话。