我尝试创建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);
}
}
答案 0 :(得分:0)
我编辑了你的代码,以便编译:
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。我强烈建议你使用Layouts而不是绝对定位!
我无法重现您删除该行会改变颜色的问题。相反,我认为颜色是在#34; d.getContentPane()。setBackground(Color.GREEN);"如果你想要一种不同的颜色,你可以在这里设置一个不同的颜色!
答案 1 :(得分:-1)
var io = require('socket.io')(server);
io.on('event', function(data){});
var socketUsers = [];
io.use(sharedSession(session, {
autoSave: true
}));
io.on('connection', function(socket) {
socketUsers.push(socket);
console.log('Player connected: ' + socket.id);
console.log('Connected players: ' + socketUsers.length);
io.on('disconnect', function() {
for(var i = 0; i < socketUsers.length; i++) {
console.log(socketUsers[i].id);
if(socketUsers[i].id == socket.id) {
//delete socketUsers[i];
socketUsers.splice(i);
break;
}
}
console.log(socketUsers.length + ' players online.');
});
io.on('chat message', function(msg){
io.emit('chat message', msg);
console.log('message: ' + msg);
});
function broadcast(key, value) {
io.emit(key, value);
}
});
答案 2 :(得分:-1)
我知道这可能不是您正在寻找的答案。但是把它作为推荐。如果您想要绝对位置,则需要将布局更改为
setLayout(null);
我建议你使用GridBagLayout: Oralce's documentation GridBagLayout
和帮助我很多的YouTube视频: GridBagLayout tutorial