在 Java GUI 中尝试更改背景颜色和元素的绝对位置时出错
Error when trying to change the color of background and the absolute position of elements in Java GUI
我正在尝试创建一个 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);
}
}
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);
}
}
我知道这可能不是您要找的答案。但将其作为推荐。如果你想要绝对位置,你需要将布局更改为
setLayout(null);
我建议您使用 GridBagLayout:
Oralce's documentation GridBagLayout
以及对我帮助很大的 YouTube 视频:
GridBagLayout tutorial
我编辑了你的代码,因此它可以编译:
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(new FlowLayout());" 将移动按钮。有不同的布局,如 FLowLayout、BorderLayout 或 GridLayout。我强烈建议您使用布局而不是绝对定位!
我无法重现您删除该行会改变颜色的问题。相反,我相信颜色在 "d.getContentPane().setBackground(Color.GREEN);" 行发生了变化,如果你想要不同的颜色,你可以在这里设置不同的颜色!
我正在尝试创建一个 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);
}
}
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);
}
}
我知道这可能不是您要找的答案。但将其作为推荐。如果你想要绝对位置,你需要将布局更改为
setLayout(null);
我建议您使用 GridBagLayout: Oralce's documentation GridBagLayout
以及对我帮助很大的 YouTube 视频: GridBagLayout tutorial
我编辑了你的代码,因此它可以编译:
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(new FlowLayout());" 将移动按钮。有不同的布局,如 FLowLayout、BorderLayout 或 GridLayout。我强烈建议您使用布局而不是绝对定位!
我无法重现您删除该行会改变颜色的问题。相反,我相信颜色在 "d.getContentPane().setBackground(Color.GREEN);" 行发生了变化,如果你想要不同的颜色,你可以在这里设置不同的颜色!