尝试更改背景颜色和Java GUI中元素的绝对位置时出错

尝试更改背景颜色和Java GUI中元素的绝对位置时出错,java,swing,awt,jbutton,Java,Swing,Awt,Jbutton,我试图创建一个JButton(r)并设置它的绝对位置。我还想改变背景色。当我像这样插入一个新的FlowLayout时:setLayout(newflowLayout())背景颜色不变,而绝对位置不变 同时删除此项:setLayout(newflowlayout())绝对位置不变,而颜色不变。那么,有人能解释一下为什么会发生这种情况,以及我怎样才能以一种既能改变JButton的颜色又能改变绝对位置的方式来改变代码吗 这是我的代码: import java.awt.*; import java.aw

我试图创建一个
JButton
r
)并设置它的绝对位置。我还想改变背景色。当我像这样插入一个新的
FlowLayout
时:
setLayout(newflowLayout())背景颜色不变,而绝对位置不变

同时删除此项:
setLayout(newflowlayout())绝对位置不变,而颜色不变。那么,有人能解释一下为什么会发生这种情况,以及我怎样才能以一种既能改变
JButton
的颜色又能改变绝对位置的方式来改变代码吗

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.Random;

public class Back extends JFrame{

    private JButton r;
    private JButton c;
    private Container container;
    public Back(){
        super("title");
        //setLayout(new FlowLayout());
        ran = new Random();
        value = nextValue();

        r=new JButton("ROLL");
        add(r,BorderLayout.SOUTH);

        thehandler hand=new thehandler(this);//konstruktori i handler merr nje instance te Background
        r.addActionListener(hand);
    }

    private class thehandler implements ActionListener{
        public void actionPerformed(ActionEvent event) {
        }
    }

    public static void main(String[] args) {
         Back  d = new Back() ;

         d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         d.getContentPane().setBackground(Color.GREEN);
         d.setSize(700,500);

         d.setVisible(true);    
    }
}

我知道这可能不是你想要的答案。但是把它当作建议。如果需要绝对位置,则需要将布局更改为

setLayout(null);
我建议您使用GridBagLayout:

youtube视频对我帮助很大:

我编辑了您的代码,以便它能够编译:

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.Random;

public class Back extends JFrame {

    private JButton r;
    private Random ran;
    private String position = BorderLayout.NORTH;
    private Container container;

    public Back() {
        super("title");
        ran = new Random();
        int value = ran.nextInt();

        r = new JButton("ROLL");
        thehandler hand = new thehandler(this);//konstruktori i handler merr     nje instance te Background
        r.addActionListener(hand);

        this.change();

    }

    private class thehandler implements ActionListener {
        private final Back back;

        public thehandler(Back back) {
            this.back = back;
        }
        public void actionPerformed(ActionEvent event) {
            back.change();
        }
    }

    private void change() {
        this.remove(r);
        String newPos = null;
        if (position.equals(BorderLayout.NORTH))
            newPos = BorderLayout.SOUTH;
        else 
            newPos = BorderLayout.NORTH;
        this.add(r, newPos);
        this.position = newPos;
        int r = ran.nextInt(255);
        int g = ran.nextInt(255);
        int b = ran.nextInt(255);
        this.getContentPane().setBackground(new Color(r, g, b));
        this.revalidate();
        this.repaint();
    }

    public static void main(String[] args) {
        Back d = new Back();

        d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        d.setSize(700, 500);
        d.setVisible(true);
    }
}

布局将安排程序中的JComponents:因此,注释或取消注释行“setLayout(newflowLayout());”将移动按钮。有不同的布局,如FLowLayout、BorderLayout或GridLayout。我强烈建议您使用布局而不是绝对定位


我无法重现你的问题,去掉那条线会改变颜色。相反,我认为颜色在“d.getContentPane().setBackground(color.GREEN);”行更改了。如果您想要不同的颜色,可以在此处设置不同的颜色

好的,代码可以编译,但不能解决我的问题!即使绝对位置是南,按钮仍然在页面的北部。我编辑代码,以解决我相信你的意图。请注意,此解决方案不需要更改布局,而需要更改布局中组件的位置。我希望这有帮助!回答正确!谢谢!设置元素绝对位置的正确方法!“设置元素绝对位置的正确方法!”这是制作脆弱GUI的正确方法,它将在下一个OS或PLAF上崩溃。请不要在应用程序中使用此选项。
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.Random;

public class Back extends JFrame {

    private JButton r;
    private Random ran;
    private String position = BorderLayout.NORTH;
    private Container container;

    public Back() {
        super("title");
        ran = new Random();
        int value = ran.nextInt();

        r = new JButton("ROLL");
        thehandler hand = new thehandler(this);//konstruktori i handler merr     nje instance te Background
        r.addActionListener(hand);

        this.change();

    }

    private class thehandler implements ActionListener {
        private final Back back;

        public thehandler(Back back) {
            this.back = back;
        }
        public void actionPerformed(ActionEvent event) {
            back.change();
        }
    }

    private void change() {
        this.remove(r);
        String newPos = null;
        if (position.equals(BorderLayout.NORTH))
            newPos = BorderLayout.SOUTH;
        else 
            newPos = BorderLayout.NORTH;
        this.add(r, newPos);
        this.position = newPos;
        int r = ran.nextInt(255);
        int g = ran.nextInt(255);
        int b = ran.nextInt(255);
        this.getContentPane().setBackground(new Color(r, g, b));
        this.revalidate();
        this.repaint();
    }

    public static void main(String[] args) {
        Back d = new Back();

        d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        d.setSize(700, 500);
        d.setVisible(true);
    }
}