【问题标题】:Error when trying to change the color of background and the absolute position of elements in Java GUI尝试在 Java GUI 中更改背景颜色和元素的绝对位置时出错
【发布时间】:2016-04-28 21:22:48
【问题描述】:

我正在尝试创建一个JButton (r) 并设置其绝对位置。我也想改变背景颜色。当我像这样插入一个新的FlowLayout 时:setLayout(new FlowLayout()); 背景的颜色不会改变,而绝对位置会改变。

同时删除这个:setLayout(new FlowLayout()); 绝对位置不会改变,而颜色会改变。那么谁能解释一下为什么会发生这种情况以及我如何更改代码以使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);    
    }
}

【问题讨论】:

    标签: java swing awt jbutton


    【解决方案1】:

    我编辑了你的代码,以便它可以编译:

    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);
        }
    }
    

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

    我无法重现您的问题,即删除该行会改变颜色。相反,我相信颜色在“d.getContentPane().setBackground(Color.GREEN);”行发生了变化如果你想要不同的颜色,你可以在这里设置不同的颜色!

    【讨论】:

    • 好的,代码可以编译,但不能解决我的问题!尽管绝对位置为南,但该按钮仍位于页面的北边
    • 我编辑代码以解决我相信您的意图。请注意,此解决方案不需要更改布局,而是更改组件在布局中的位置。我希望这会有所帮助!
    【解决方案2】:
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class Back extends JFrame{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JButton r;
    public Back(){
        super("title");
        setLayout(null);
        r=new JButton("ROLL");
        r.setBounds(20, 20, 70, 30);
        add(r);
    
        Thehandler hand=new Thehandler(this);//konstruktori i handler merr nje instance te Background
        r.addActionListener(hand);
    }
    
    private class Thehandler implements ActionListener{
        private JFrame frame;
        public Thehandler(JFrame frame) {
            this.frame = frame;
        }
        public void actionPerformed(ActionEvent event) {
            Random ra = new Random();
            int r = ra.nextInt(255);
            int g = ra.nextInt(255);
            int b = ra.nextInt(255);
            Color c = new Color(r, g, b);
    
            frame.getContentPane().setBackground(c);
    
        }
    }
    
    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);    
    }
    }
    

    【讨论】:

    • 正确答案!非常感谢!设置元素绝对位置的正确方法!
    • “设置元素绝对位置的正确方法!” 制作在下一个操作系统或 PLAF 上会崩溃的脆弱 GUI 的正确方法。不要在你的应用程序中这样做。,程序员。
    【解决方案3】:

    我知道这可能不是您正在寻找的答案。但是把它当作推荐。如果您想要绝对位置,则需要将布局更改为

    setLayout(null);
    

    我会推荐你​​使用 GridBagLayout: Oralce's documentation GridBagLayout

    以及对我帮助很大的 youtube 视频: GridBagLayout tutorial

    【讨论】:

      猜你喜欢
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 2020-02-22
      相关资源
      最近更新 更多