简体   繁体   English

如何在按钮点击时更改jlabel的数据?

[英]How to change data of jlabel on button click?

Hi i am trying to make java desk top application where i am using 2 jpanel in that 2 jpanel i am drawing jlabel 嗨,我正在尝试制作java桌面应用程序,我正在使用2 jpanel在那2 jpanel我正在绘制jlabel

i am having button next i want when i click button next jlabel data should change how can i do this 我有按钮接下来我想要当我点击按钮下一个jlabel数据应该改变我怎么能这样做

here is my code 这是我的代码

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

How can i achieve my desired output 我怎样才能实现我想要的输出

my updated code 我的更新代码

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

on every click of button i want 每按一下我想要的按钮

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

and so on

Thanks in advance 提前致谢

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

Will change the text of label to new text when button is clicked. 单击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
            }
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM