java添加菜单条和按钮

java添加菜单条和按钮,第1张

试试这样行不?

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=0i<jb.lengthi++){

jb[i]=new JButton(Integer.toString(i))//创建按键的名字,Integer型的名字为i的字符串

if((i>=0&&i<=9)||(i>=90&&i<=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)//表明以上创建的所有窗体、按键等组件都是可见

}

}

上面那个是下拉式菜单吧 JComboBox.setEditable(true)就可以输入数据

下面那个用个面板把JPanel左边输入框 右边添加一个JButton 边距弄成0 让他们两个贴在一起完事

问题补充做打

首先楼主你的Dialog

其实已经添加了按钮,只不过一开始没有显示

你需要用鼠标拖动一下对话框,才能显示按钮,

第二添加事件监听就象我以前说的一样

使用的是btn.addActionListener(new ActionListener(){

事件代码

})

我补充了一下你的代码

如下:

import java.awt.BorderLayout

import java.awt.Color

import java.awt.Container

import java.awt.Dialog

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.awt.event.WindowAdapter

import java.awt.event.WindowEvent

import java.io.File

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JPanel

import javax.swing.JTextField

public class Del {

JTextField pathField

JFrame delFrame

JPanel panel

Dialog completeDialog

public static void main(String[] args) {

new Del()

}

public Del() {

frameDel()

}

public void frameDel() {

delFrame = new JFrame("文件删除器")

delFrame.setBounds(150, 150, 500, 200)

delFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

delFrame.setVisible(true)

delFrame.setLayout(null)

panel = new JPanel()

Container con = delFrame.getContentPane()

con.setBounds(0, 0, 400, 200)

con.setBackground(new Color(222, 111, 111))

con.setLayout(null)

pathField = new JTextField()

pathField.setBounds(30, 30, 250, 30)

con.add(pathField)

JButton delButton = new JButton("删除")

delButton.setBounds(350, 30, 60, 30)

con.add(delButton)

delButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

delFile(pathField.getText())

}

})

delFrame.validate()

delFrame.repaint()

}

public void delFile(String path) {

File goalFile = new File(path)

if (goalFile.exists()) {

goalFile.delete()

completeDialog = new Dialog(delFrame, "删除结果提示")

completeDialog.setVisible(true)

completeDialog.setBounds(250, 250, 250, 90)

completeDialog.setModal(true)

JButton testButton = new JButton("继续删除")

JButton testButton2 = new JButton("退出")

completeDialog.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e) {

System.exit(0)

}

})

testButton2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.exit(0)

}

})

testButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

completeDialog.setVisible(false)

}

})

panel.add(testButton,BorderLayout.WEST)

panel.add(testButton2,BorderLayout.EAST)

completeDialog.add(panel,BorderLayout.CENTER)

pathField.setText("文件删除成功!")

} else {

pathField.setText("文件不存在!")

}

}

}

不过缺点在于以显示的时候只有一个对话框,没有按钮,只有用鼠标拖动对话框的大小后才能显示按钮,我这个在找方法,希望你能找到方法解决,我对javaGUI学的不深,希望有哪位大侠可以给个方法解决一下一开始不显示按钮的问题


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11646494.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存