日期:2014-05-20  浏览次数:20635 次

请帮我编译下这个问题 Label无法显示 求助!!!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JLabel.*;

class MyJDialog extends JDialog { // 创建新类继承JDialog类
public MyJDialog(Land frame) {
// 实例化一个JDialog类对象,指定对话框的父窗体、窗体标题和类型
super(frame, "第一个JDialog窗体", true);
Container container = getContentPane(); // 创建一个容器
container.add(new JLabel("这是一个对话框")); // 在容器中添加标签
setBounds(740, 350, 200, 200); // 设置对话框窗体大小
}
}
public class Land extends JFrame{
public void CreateJFrame(String title){
JFrame jf=new JFrame(title);
Container Container_=jf.getContentPane();
JLabel txt_1=new JLabel("User:");
txt_1.setHorizontalAlignment(SwingConstants.CENTER);
Container_.setLayout(null);
//setLayout(new BorderLayout());
JButton bl = new JButton("OK"); // 定义一个按钮
bl.setBounds(120,200,60,30);
bl.addActionListener(new ActionListener() { // 为按钮添加鼠标单击事件
public void actionPerformed(ActionEvent e) {
// 使MyJDialog窗体可见
new MyJDialog(Land.this).setVisible(true);
}
});
bl.setBorderPainted(true);
JButton b2 = new JButton("Cancel"); // 定义一个按钮
b2.setBounds(220,200,60,30);
b2.addActionListener(new ActionListener() { // 为按钮添加鼠标单击事件
public void actionPerformed(ActionEvent e) {
// 使MyJDialog窗体可见
new MyJDialog(Land.this).setVisible(false);
}
});
b2.setBorderPainted(true);
//txt_1.setQpaque(true);
Container_.add(bl); // 将按钮添加到容器中
Container_.add(b2);
Container_.add(txt_1);
Container_.add(BorderLayout.SOUTH,bl);
Container_.add(BorderLayout.SOUTH,b2);
Container_.setBackground(Color.white);
jf.setVisible(true);
jf.setSize(400, 300);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String args[]){
Land win=new Land();
win.CreateJFrame("Welcome in Liberary System");
}
}

------解决方案--------------------
public class Land extends JFrame
{
public void CreateJFrame(String title)
{
Container Container_ = getContentPane();
Container_.setLayout(new FlowLayout());


JLabel txt_1 = new JLabel("User:");
txt_1.setHorizontalAlignment(SwingConstants.CENTER);

//setLayout(new BorderLayout());
JButton bl = new JButton("OK"); // 定义一个按钮
//bl.setBounds(120, 200, 60, 30);
// bl.addActionListener(new ActionListener()
// { // 为按钮添加鼠标单击事件
// public void actionPerformed(ActionEvent e)
// {
// // 使MyJDialog窗体可见
// new MyJDialog(Land.this).setVisible(true);
// }
// });
bl.setBorderPainted(true);
JButton b2 = new JButton("Cancel"); // 定义一个按钮
b2.setBounds(220, 200, 60, 30);
b2.setVisible(true);
b2.setSize(10,10);
// b2.addActionListener(new ActionListener()
// { // 为按钮添加鼠标单击事件
// public void actionPerformed(ActionEvent e)
// {
// // 使MyJDialog窗体可见
// new MyJDialog(Land.this).setVisible(false);
// }
// });
 
b2.setBorderPainted(true);
//txt_1.setQpaque(true);
Container_.add(bl); // 将按钮添加到容器中
Container_.add(b2);
Container_.add(txt_1);
// Container_.add(BorderLayout.SOUTH, bl);
// Container_.add(BorderLayout.SOUTH, b2);
// Container_.setBackground(Color.white);
}

public static void main(String args[])
{
Land win = new Land();