Java 未设置全局变量
我使用的是swing,有一个函数(CreateAndShowGUI())可以监听向上、向左、向右和向下的按键。现在,无论何时按下任何一个键,我都会将按键按下标志设置为True。按键按下标志是一个全局变量 现在,当我运行程序并按下任意键(向上、向左、向右、向下)时,我可以看到按下的键变量被设置为True。但是当我尝试在main中访问相同的值时,按下的键的值为false。有人能帮我找出哪里错了吗Java 未设置全局变量,java,swing,static,global-variables,key-bindings,Java,Swing,Static,Global Variables,Key Bindings,我使用的是swing,有一个函数(CreateAndShowGUI())可以监听向上、向左、向右和向下的按键。现在,无论何时按下任何一个键,我都会将按键按下标志设置为True。按键按下标志是一个全局变量 现在,当我运行程序并按下任意键(向上、向左、向右、向下)时,我可以看到按下的键变量被设置为True。但是当我尝试在main中访问相同的值时,按下的键的值为false。有人能帮我找出哪里错了吗 public class table1 { public static class Global {
public class table1 {
public static class Global {
// public static boolean key_pressed=false;
public static int[][] a={{2,8,32,64},{2,4,16,16},{16,128,8,64},{2,4,8,16}};
}
public static boolean key_pressed=false;
public static JTable table;
public static JFrame frame=new JFrame("FrameDemo");
public static void createAndShowGUI() {
//Create and set up the window.
//frame =
frame.setPreferredSize(new Dimension(875, 800));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setIconImage(new ImageIcon("menu.jpg").getImage());
JLabel emptyLabel = new JLabel("");
emptyLabel.setFocusable(true);
Action left_action = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
key_pressed=true;
System.out.println("left");
//do nothing
}
};
Action right_action = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
key_pressed=true;
System.out.println("right");
//do nothing
}
};
Action up_action = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
key_pressed=true;
System.out.println("up");
//do nothing
}
};
Action down_action = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.out.println("down");
//do nothing
}
};
emptyLabel.getInputMap().put(KeyStroke.getKeyStroke("LEFT"),"left");
emptyLabel.getActionMap().put("left", left_action);
emptyLabel.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"),"right");
emptyLabel.getActionMap().put("right", right_action);
emptyLabel.getInputMap().put(KeyStroke.getKeyStroke("UP"),"up");
emptyLabel.getActionMap().put("up", up_action);
emptyLabel.getInputMap().put(KeyStroke.getKeyStroke("DOWN"),"down");
emptyLabel.getActionMap().put("down", down_action);
emptyLabel.setPreferredSize(new Dimension(875, 800));
//frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
Font f = new Font("serif", Font.PLAIN, 15);
JLabel score_area = new JLabel();
score_area.setBounds(870-130,00, 130, 50);
score_area.setText("sample text");
emptyLabel.add(score_area);
/*JTable t1=addtable(Global.a);
emptyLabel.add(t1);
t1.setBounds(0, 80, 875,875-80);*/
//pnl.add(t1);
frame.add(emptyLabel);
frame.invalidate();
frame.validate();
frame.repaint();
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static JTable addtable(int[][] a)
{
System.out.println("add game_gui entered");
//a[0][0] +=1;
System.out.println("a value "+a[0][0]);
String[] columnNames = {"First Name",
"Last Name",
"Sport","sample"
};
Object[][] data1= new Object[4][4];
for(int i=0;i<a.length;i++){
for(int j=0;j<a.length;j++)
{
data1[i][j]=(Object)a[i][j];
}
}
Font f = new Font("serif", Font.PLAIN, 55);
table = new JTable( data1, columnNames);/*{
Object x;
JLabel label;
@Override
public Component prepareRenderer(TableCellRenderer renderer,
int row, int column) {
x=table.getModel().getValueAt(row, column);
// System.out.println(x+","+row+","+column);
label = (JLabel) super.prepareRenderer(renderer, row, column);
if(x ==(Object)2)
{
System.out.println(x+" "+row+" " +column);
label.setBackground(Color.YELLOW);
} else if(x==(Object)32) {
label.setBackground(Color.gray);
}
else if(x==(Object)
16) {
label.setBackground(Color.blue);
}
return label;
};
};*/
table.setRowHeight(125);
table.setEnabled(false);
DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
dtcr.setHorizontalAlignment(SwingConstants.CENTER);
table.getColumnModel().getColumn(0).setCellRenderer(dtcr);
int x=((Integer)table.getValueAt(0, 0)).intValue();
System.out.println("sdfsfsdfsfds"+x);
if(x==55)
System.out.println("hahahah "+x);
table.getColumnModel().getColumn(1).setCellRenderer(dtcr);
table.getColumnModel().getColumn(2).setCellRenderer(dtcr);
table.getColumnModel().getColumn(3).setCellRenderer(dtcr);
DefaultTableCellRenderer dtcr1 = new DefaultTableCellRenderer();
dtcr1.setBackground(Color.cyan);
table.repaint();
table.setFont(f);
return table;
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
//key_pressed=true;
createAndShowGUI();
while(true)
{
//System.out.println(key_pressed);
if(key_pressed)
{
key_pressed=false;
System.out.println("Hello");
}
}
}}
公共类表1{
公共静态类全局{
//公共静态布尔键_按下=false;
公共静态int[]a={2,8,32,64},{2,4,16,16},{16128,8,64},{2,4,8,16};
}
公共静态布尔键_按下=false;
公共静态JTable表;
publicstaticjframe=newjframe(“FrameDemo”);
公共静态void createAndShowGUI(){
//创建并设置窗口。
//帧=
框架。设置首选尺寸(新尺寸(875800));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setIconImage(新的图像图标(“menu.jpg”).getImage();
JLabel emptyLabel=新JLabel(“”);
emptyLabel.setFocusable(true);
Action left\u Action=new AbstractAction(){
已执行的公共无效操作(操作事件e){
按键按下=真;
系统输出打印项次(“左”);
//无所事事
}
};
Action right\u Action=new AbstractAction(){
已执行的公共无效操作(操作事件e){
按键按下=真;
System.out.println(“右”);
//无所事事
}
};
Action up\u Action=new AbstractAction(){
已执行的公共无效操作(操作事件e){
按键按下=真;
System.out.println(“up”);
//无所事事
}
};
Action down\u Action=new AbstractAction(){
已执行的公共无效操作(操作事件e){
System.out.println(“向下”);
//无所事事
}
};
emptyLabel.getInputMap().put(KeyStroke.getKeyStroke(“左”),“左”);
emptyLabel.getActionMap().put(“左”,左动作);
emptyLabel.getInputMap().put(KeyStroke.getKeyStroke(“右”),“右”);
emptyLabel.getActionMap().put(“right”,right\u action);
emptyLabel.getInputMap().put(KeyStroke.getKeyStroke(“向上”),“向上”);
emptyLabel.getActionMap().put(“up”,up\u action);
emptyLabel.getInputMap().put(KeyStroke.getKeyStroke(“向下”),“向下”);
emptyLabel.getActionMap().put(“放下”,放下动作);
emptyLabel.setPreferredSize(新尺寸(875800));
//frame.getContentPane().add(emptyLabel,BorderLayout.CENTER);
Font f=新字体(“衬线”,字体为普通,15);
JLabel score_area=新JLabel();
得分(870-130,00,130,50);
score_area.setText(“示例文本”);
emptyLabel.add(得分区域);
/*JTable t1=可添加表(全局.a);
emptyLabel.add(t1);
t1.立根(0,80,875875-80)*/
//pnl.add(t1);
frame.add(emptyLabel);
frame.invalidate();
frame.validate();
frame.repaint();
//显示窗口。
frame.pack();
frame.setVisible(true);
}
公共静态JTable addtable(int[]a)
{
System.out.println(“添加游戏用户界面输入”);
//a[0][0]+=1;
System.out.println(“a值”+a[0][0]);
String[]columnNames={“名字”,
“姓氏”,
“运动”、“样本”
};
对象[][]数据1=新对象[4][4];
对于@Lashane提到的(int i=0;i:使用volatile to key_pressed解决了这个问题。即使使用volatile解决了这个问题,我认为在main中正确(推荐)使用os实例和类也会解决这个问题
当我看到您的代码时,我的第一个想法是“为什么他不使用类的名称来访问方法和类属性?”第二个想法是“内部类不应该声明为单例优于静态吗?”。我想知道我的第二个想法是否准确,因为我不知道是更有效还是“更好”使用static或singleton,但肯定也解决了这个问题
干杯试着把它定义为公共静态易失性布尔键\u pressed=false;
让我们从,静态
不是你的朋友开始…@Lashane非常感谢你..它起了作用。你能详细说明一下你用这个键
布尔值想要实现什么吗?@Parasu:我正在检查按键的情况事件。一旦按下4个箭头键中的任何一个,则设置按下的键,并且还有其他方法需要遵循。