【发布时间】: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);
}
}
【问题讨论】: