Java 如何在单击按钮时更改jlabel的数据?
您好,我正在尝试制作java桌面应用程序,我在其中使用2 jpanel,我正在绘制2 jpanel jlabel 当我单击按钮下一步时,我正在使用我想要的按钮下一步jlabel数据应该更改我如何才能做到这一点 这是我的密码Java 如何在单击按钮时更改jlabel的数据?,java,swing,jpanel,jlabel,Java,Swing,Jpanel,Jlabel,您好,我正在尝试制作java桌面应用程序,我在其中使用2 jpanel,我正在绘制2 jpanel jlabel 当我单击按钮下一步时,我正在使用我想要的按钮下一步jlabel数据应该更改我如何才能做到这一点 这是我的密码 public Second() { this.getContentPane().setBackground(new java.awt.Color(255, 140, 0)); this.setExtendedState(JFrame.MAXIMIZED_B
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 Item 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;
}
提前谢谢
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("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
}
}
});
单击按钮时将标签文本更改为新文本
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 ideEr,你做错了。转到UI设计器,右键单击按钮,然后转到事件,然后转到操作。然后单击actionPerformed并添加标签.setTexttext;在那里。请看我原始帖子中的图片。我这样做了,但更改是在onluy only标签中进行的,并且有多个标签可用。对不起,请您重新措辞好吗?我不明白你在说什么。如果要更改多个标签的文本,只需添加更多语句。label2.setTextsdfsdf;label3.setText3242;等等,我使用for循环来创建许多jlabel,而不是单独制作标签。我使用netbeans ideEr,你做错了。转到UI设计器,右键单击按钮,然后转到事件,然后转到操作。然后单击actionPerformed并添加标签.setTexttext;在那里。请看我原始帖子中的图片。我这样做了,但更改是在onluy only标签中进行的,并且有多个标签可用。对不起,请您重新措辞好吗?我不明白你在说什么。如果要更改多个标签的文本,只需添加更多语句。label2.setTextsdfsdf;label3.setText3242;等等,我使用循环来创建许多不单独制作标签的jlabel
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
}
}
});