我试图在单击JButton时显示JLabel.我添加了一个动作侦听器并将该组件添加到布局中.在actionPerformed中单击JButton时,我正在使用label1.setVisible(true).我仍然无法使它工作.有人可以看看我的代码吗?
public class LearnAppMain extends JFrame implements ActionListener {
// Define variables
public JButton button1;
public JLabel label1;
public JTextField field1;
private Image image1;
private String apple = "apple.jpg";
public LearnAppMain() {
ImageIcon image1 = new ImageIcon(this.getClass().getResource(apple));
JLabel label1 = new JLabel(image1);
button1 = new JButton("A");
button1.addActionListener(this);
field1 = new JTextField(10);
// Create layout
setLayout(new FlowLayout());
// create Container
final Container cn = getContentPane();
cn.add(button1);
cn.add(field1);
cn.add(label1);
// setLayout(new FlowLayout());
setSize(250, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent …Run Code Online (Sandbox Code Playgroud) 我知道如何创建一个PrintWriter并且能够从我的gui中获取字符串并将其打印到文本文件中.
我希望能够采用相同的程序并打印到文件中,将文本添加到文件中,而不是替换文本文件中已有的所有内容.我如何制作它,以便当更多数据添加到文本文件时,每次都会在新行上打印?
任何示例或资源都很棒.