import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
class MyWindow extends JFrame implements ActionListener{
JTextField text;
JButton jb;
Box box;

MyWindow(String s){
setTitle(s);
jb = new JButton("确定");
text = new JTextField(12);
jb.addActionListener(this);

box = Box.createVerticalBox();
box.add(text);
box.add(Box.createVerticalStrut(10));
box.add(jb);

setBounds(300,300,300,300);
setVisible(true);
validate();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e){
if(e.getSource().equals(jb)){
text.setText("确定");
}
}
}public class Ex {
public static void main(String[] args){
MyWindow win = new MyWindow("窗口");
}
}=============
题意是点击“确定”按钮,文本框中显示“确定”,我用box想在上面显示文本框,下面显示按钮,可是界面中没有

解决方案 »

  1.   


    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.Box;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;public class Ex extends JFrame implements ActionListener {
    JTextField text;
    JButton jb;
    Box box; Ex(String s) {
    setTitle(s);
    jb = new JButton("确定");
    text = new JTextField(12);
    jb.addActionListener(this); box = Box.createVerticalBox();
    box.add(text);
    box.add(Box.createVerticalStrut(10));
    box.add(jb); add(box);
    setBounds(300, 300, 300, 300);
    setVisible(true);
    validate();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    } public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(jb)) {
    text.setText("确定");
    }
    }

    public static void main(String[] args) {
    new Ex("窗口");
    }
    }够粗心
    box.add(Box.createVerticalStrut(10));
    box.add(jb);add(box);
    setBounds(300, 300, 300, 300);另外我实在不明白你的类结构,为什么外部类就一个main,也没见你有package,直接照我这么可以了啦
      

  2.   

    你忘了在把Button加入到frame里了,就是在构造函数里面加上
    add(box);
    就可以了。
    刚开始学swing,总会忘了加入,以后小心。
      

  3.   

    上面打错了,是把box加到frame里。