【问题标题】:How to change data of jlabel on button click?如何在单击按钮时更改 jlabel 的数据?
【发布时间】:2014-04-03 17:11:08
【问题描述】:

您好,我正在尝试制作 java 桌面应用程序,我在该 2 jpanel 中使用 2 jpanel 我正在绘制 jlabel

当我单击下一个按钮时我想要下一个按钮 jlabel 数据应该改变我该怎么做

这是我的代码

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

每次点击我想要的按钮

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

and so on

提前致谢

【问题讨论】:

    标签: java swing jpanel jlabel


    【解决方案1】:
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText("new text");
            }
        });
    

    点击button时会将label的文字改为new text

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

    【讨论】:

    • 我正在使用 netbeans ide
    • 呃,你做错了。转到您的 UI 设计器并右键单击您的按钮,然后转到事件,然后执行操作。然后单击 actionPerformed 并在其中添加yourlabel.setText("text");。看看我原帖中的图片。
    • 我这样做了,但更改是在仅标签中进行的,并且有多个标签可用
    • 对不起,您能改写一下吗?我不明白你在说什么。如果要更改多个标签的文本,只需添加更多语句。 label2.setText("sdfsdf"); label3.setText("3242"); 等..
    • 我用for循环来创建许多jlabel而不是单独制作标签
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    相关资源
    最近更新 更多