标题:创造图形界面
只看楼主
wc524500
Rank: 1
等 级:新手上路
帖 子:10
专家分:2
注 册:2010-9-14
结帖率:66.67%
 问题点数:0 回复次数:1 
创造图形界面
package yonghujiemian;
import java.awt.*;
import javax.swing.*;
public class layoutdemo extends JFrame
{
    JPanel pNorth;
    JLabel lblDrive;
    JComboBox cboDrive;
    JLabel lblSize;
    JTextField txtSize;
    JButton btnStart;
    JButton btnQuit;
    JTextArea areaStatus;
    JProgressBar pBar;
    public layoutdemo()
    {
        super("往磁盘写随机文件(务必谨慎使用)");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pNorth=new JPanel();
        getContentPane().add("north",pNorth);
        lblDrive=new JLabel("选择路径:");
        cboDrive=new JComboBox();
        for (char ch='A';ch<='H';ch++)
            cboDrive.addItem(ch+":");
        cboDrive.setSelectedIndex(2);
        lblSize=new JLabel("输入要生成文件的大小:");
        txtSize=new JTextField("100M",6);
        txtSize.setToolTipText("最大可输入65535G!");
        btnStart=new JButton("开始写程序(W)");
        btnQuit=new JButton("退出程序(Q)");
        btnStart.setMnemonic('W');
        btnQuit.setMnemonic('Q');
        pNorth.setLayout(new FlowLayout(FlowLayout.CENTER,3,3));
        pNorth.add(lblDrive);
        pNorth.add(cboDrive);
        pNorth.add(lblSize);
        pNorth.add(txtSize);
        pNorth.add(btnStart);
        pNorth.add(btnQuit);
        areaStatus=new JTextArea("启动就绪,等待你的操作…" +
                "\n提示:输入文件大小,可以用字母M或者G结尾!",7,12);
        areaStatus.setEditable(false);
        String sOsName=System.getProperty("os.name");
        areaStatus.setToolTipText(sOsName+ " Version");
        getContentPane().add("Center",scPane);
        pBar=new JProgressBar();
        getContentPane().add("South",pBar);
        Font f =new Font(null,Font.BOLD,18);
        lblDrive.setFont(f);
        cboDrive.setFont(f);
        lblSize.setFont(f);
        txtSize.setFont(f);
        btnStart.setFont(f);
        btnQuit.setFont(f);
        areaStatus.setFont(f);
        pack();
        Dimension dScreen=Toolkit.getDefaultToolkit().getScreenSize();
        double wS=dScreen.getWidth();
        double hS=dScreen.getHeight();
        int x=(int) (wS-getWidth())/2;
        int y=(int) (hS-getHeight())/2;
        setLocation(x,y);
        setVisible(true);
    }
    public static void main(String args[])
    {
        layoutdemo objHD=new layoutdemo();
    }
}
不知道为什么总是运行不了,错误提醒是:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    scPane cannot be resolved

    at yonghujiemian.layoutdemo.<init>(layoutdemo.java:45)
    at yonghujiemian.layoutdemo.main(layoutdemo.java:67)
请大家帮帮忙,看看应该怎么改。谢了啦
搜索更多相关主题的帖子: super 
2011-03-05 01:23
HanLe0814
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2018-5-10
得分:0 
回复 楼主 wc524500
程序代码:
import java.awt.*;
import javax.swing.*;
public class LayoutDemo extends JFrame
{
  JPanel pNorth;
  JLabel lblDrive;
  JComboBox cboDrive;
  JLabel lblSize;
  JTextField txtSize;
  JButton btnStart;
  JButton btnQuit;
  JTextArea areaStatus;
  JProgressBar pBar;
  //构造方法
  public LayoutDemo()
  {
    super("往磁盘写随机文件(务必谨慎使用)");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    pNorth=new JPanel();
    getContentPane().add("North",pNorth);
    //添加JComboBox
    lblDrive=new JLabel("选择路径:");
    cboDrive=new JComboBox();
    for(char ch='A';ch<='H';ch++)
    cboDrive.addItem(ch+":");
    cboDrive.setSelectedIndex(2); //默认选中C盘
    //添加标签、文本框、按钮
    lblSize=new JLabel("输入要生成文件的大小:");

    txtSize=new JTextField("100M",6);
    txtSize.setToolTipText("最大可以输入65535G!");
    btnStart=new JButton("开始写操作(W)");
    btnQuit=new JButton("退出程序(Q)");
    //设置快捷键
    btnStart.setMnemonic('W');
    btnQuit.setMnemonic('Q');
    //设置布局方式,添加组件
    pNorth.setLayout(new FlowLayout(FlowLayout.CENTER,3,3));
    pNorth.add(lblDrive);
    pNorth.add(cboDrive);
    pNorth.add(lblSize);
    pNorth.add(txtSize);
    pNorth.add(btnStart);
    pNorth.add(btnQuit);
    //添加屏幕中间的文本区,下方的进度条
    areaStatus=new JTextArea("启动就绪,等待您的操作...\n提示:输入文件大小,可以用字母M或者G结尾!",7,12);
    areaStatus.setEditable(false);
    String sOsName=System.getProperty("os.name");
    areaStatus.setToolTipText(sOsName+" Version");
    JScrollPane scPane=new JScrollPane(areaStatus);
    getContentPane().add("Center",scPane);
    pBar=new JProgressBar();
    getContentPane().add("South",pBar);
    //设置字体
    Font f=new Font(null,Font.BOLD,18);
    lblDrive.setFont(f);
    cboDrive.setFont(f);
    lblSize.setFont(f);
    txtSize.setFont(f);
    btnStart.setFont(f);
    btnQuit.setFont(f);
    areaStatus.setFont(f);
    pack(); //压缩和整理窗口
    Dimension dScreen=Toolkit.getDefaultToolkit().getScreenSize();
    //屏幕的宽度和高度
    double wS=dScreen.getWidth();
    double hS=dScreen.getHeight();
    //窗口x座标和y座标
    int x=(int)(wS-getWidth())/2;
    int y=(int)(hS-getHeight())/2;
    //位于屏幕中心
    setLocation(x,y);
    setVisible(true);
  }//end of 构造
  //main方法
  public static void main(String args[])
  {
    LayoutDemo objHD=new LayoutDemo();
  }//end of main
}//end of class


它会出现一个警告,无需理会,直接运行即可。
2018-05-10 22:47



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-333260-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.984779 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved