2014-04-03 29 views
0

你好我試圖讓我在哪裏使用2周的JPanel在2周的JPanel我正在畫的JLabel如何更改按鈕點擊jlabel的數據?

我有按鈕旁邊,我想,當我點擊按鈕旁邊的JLabel數據發生變更,我該怎麼辦的Java桌面應用程序這

這裏是我的代碼

public Second() { 
      this.getContentPane().setBackground(new java.awt.Color(255, 140, 0)); 
this.setExtendedState(JFrame.MAXIMIZED_BOTH); 
this.setUndecorated(true); 

      initComponents(); 


      JPanel panel = createPanel(); 
     jPanel1.setLayout(new GridBagLayout()); 
//   jPanel1.setLayout(null); 
    jPanel1.add(panel); 

    JPanel panel1 = createPanel(); 
     jPanel2.setLayout(new GridBagLayout()); 
// jPanel2.setLayout(null); 
    jPanel2.add(panel1); 


     DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); 
    //get current date time with Date() 
    Date date = new Date(); 
    //System.out.println(dateFormat.format(date)); don't print it, but save it! 
    String yourDate = dateFormat.format(date); 
     jButton7.setText(yourDate); 
     jButton6.setText(yourDate); 
     jButton5.setText(yourDate); 
     jButton1.setText(yourDate); 

    } 


    private JPanel createPanel() { 
     JPanel panel = new JPanel(new GridLayout(0, 1, 10, 5)); 
     EmptyBorder panelBorder = new EmptyBorder(10, 5, 10, 10); 
     panel.setBorder(panelBorder); 
panel.setBackground(new java.awt.Color(255, 153, 51)); 
     panel.setOpaque(true); 
     EmptyBorder border1 = new EmptyBorder(15, 20, 15, 18); 
     // LineBorder line = new LineBorder(Color.blue, 2, true); 
    // CompoundBorder compound = new CompoundBorder(line, border); 
      Border border = BorderFactory.createLineBorder(Color.BLUE, 2); 
     for (int i = 0; i <11; i++) { 
      JLabel label = new JLabel("<html>Case &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CaseNum<br><font color=yellow>Party1<br>Party2</font></html>"); 
    label.setFont(new java.awt.Font("Times New Roman", 1, 18)); 
      label.setBorder(border); 

      label.setBorder(border1); 

      label.setBackground(Color.GRAY); 
      label.setForeground(new java.awt.Color(255, 255,255)); 
      label.setOpaque(true); 
      panel.add(label); 
     } 
     return panel; 
    } 

我怎樣才能達到我的期望輸出

我更新的代碼

public Second() { 
       this.getContentPane().setBackground(new java.awt.Color(255, 140, 0)); 
    this.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    this.setUndecorated(true); 

       initComponents(); 


       JPanel panel = createPanel(); 
      jPanel1.setLayout(new GridBagLayout()); 
    //   jPanel1.setLayout(null); 
     jPanel1.add(panel); 

     JPanel panel1 = createPanel(); 
      jPanel2.setLayout(new GridBagLayout()); 
    // jPanel2.setLayout(null); 
     jPanel2.add(panel1); 


      DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); 
     //get current date time with Date() 
     Date date = new Date(); 
     //System.out.println(dateFormat.format(date)); don't print it, but save it! 
     String yourDate = dateFormat.format(date); 
      jButton7.setText(yourDate); 
      jButton6.setText(yourDate); 
      jButton5.setText(yourDate); 
      jButton1.setText(yourDate); 

     } 


     private JPanel createPanel() { 
      JPanel panel = new JPanel(new GridLayout(0, 1, 10, 5)); 
      EmptyBorder panelBorder = new EmptyBorder(10, 5, 10, 10); 
      panel.setBorder(panelBorder); 
    panel.setBackground(new java.awt.Color(255, 153, 51)); 
      panel.setOpaque(true); 
      EmptyBorder border1 = new EmptyBorder(15, 20, 15, 18); 
      // LineBorder line = new LineBorder(Color.blue, 2, true); 
     // CompoundBorder compound = new CompoundBorder(line, border); 
       Border border = BorderFactory.createLineBorder(Color.BLUE, 2); 
      for (int i = 0; i <11; i++) { 
       JLabel label = new JLabel("<html>Case &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CaseNum<br><font color=yellow>Party1<br>Party2</font></html>"); 
     label.setFont(new java.awt.Font("Times New Roman", 1, 18)); 
       label.setBorder(border); 

       label.setBorder(border1); 

       label.setBackground(Color.GRAY); 
       label.setForeground(new java.awt.Color(255, 255,255)); 
       label.setOpaque(true); 
       panel.add(label); 
      } 
      return panel; 
     } 


private void NextActionPerformed(java.awt.event.ActionEvent evt) {          
    // TODO add your handling code here: 

    label.setText("new text"); 
}          
按鈕的每次點擊

我想提前

回答

3
button.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      label.setText("new text"); 
     } 
    }); 

1 click "hello"; 
2 click "sharma" 
3 click " not bad"; 
4 click " not good" 

and so on 

由於意志label文字更改爲new text點擊button時。

button.addActionListener(new ActionListener() { 
     int clicks = 0; 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      switch (++clicks) { 
       case 1: 
        label.setText("hello"); 
       break; 

       case 2: 
        label.setText("sharma"); 
       break; 

       //continue doing it like this 
      } 
     } 
    }); 
+0

我在做同樣的事但不工作 – user3456343

+0

你能更新你的代碼嗎? – Arrem

+0

是的,我正在更新我的代碼 – user3456343