import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MiniDrawPad extends JFrame //主类,扩展了JFrame类,用来生成主界面
{
private ObjectInputStream input;
private ObjectOutputStream output; //定义输入输出流,用来调用和保存图像文件 private JButton choices[]; //按钮数组,存放以下名称的功能按钮 private String names[]={
"New",
"Open",
"Save", //这三个是基本操作按钮,包括"新建"、"打开"、"保存"
"Pencil", //铅笔画,也就是用鼠标拖动着随意绘图
}; private String styleNames[]={
" 宋体 " , " Times New Roman " , " Serif "
}; //可供选择的字体项
private Icon items[];
JToolBar buttonPanel ; //定义按钮面板
private JLabel statusBar; //显示鼠标状态的提示条 private DrawPanel drawingArea; //画图区域 JCheckBox bold,italic; //定义字体风格选择框
JComboBox styles; public MiniDrawPad() //构造函数
{
super("Drawing Pad");
JMenuBar bar=new JMenuBar(); //定义菜单条
JMenu fileMenu=new JMenu("File");
fileMenu.setMnemonic('F');//新建文件菜单条
JMenuItem newItem=new JMenuItem("New");
fileMenu.add(newItem);//保存文件菜单项
JMenuItem saveItem=new JMenuItem("Save");
fileMenu.add(saveItem);//打开文件菜单项
JMenuItem loadItem=new JMenuItem("Load");
fileMenu.add(loadItem); fileMenu.addSeparator();//退出菜单项
JMenuItem exitItem=new JMenuItem("Exit");
fileMenu.add(exitItem);
bar.add(fileMenu);//设置颜色菜单条
JMenu colorMenu=new JMenu("Color");
colorMenu.setMnemonic('C');//选择颜色菜单项
JMenuItem colorItem=new JMenuItem("Choose Color");
colorMenu.add(colorItem);
bar.add(colorMenu); items=new ImageIcon[names.length];//创建各种基本图形的按钮
drawingArea=new DrawPanel();
choices=new JButton[names.length];
buttonPanel = new JToolBar( JToolBar.VERTICAL ) ;
buttonPanel = new JToolBar( JToolBar.HORIZONTAL) ;//导入需要的图形图标,这些图标都存放在与源文件相同的目录下面
for(int i=0;i<choices.length;i++)
{items[i]=new ImageIcon(names[i] + ".gif");
choices[i]=new JButton("",items[i]);
choices[i].setToolTipText(tipText[i]);
buttonPanel.add(choices[i]);
public static void main(String[] arguments){
JFrame newPad=new MiniDrawPad();
ExitWindow exit=new ExitWindow();
frame.addWindowListener(exit);
}
}class ExitWindow extends WindowAdaper{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
这段代码我想实现一个图形化界面,为什么编译有错?
我只是觉得是main的作用域的问题,但找不出问题来
我是初学者,所以麻烦各位前辈了.谢谢谢谢
import java.awt.event.*;
import javax.swing.*;
public class MiniDrawPad extends JFrame //主类,扩展了JFrame类,用来生成主界面
{
private ObjectInputStream input;
private ObjectOutputStream output; //定义输入输出流,用来调用和保存图像文件 private JButton choices[]; //按钮数组,存放以下名称的功能按钮 private String names[]={
"New",
"Open",
"Save", //这三个是基本操作按钮,包括"新建"、"打开"、"保存"
"Pencil", //铅笔画,也就是用鼠标拖动着随意绘图
}; private String styleNames[]={
" 宋体 " , " Times New Roman " , " Serif "
}; //可供选择的字体项
private Icon items[];
JToolBar buttonPanel ; //定义按钮面板
private JLabel statusBar; //显示鼠标状态的提示条 private DrawPanel drawingArea; //画图区域 JCheckBox bold,italic; //定义字体风格选择框
JComboBox styles; public MiniDrawPad() //构造函数
{
super("Drawing Pad");
JMenuBar bar=new JMenuBar(); //定义菜单条
JMenu fileMenu=new JMenu("File");
fileMenu.setMnemonic('F');//新建文件菜单条
JMenuItem newItem=new JMenuItem("New");
fileMenu.add(newItem);//保存文件菜单项
JMenuItem saveItem=new JMenuItem("Save");
fileMenu.add(saveItem);//打开文件菜单项
JMenuItem loadItem=new JMenuItem("Load");
fileMenu.add(loadItem); fileMenu.addSeparator();//退出菜单项
JMenuItem exitItem=new JMenuItem("Exit");
fileMenu.add(exitItem);
bar.add(fileMenu);//设置颜色菜单条
JMenu colorMenu=new JMenu("Color");
colorMenu.setMnemonic('C');//选择颜色菜单项
JMenuItem colorItem=new JMenuItem("Choose Color");
colorMenu.add(colorItem);
bar.add(colorMenu); items=new ImageIcon[names.length];//创建各种基本图形的按钮
drawingArea=new DrawPanel();
choices=new JButton[names.length];
buttonPanel = new JToolBar( JToolBar.VERTICAL ) ;
buttonPanel = new JToolBar( JToolBar.HORIZONTAL) ;//导入需要的图形图标,这些图标都存放在与源文件相同的目录下面
for(int i=0;i<choices.length;i++)
{items[i]=new ImageIcon(names[i] + ".gif");
choices[i]=new JButton("",items[i]);
choices[i].setToolTipText(tipText[i]);
buttonPanel.add(choices[i]);
public static void main(String[] arguments){
JFrame newPad=new MiniDrawPad();
ExitWindow exit=new ExitWindow();
frame.addWindowListener(exit);
}
}class ExitWindow extends WindowAdaper{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
这段代码我想实现一个图形化界面,为什么编译有错?
我只是觉得是main的作用域的问题,但找不出问题来
我是初学者,所以麻烦各位前辈了.谢谢谢谢
import java.awt.event.WindowEvent;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
public class MiniDrawPad extends JFrame // 主类,扩展了JFrame类,用来生成主界面
{
private ObjectInputStream input; private ObjectOutputStream output; // 定义输入输出流,用来调用和保存图像文件 private JButton choices[]; // 按钮数组,存放以下名称的功能按钮 private String names[] = { "New", "Open", "Save", // 这三个是基本操作按钮,包括"新建"、"打开"、"保存"
"Pencil",// 铅笔画,也就是用鼠标拖动着随意绘图
}; private String styleNames[] = { " 宋体 ", " Times New Roman ", " Serif " }; // 可供选择的字体项 private Icon items[]; JToolBar buttonPanel; // 定义按钮面板 private JLabel statusBar; // 显示鼠标状态的提示条 private DrawPanel drawingArea; // 画图区域 JCheckBox bold, italic; // 定义字体风格选择框 JComboBox styles; public MiniDrawPad() // 构造函数
{
super("Drawing Pad");
JMenuBar bar = new JMenuBar();// 定义菜单条
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F'); // 新建文件菜单条
JMenuItem newItem = new JMenuItem("New");
fileMenu.add(newItem); // 保存文件菜单项
JMenuItem saveItem = new JMenuItem("Save");
fileMenu.add(saveItem); // 打开文件菜单项
JMenuItem loadItem = new JMenuItem("Load");
fileMenu.add(loadItem); fileMenu.addSeparator(); // 退出菜单项
JMenuItem exitItem = new JMenuItem("Exit");
fileMenu.add(exitItem);
bar.add(fileMenu); // 设置颜色菜单条
JMenu colorMenu = new JMenu("Color");
colorMenu.setMnemonic('C'); // 选择颜色菜单项
JMenuItem colorItem = new JMenuItem("Choose Color");
colorMenu.add(colorItem);
bar.add(colorMenu); items = new ImageIcon[names.length]; // 创建各种基本图形的按钮
drawingArea = new DrawPanel(); //这个DrawPanel是什么东西???
choices = new JButton[names.length];
buttonPanel = new JToolBar(JToolBar.VERTICAL);
buttonPanel = new JToolBar(JToolBar.HORIZONTAL); // 导入需要的图形图标,这些图标都存放在与源文件相同的目录下面
for (int i = 0; i < choices.length; i++) {
items[i] = new ImageIcon(names[i] + ".gif");
choices[i] = new JButton("", items[i]);
choices[i].setToolTipText(tipText[i]); //这个tipText又是哪里来的?
buttonPanel.add(choices[i]);
}
} public static void main(String[] arguments) {
JFrame newPad = new MiniDrawPad();
ExitWindow exit = new ExitWindow();
frame.addWindowListener(exit);//<<<<<<<这里的这个frame哪里来的?>>>>>>>>>
}
}class ExitWindow extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
JFrame newPad=new MiniDrawPad();
ExitWindow exit=new ExitWindow();
frame.addWindowListener(exit);
}
其中frame没有定义~