点击“OK”按钮,希望在frame中出现label"The 'OK' button clicked",该怎么做阿,急啊!
package programmingExercises;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Exercise14_3last extends JFrame{
public Exercise14_3last(){
this.setLayout(new BorderLayout());
JLabel label=new JLabel("THE RESULT WILL BE PLAYED HERE");
this.add(label, BorderLayout.NORTH);
JPanel panel=new JPanel();
JButton jbtok=new JButton("OK");
JButton jbtcancel =new JButton("Cancel");
panel.add(jbtok);
panel.add(jbtcancel);
this.add(panel,BorderLayout.SOUTH);
jbtok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
add(new JLabel("The 'OK' button clicked"),BorderLayout.CENTER);
}
} );
}
public static void main(String[] args){
Exercise14_3last frame = new Exercise14_3last();
frame.setSize(300,200);
frame.setTitle("Exercise 14_3");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
后面添加两行:
Exercise14_3last.this.invalidate();
Exercise14_3last.this.validate();重新布局一下就好了
或者:
Exercise14_3last.repaint();
Exercise14_3last .this.show();两种方法都能解决问题