gpt4 book ai didi

java - 我如何从另一个类设置Visible(true) JLabel

转载 作者:行者123 更新时间:2023-12-01 10:06:48 24 4
gpt4 key购买 nike

这是我的代码。我在 Check1 标签上遇到问题,因为我希望它在答案正确时可见,顺便说一下,我正在使用卡片布局。我删除了不重要的代码

public class Category1 extends JPanel {
public static JLabel Check1;
public Category1 () {


Check1 = new JLabel(newImageIcon(getClass().getResource("Buttons/Check.png")));
Check1.setBounds(75 , 305, 40, 40);
Check1.setVisible(false);
add(Check1);

}}

这是另一个类,如果您单击 Submit1 按钮,并且 JTextField 中的文本正确,我希望 Check1 按钮可见。

public class QuizPanelc1 {
JPanel Quiz1;
JTextField Answer1;
JButton Submit1;

public QuizPanelc1(){

Answer1 = new JTextField();
Answer1.setBounds(180, 480, 200, 40);
Quiz1.add(Answer1);
Submit1 = new JButton(new ImageIcon(getClass().getResource("Buttons/SubmitButton.png")));
Submit1.setBounds(390, 480, 40, 40);
Quiz1.add(Submit1);

ButtonHandler1 events1 = new ButtonHandler1();
Submit1.addActionListener(events1);
Back1.addActionListener(events1)

}
private class ButtonHandler1 implements ActionListener {
public void actionPerformed (ActionEvent eventClick) {
Object event = eventClick.getSource();
Category1 c1 = new Category1();

if(Submit1==event)
{
if(Answer1.getText().equalsIgnoreCase("Fila"))
{
Answer1.setEditable(false);

JOptionPane.showMessageDialog(null, "Correct");
c1.Check1.setVisible(true);

}
else
{

JOptionPane.showMessageDialog(null, "Wrong Answer");

}
}
else
{
System.exit(1);
}
}}

最佳答案

使 check1 成为类的字段并且不是静态的,然后创建一个用于设置可见性的公共(public)方法:

示例:

public class Category1 extends JPanel {
private JLabel check1;

public void setCheck1Visibility(boolean visible) {
check1.setVisible(visible);
}

public Category1() {
check1 = new JLabel(new ImageIcon(getClass().getResource("Buttons/Check.png")));
check1.setBounds(75, 305, 40, 40);
check1.setVisible(false);
add(check1);
}
}
<小时/>

既然您有一个 Category1 实例,您可以执行以下操作:

Category1 c1 = new Category1();
c1.setCheck1Visibility(true);
or
c1.setCheck1Visibility(false);

关于java - 我如何从另一个类设置Visible(true) JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36388623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com