求助!显示结果有问题,要拉动边框才会显示出来
程序代码:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ImproveProgramme extends JFrame{ JLabel label = new JLabel("Enter a new message"); JTextField jtf = new JTextField(10); public static void main(String[] args) { ImproveProgramme frame = new ImproveProgramme(); frame.setTitle("improve 15_2"); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300,200); frame.setVisible(true); } public ImproveProgramme(){ JPanel p = new JPanel(); p.add(label); p.add(jtf); jtf.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ jtf.requestFocusInWindow(); } }); jtf.addKeyListener(new KeyAdapter(){ public void keyReleased(KeyEvent e){ if(e.getKeyCode()==KeyEvent.VK_ENTER){ JLabel jlb = new JLabel(jtf.getText()); add(jlb); } } }); add(p); } }