我正在尝试绘制一个彩色条,随着时间的推移它会变大.它在我使用默认布局管理器时有效,但是当我试图用GridBagLayout实现它时,它不会.我编写了一个测试项目,仅用于测试目的,以调查问题所在.我添加了几个按钮,只是为了除了graphics2D对象之外还有别的东西,所以它看起来像我正在工作的实际项目.但过了几天,我不得不承认我没有线索,出了什么问题.我希望有一个人可以帮助我!
提前,不要对我在代码中使用的一些单词感到困惑.我的母语不是英语.
import java.awt.*;
import javax.swing.*;
import java.util.TimerTask;
import java.util.Timer;
public class FarbBalkenTest extends JPanel {
static Timer timer = new Timer ();
static TimerTask task ;
static int time;
public static void main(String[] args) {
FarbBalkenTest fbt = new FarbBalkenTest();
fbt.init();
}
public static void addComponent(Container cont, GridBagLayout gbl, Component c, int x, int y, int width, int height, double weightx, double weighty ){
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx=x;
gbc.gridy=y;
gbc.gridwidth=width;gbc.gridheight=height;
gbc.weightx=weightx;gbc.weighty=weighty;
gbl.setConstraints(c,gbc);
cont.add(c);
} …Run Code Online (Sandbox Code Playgroud)