java swing 做一窗体 上头有一菜单栏menubar 下面一工具栏toolbar 在下面是用户一些查询信息 怎么把toolba
javaswing做一窗体上头有一菜单栏menubar下面一工具栏toolbar在下面是用户一些查询信息怎么把toolbar和menubar加上去?我把menubar,t...
java swing 做一窗体 上头有一菜单栏menubar 下面一工具栏toolbar 在下面是用户一些查询信息 怎么把toolbar和menubar加上去 ?我把menubar,toolbar先加到一个panel里,再加到整个panel里,报错,应该用什么方法加??
展开
2个回答
展开全部
package test.combobox;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
public class TestFrame extends JFrame
{
JTextArea theArea=null;
static final String ComboStr[] = {"Times New Roman","Dialog","宋体","黑体","楷体"};
public TestFrame()
{
super("JMenu1");
theArea=new JTextArea();
theArea.setEditable(true);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(theArea),BorderLayout.CENTER);
JToolBar theBar=buildToolBar();
getContentPane().add(theBar,BorderLayout.NORTH);
JMenuBar MBar=new JMenuBar();
JMenu mfile=buildFileMenu();
MBar.add(mfile);
setJMenuBar(MBar);
}//end of JMenu1()
public JMenu buildFileMenu()
{
JMenu thefile=new JMenu("文件");
JMenuItem newf=new JMenuItem("新建");
JMenuItem open=new JMenuItem("打开");
JMenuItem close=new JMenuItem("关闭");
JMenuItem exit=new JMenuItem("退出");
thefile.add(newf);
thefile.add(open);
thefile.add(close);
thefile.addSeparator();//分隔线
thefile.add(exit);
return thefile;
}//end of buildFileMenu()
public JToolBar buildToolBar()
{
JToolBar toolBar=new JToolBar();
toolBar.setFloatable(true);//设置JToolBar可否浮动.
//分别构造三个ToolBarAction的类组件,准备将这三个组件设置到JToolBar中.ToolBaarAction类是我们自己编写的inner
// class,这个inner class在程序后半部份,这个类继承AbstractAction,AbstractAction是一个抽象类,实现Action
//interface,而Action interface又继承ActionListener interface又继承AActionListener interface,因此可直接在
//AbstractAction中覆写actionPerformed()方法,处理ActionEvent事件.在JToolBar中有一个add(Action a)方法,只要传入
//Action参数就可以得到一个JButton对象,因此在程序中下面的98,100,102就是以这样的方式来构造出JToolBar上的按钮组
//件.
ToolBarAction tba_new=new ToolBarAction("new",new ImageIcon(
"icons/new24.gif"));
ToolBarAction tba_open=new ToolBarAction("open",new ImageIcon(
"icons/open24.gif"));
ToolBarAction tba_close=new ToolBarAction("close",new ImageIcon(
"icons/close24.gif"));
JButton JB;
JB=toolBar.add(tba_new);//将ToolBarAction组件加入JToolBar中.
JB.setActionCommand("#TooBar_NEW performed!");
JB=toolBar.add(tba_open);//将ToolBarAction组件加入JToolBar中.
JB.setActionCommand("#ToolBar_OPEN performed!");
JB=toolBar.add(tba_close);//将ToolBarAction组件加入JToolBar中.
JB.setActionCommand("#ToolBar_CLOSE performed!");
toolBar.addSeparator();//在JToolBar加上分隔线,如同在JMenu上利用addSeparator()方法加上分隔线一样,不同的是在
//JMenu中分隔线是以灰色直线的样式表现,而在JToolBar中则是以一小段空来表示.
//在JToolBar中加入一个JLabel组件.
JLabel JLfont=new JLabel("Font Type");
toolBar.add(JLfont);
toolBar.addSeparator();
//利用上面定义的ComboStr数建立一个JComboBox组件.
JComboBox jcb = new JComboBox(ComboStr);
//将JComboBar加入事件处理模式.这里是将所选到的组件名称填入JTextArea中.
jcb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
theArea.append("*Combobox "+((JComboBox)e.getSource()).getSelectedItem()+
" performed!\n");
}});
toolBar.add(jcb);
return toolBar;
}//end of buildToolBar()
class ToolBarAction extends AbstractAction
{
public ToolBarAction(String name,Icon icon)
{
// super(name,icon);
super(name );
}
//对于JToolBar上按钮的事件处理是将组件的ActionCommand返回值字符串加入JTextArea中.
public void actionPerformed(ActionEvent e)
{
try
{
theArea.append(e.getActionCommand()+"\n");
} catch(Exception ex)
{
}
}
}
public static void main(String[] args)
{
JFrame F=new TestFrame();
F.setSize(400,200);
F.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});//end of addWindowListener
F.setVisible(true);
} // end of main
}//end of class JMenu1
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
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.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
public class TestFrame extends JFrame
{
JTextArea theArea=null;
static final String ComboStr[] = {"Times New Roman","Dialog","宋体","黑体","楷体"};
public TestFrame()
{
super("JMenu1");
theArea=new JTextArea();
theArea.setEditable(true);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(theArea),BorderLayout.CENTER);
JToolBar theBar=buildToolBar();
getContentPane().add(theBar,BorderLayout.NORTH);
JMenuBar MBar=new JMenuBar();
JMenu mfile=buildFileMenu();
MBar.add(mfile);
setJMenuBar(MBar);
}//end of JMenu1()
public JMenu buildFileMenu()
{
JMenu thefile=new JMenu("文件");
JMenuItem newf=new JMenuItem("新建");
JMenuItem open=new JMenuItem("打开");
JMenuItem close=new JMenuItem("关闭");
JMenuItem exit=new JMenuItem("退出");
thefile.add(newf);
thefile.add(open);
thefile.add(close);
thefile.addSeparator();//分隔线
thefile.add(exit);
return thefile;
}//end of buildFileMenu()
public JToolBar buildToolBar()
{
JToolBar toolBar=new JToolBar();
toolBar.setFloatable(true);//设置JToolBar可否浮动.
//分别构造三个ToolBarAction的类组件,准备将这三个组件设置到JToolBar中.ToolBaarAction类是我们自己编写的inner
// class,这个inner class在程序后半部份,这个类继承AbstractAction,AbstractAction是一个抽象类,实现Action
//interface,而Action interface又继承ActionListener interface又继承AActionListener interface,因此可直接在
//AbstractAction中覆写actionPerformed()方法,处理ActionEvent事件.在JToolBar中有一个add(Action a)方法,只要传入
//Action参数就可以得到一个JButton对象,因此在程序中下面的98,100,102就是以这样的方式来构造出JToolBar上的按钮组
//件.
ToolBarAction tba_new=new ToolBarAction("new",new ImageIcon(
"icons/new24.gif"));
ToolBarAction tba_open=new ToolBarAction("open",new ImageIcon(
"icons/open24.gif"));
ToolBarAction tba_close=new ToolBarAction("close",new ImageIcon(
"icons/close24.gif"));
JButton JB;
JB=toolBar.add(tba_new);//将ToolBarAction组件加入JToolBar中.
JB.setActionCommand("#TooBar_NEW performed!");
JB=toolBar.add(tba_open);//将ToolBarAction组件加入JToolBar中.
JB.setActionCommand("#ToolBar_OPEN performed!");
JB=toolBar.add(tba_close);//将ToolBarAction组件加入JToolBar中.
JB.setActionCommand("#ToolBar_CLOSE performed!");
toolBar.addSeparator();//在JToolBar加上分隔线,如同在JMenu上利用addSeparator()方法加上分隔线一样,不同的是在
//JMenu中分隔线是以灰色直线的样式表现,而在JToolBar中则是以一小段空来表示.
//在JToolBar中加入一个JLabel组件.
JLabel JLfont=new JLabel("Font Type");
toolBar.add(JLfont);
toolBar.addSeparator();
//利用上面定义的ComboStr数建立一个JComboBox组件.
JComboBox jcb = new JComboBox(ComboStr);
//将JComboBar加入事件处理模式.这里是将所选到的组件名称填入JTextArea中.
jcb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
theArea.append("*Combobox "+((JComboBox)e.getSource()).getSelectedItem()+
" performed!\n");
}});
toolBar.add(jcb);
return toolBar;
}//end of buildToolBar()
class ToolBarAction extends AbstractAction
{
public ToolBarAction(String name,Icon icon)
{
// super(name,icon);
super(name );
}
//对于JToolBar上按钮的事件处理是将组件的ActionCommand返回值字符串加入JTextArea中.
public void actionPerformed(ActionEvent e)
{
try
{
theArea.append(e.getActionCommand()+"\n");
} catch(Exception ex)
{
}
}
}
public static void main(String[] args)
{
JFrame F=new TestFrame();
F.setSize(400,200);
F.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});//end of addWindowListener
F.setVisible(true);
} // end of main
}//end of class JMenu1
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询