
正文
java菜单栏工具栏代码 java中工具栏消失了
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java添加菜单条和按钮
试试这样行不?
public class Mazelp {/*extends JFrame*/ //implements ActionListener{
private static final int wid = 10;
private static final int hei = 10;
JFrame jf;
JButton jb1,jb2;
JButton jb[];
JPanel p1,p2;
private Stack stack = new Stack();//Stack 类表示后进先出(LIFO)的对象堆栈。
MenuBar menu;
Menu file;
MenuItem closeMenu;
public Mazelp() {
jf=new JFrame("迷宫");//申请内存空间设置标题
jf.setBounds(300,240,500,500); //调整迷宫出现的位置(300,240)及大小(500,500)
jf.setResizable(false);//窗体不可拉伸
menu = new MenuBar();//设置菜单条
file = new Menu("文件");//设置菜单栏
closeMenu = new MenuItem("关闭");//设置菜单项
//closeMenu.addActionListener(this);//添加监听对菜单项
p1=new JPanel();
//jf.add(menu);
jf.setMenuBar(menu);
menu.add(file);//将菜单栏添加到菜单条上
file.add(closeMenu);//将菜单项添加到菜单栏
jf.getContentPane().add(p1);
p1.setLayout(new GridLayout(10,10)); //p1用网格布局,10行10列
jb=new JButton[100];//作为迷宫的墙和路
for(int i=0;ijb.length;i++){
jb[i]=new JButton(Integer.toString(i));//创建按键的名字,Integer型的名字为i的字符串
if((i=0i=9)||(i=90i=99)||i%10==0||i%10==9||i==13||i==17||i==23||i==27||i==35||i==36||i==42||i==43||i==44||i==54||i==62||i==66||i==72||i==73||i==74||i==76||i==77||i==81){
jb[i].setBackground(Color.red);//将墙涂色
}
else {
jb[i].setBackground(Color.yellow);
}
jb[i].setSize(10,10);
p1.add(jb[i]);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE); //声明点“X”图标后结束窗体所在的应用程序
jf.setVisible(true); //表明以上创建的所有窗体、按键等组件都是可见
}
}
相关问答
Q1: java设计一个创建二级菜单的程序。
100分,100分,100分, 重要的事情说三遍..~
我来1个参考案例
效果图
参考代码
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
// 本来继承自JFrame .实现ActionListener接口
public class TextFrame extends JFrame implements ActionListener {
private final JTextArea jta;
boolean isBold, isItalic;
public TextFrame() {
jta = new JTextArea();
jta.setLineWrap(true); // 自动换行
Font font = new Font(Font.DIALOG, Font.PLAIN, 26);
jta.setFont(font);
JScrollPane jsp = new JScrollPane(jta);// 会自动生成滚动条的面板
add(jsp);
//菜单栏的创建和设置
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
JMenuItem jmia01 = new JMenuItem("About");
jmia01.addActionListener(this);
JMenuItem jmia02 = new JMenuItem("Exit");
jmia02.addActionListener(this);
menuFile.add(jmia01);
menuFile.add(jmia02);
JMenu menuFormat = new JMenu("Format");
JMenu menuColor = new JMenu("Color");
JMenuItem jmib01 = new JMenuItem("Blue");
jmib01.addActionListener(this);
JMenuItem jmib02 = new JMenuItem("Red");
jmib02.addActionListener(this);
JMenuItem jmib03 = new JMenuItem("Yellow");
jmib03.addActionListener(this);
menuColor.add(jmib01);
menuColor.add(jmib02);
menuColor.add(jmib03);
JMenu menuFont = new JMenu("Font");
JMenuItem jmic01 = new JMenuItem("Bold");
jmic01.addActionListener(this);
JMenuItem jmic02 = new JMenuItem("Italic");
jmic02.addActionListener(this);
menuFont.add(jmic01);
menuFont.add(jmic02);
menuFormat.add(menuColor);
menuFormat.add(menuFont);
menuBar.add(menuFile);
menuBar.add(menuFormat);
setJMenuBar(menuBar);//设置菜单栏
setTitle("文本编辑窗口"); // 设置标题
setSize(520, 350);//设置窗口大小
setLocationRelativeTo(null);//设置窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置窗口点击关闭按钮时,退出
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();// 获取ActionCommand ,用于判断是哪一个按钮被点击了
if (cmd.equals("Exit")) {
System.exit(0);
} else if (cmd.equals("About")) {
JOptionPane.showMessageDialog(this, "程序版本Ver1.0", "About", JOptionPane.INFORMATION_MESSAGE);
} else if (cmd.equals("Blue")) {
jta.setForeground(Color.BLUE);
} else if (cmd.equals("Red")) {
jta.setForeground(Color.RED);
} else if (cmd.equals("Yellow")) {
jta.setForeground(Color.YELLOW);
} else if (cmd.equals("Bold")) { // 第一次选择Bold 会加粗,第二次选择Bold会取消加粗
isBold = !isBold;
if (isBold) {
jta.setFont(new Font(Font.DIALOG, Font.BOLD, 26));
} else {
jta.setFont(new Font(Font.DIALOG, Font.PLAIN, 26));
}
} else if (cmd.equals("Italic")) {
isItalic = !isItalic;
if (isItalic) {
jta.setFont(new Font(Font.DIALOG, Font.ITALIC, 26));
} else {
jta.setFont(new Font(Font.DIALOG, Font.PLAIN, 26));
}
}
}
public static void main(String[] args) {
new TextFrame().setVisible(true);// 创建窗口设置可见
}
}
Q2: javaSwing如何创建一个有工具条和菜单的窗口
代码如下:
public class App extends JFrame {
public App() {
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加菜单
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
JMenu menu = new JMenu("文件");
menuBar.add(menu);
JMenuItem testMenuItem = new JMenuItem("打开");
testMenuItem.addActionListener(e - JOptionPane.showMessageDialog(this, "打开"));
menu.add(testMenuItem);
// 添加工具栏
JToolBar toolBar = new JToolBar();
this.add(toolBar, BorderLayout.NORTH);
JButton btnSave = new JButton("保存");
btnSave.addActionListener(e - JOptionPane.showMessageDialog(this, "保存"));
toolBar.add(btnSave);
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
运行结果:
Q3: 用java编写一个界面,界面里有菜单栏,菜单栏可以下拉。
import java.awt.*;
import javax.swing.*;
public class MenuDemo extends JFrame{
JMenu jm1,jm2,jm3;
JMenuBar jmb;
JMenuItem jmt1,jmt2,jmt3;
public MenuDemo()
{
//菜单条
jmb=new JMenuBar();
jm1=new JMenu("文件");
jm2=new JMenu("编辑");
jmb.add(jm1);
jmb.add(jm2);
jmt2=new JMenuItem("关闭");
jmt3=new JMenuItem("退出");
jm3=new JMenu("新建");
jm3.add(jmt3);
jm1.add(jmt2);
jm1.add(jm3);
this.add(jmb,"North");
this.setVisible(true);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[])
{
new MenuDemo();
}
}
运行下看看是不是你想要的
java菜单栏工具栏代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java中工具栏消失了、java菜单栏工具栏代码的信息别忘了在本站进行查找喔。







