全局变量未设置

时间:2014-04-29 06:33:54

标签: java swing static global-variables key-bindings

我正在使用swing并且有一个函数(CreateAndShowGUI()),它可以监听Up,Left,Right&下 。现在,只要按下任何一个键,我就将key_pressed标志设置为True。 key_pressed标志是全局变量。

现在,当我运行程序并按任意键(向上,向左,向右,向下)时,我可以看到key_pressed变量被设置为True。但是当我尝试在main中访问它时,此key_pressed的值是假的。任何人都可以帮我解决我的错误吗?

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

2 个答案:

答案 0 :(得分:0)

如@Lashane所述:使用volatile到key_pressed解决了这个问题。

答案 1 :(得分:0)

即使使用volatile解决了这个问题,我认为正确(并推荐)使用主实例中的os实例和类也可以解决问题。

当我看到你的代码时,我第一个想到的是“为什么他不使用类的名称来访问方法和类属性?”第二个“内部阶级不应该被宣布为单身优于静态吗?”。我想知道我的第二个想法是否准确,因为我不知道是否更有效或“更好”使用静态或单身,但肯定也解决了这个问题。

干杯