所以我编写了一个小游戏,当我尝试将JLabel设置为x = 0且y = 0时 它出现在JFrame的中间。 当我添加它时代码是
for (int i = 0; i < btns.size(); i++){
Button btn = btns.get(i);
if (!components.contains(btn.getJLabel())){
frame.add(btn.getJLabel());
components.add(btn.getJLabel());
}
if (!btn.getJLabel().isVisible()) btn.getJLabel().setVisible(true);
btn.getJLabel().setText(btn.getText());
}
并且我还在其中放置了一个MouseListener,以便当鼠标移过按钮时它会变为红色但是当我将鼠标悬停在JFrame上时它会变为红色
label.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
onClick();
}
@Override
public void mouseEntered(MouseEvent e) {
Hover();
}
@Override
public void mouseExited(MouseEvent e) {
out();
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}});
}
public void Hover(){
label.setForeground(Color.RED);
}
public void out(){
label.setForeground(Color.BLACK);
}
这里是主要类的完整代码: ` public static JFrame框架;
public static double VERSION = 1.0;
public static int WIDTH = 800, HEIGHT = 700;
public static boolean editorMode = false;
public static boolean selectedTower = false;
public static boolean running = false;
public static boolean mouseOver = false;
public static Tower editorTower = null;
public static Tower selectTower = null;
public static ArrayList<Button> btns;
public static ArrayList<Tower> towers;
public static ArrayList<Component> components;
public static void main(String[] args) {
frame = new JFrame();
frame.setTitle("MyLittleGame v"+VERSION);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
mouseOver = true;
}
@Override
public void mouseExited(MouseEvent arg0) {
mouseOver = false;
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}});
btns = new ArrayList<Button>();
towers = new ArrayList<Tower>();
components = new ArrayList<Component>();
registerBtns();
running = true;
startGameLoop();
}
public static void registerBtns(){
btns.add(new NewTowerBtn());
}
private static void startGameLoop(){
new Thread(new Runnable(){
@Override
public void run() {
int towerAdding = 0;
while (running){
int mouseX = 10;
int mouseY = 10;
if (mouseOver){
mouseX = (int) frame.getMousePosition().getX();
mouseY = (int) frame.getMousePosition().getY();
}
if (editorMode && editorTower != null){
editorTower.setXloc(mouseX);
editorTower.setYloc(mouseY);
}
for (int i = 0; i < towers.size(); i++){
Tower tower = towers.get(i);
if (!components.contains(tower.getLabel())){
frame.add(tower.getLabel());
components.add(tower.getLabel());
}
if (towerAdding >= 100) tower.grow();
if (!tower.getLabel().isVisible()) tower.getLabel().setVisible(true);
tower.getLabel().setText(tower.getWholeString());
}
for (int i = 0; i < btns.size(); i++){
Button btn = btns.get(i);
if (!components.contains(btn.getJLabel())){
frame.add(btn.getJLabel());
components.add(btn.getJLabel());
}
if (!btn.getJLabel().isVisible()) btn.getJLabel().setVisible(true);
btn.getJLabel().setText(btn.getText());
}
if (towerAdding >= 100) towerAdding = 0;
towerAdding++;
System.out.println("Went through");
}
}}).start();
}`
此外,当我添加一个位置为x = 0且y = 0的普通JLabel时,它又将它放在中间位置? 谢谢。
答案 0 :(得分:1)
正如arcy所说,如果您尝试使用absolute positioning,则必须将框架的布局更改为null
request.headers.authorization = value;
您还可以查看如何使用其他布局管理器here。