2013-09-16 40 views
-2

我是一名学生程序员,我确实需要修复此代码才能完成我的项目。如何将名称贴上标签?

是否有任何其他方式可以为此循环标签赋予一个唯一的名称?

因为无论何时用户单击其中一个标签,文本将更改其颜色。不是所有的标签,只有用户点击过的标签。

public void addComponentsToPane(final JPanel pane) { 
    if (RIGHT_TO_LEFT) { 
     pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 
    } 

    pane.setLayout(new GridBagLayout()); 

    c.insets = new Insets(20, 20, 5, 5); 
    c.anchor = GridBagConstraints.NORTH; 
    if (shouldFill) { 
     //natural height, maximum width 
     c.fill = GridBagConstraints.HORIZONTAL; 
    } 
    int i; 
    for (i = 1; i <= 80; i++) { 
     if (z == 14) { 
      y++; 
      x = 0; 
      z = 0; 
     } 
     try { 

      label = new JLabel("# " + i); 
      labels(); 
      c.weightx = 0.5; 
      c.fill = GridBagConstraints.HORIZONTAL; 
      c.gridx = x; 
      c.gridy = y; 
      pane.add(label, c); 
      x++; 
      z++; 
      set_x = x; 
      set_y = y; 
     } catch (Exception e) { 
     } 

    } 
    tableNum = i; 
    btn.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) { 
      try { 
       int UserInput = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Number of Table:")); 
       int i; 
       for (i = tableNum; i <= (tableNum + UserInput) - 1; i++) { 
        if (z == 14) { 
         set_y++; 
         set_x = 0; 
         z = 0; 
        } 
        try { 

         label = new JLabel("# " + i); 

         labels(); 
         c.weightx = 0.5; 
         c.fill = GridBagConstraints.HORIZONTAL; 
         c.gridx = set_x; 
         c.gridy = set_y; 
         pane.add(label, c); 
         set_x++; 
         z++; 
         lbl[i] = label; 
        // lbl[i].setForeground(Color.red); 
         System.out.print(lbl[i].getText()); 
         // set_x = x; 
         // set_y = y; 
        } catch (Exception ex) { 
        } 

       } 
       tableNum = i; 
       frame.revalidate(); 

      } catch (Exception ex) { 
       JOptionPane.showMessageDialog(null, "PLease Input Digits Only", "Input      Error", JOptionPane.ERROR_MESSAGE); 
      } 

     } 
    }); 

} 

private void labels() { 
    ImageIcon icon = new ImageIcon(imagePath + labelIconName); 
    icon.getImage().flush(); 
    label.setIcon(icon); 
    label.setBackground(Color.WHITE); 
    label.setPreferredSize(new Dimension(80, 80)); 
    label.setFont(new Font("Serif", Font.PLAIN, 18)); 
    label.setForeground(Color.black); 
    label.setVerticalTextPosition(SwingConstants.BOTTOM); 
    label.setHorizontalTextPosition(SwingConstants.CENTER); 
    // label.setBorder(BorderFactory.createBevelBorder(0)); 
    label.addMouseListener(new MouseAdapter() { 

     public void mouseClicked(MouseEvent e) { 
      // String[] option = {"Available", "Occupied", "Reserved"}; 

      // choose = (String) JOptionPane.showInputDialog(this, "Table :"+label.getText() + , "Choose transaction", JOptionPane.INFORMATION_MESSAGE, null, option, option[0]); 
      System.out.print(label.getName()); 
      centerScreen(); 

      selectAllMenu(); 
      jDialogMenu.show(); 

      // frame.setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE); 

     } 
    }); 

} 
+10

请帮助和ASAP在这里赚你没有帮助。表现出尊重,最重要的是你尝试过的将会。 [FAQ](在第二个笔记中,详细说明你真正想要的也会有所帮助。坦率地说,我不知道你在问什么,尽管我已经看到了我的问题......) – ppeterka

+0

你是什么意思by'unique namea' – exexzian

+0

小提示:您应该摆脱代码示例中不相关的代码,以使其更具可读性。 (在这种情况下,所有的布局代码和大部分的标签样式等,只保留基本设置它们,添加一些虚拟文本以及应该工作但不包含的代码部分)。 – millimoose

回答

1

我想每个面板的设置一个名称。所以我可以为每个标签设置一个前景。

您不需要一个唯一的名称。您需要将MouseListener添加到您创建的每个标签。然后是mousePressed()事件,您可以使用通用代码更改单击的标签的前景。例如:

MouseListener ml = new MouseAdapter() 
{ 
    @Override 
    public void mousePressed(MouseEvent e) 
    { 
     JLabel label = (JLabel)e.getSource(); 
     label.setForeground(...); 
    } 
} 

... 

label.addMouseListener(ml);