使用Swing实现的一个简单弹窗功能,基本容器的使用办法,代码如下:
package test1;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.Jframe;
import javax.swing.JLabel;
public class MyJframe extends Jframe{
private static final long serialVersionUID = 1L;
public void Creatframe(){
Jframe jf = new Jframe();
//一般情况下,他不能被直接放在顶层容器中
Container con = jf.getContentPane();
JLabel jb = new JLabel("this is a pane");
JButton jt = new JButton("alert a new window");
jt.setBounds(10, 10, 100, 21);
jt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new MyDialog1(MyJframe.this).setVisible(true);
}
});
jb.setHorizontalAlignment(MAXIMIZED_HORIZ);
con.add(jt);
con.add(jb);
con.setBackground(Color.RED);
jf.setVisible(true);
jf.setSize(200, 150);
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String []args){
new MyJframe().Creatframe();
}
}
class MyDialog1 extends JDialog{
private static final long serialVersionUID = 1L;
public MyDialog1(Jframe frame){
super(frame,"alert the window");
Container conn = getContentPane();
conn.add(new JLabel("test lable"));
setBounds(100,100,100,100);
System.out.println("test succesfully");
}
}
执行结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



