编辑java菜单
发布网友
发布时间:2022-04-25 14:49
我来回答
共2个回答
热心网友
时间:2023-10-09 22:51
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MyTest extends JFrame implements ActionListener {
JMenuBar jMenuBar1 = new JMenuBar();// 寤虹珛凿滃崟镙
JMenu fileMenu = new JMenu("File(F)");// 寤虹珛凿滃崟缁
JMenuItem saveItem = new JMenuItem("save(S)");
JMenuItem saveAsItem = new JMenuItem("save As(A)");
JMenuItem closeItem = new JMenuItem("close(C)");
JMenuItem exitItem = new JMenuItem("exit(E)");
public MyTest(){
this.setLayout(new BorderLayout());
fileMenu.add(saveItem);
fileMenu.add(saveAsItem);
fileMenu.add(closeItem);
fileMenu.add(exitItem);
jMenuBar1.add(fileMenu);
exitItem.addActionListener(this);
exitItem.setMnemonic('E');
closeItem.addActionListener(this);
closeItem.setMnemonic('C');
saveAsItem.addActionListener(this);
saveAsItem.setMnemonic('A');
saveItem.addActionListener(this);
saveItem.setMnemonic('S');
this.add(jMenuBar1,BorderLayout.NORTH);
this.setSize(500,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == saveItem) {
System.out.println("saveItem");
}
if (e.getSource() == saveAsItem) {
System.out.println("saveAsItem");
}
if (e.getSource() == closeItem) {
System.out.println("closeItem");
}
if (e.getSource() == exitItem) {
System.out.println("exitItem");
System.exit(0);
}
}
public static void main(String[] args){
new MyTest();
}
}
热心网友
时间:2023-10-09 22:51
只能打开txt文件
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
public class TestMenuBar
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new Jframe().buildFrame();
}
});
}
}
class Jframe extends JFrame
{
public Jframe()
{
}
public Jframe(String string)
{
super(string);
}
JMenuBar jmb;
JMenu jmenu;
JMenuItem jmi1, jmi2, jmi3;
JTextArea jta;
public void buildFrame()
{
jmb = new JMenuBar();
jmenu = new JMenu("操作");
jmi1 = new JMenuItem("打开");
jmi2 = new JMenuItem("保存");
jmi3 = new JMenuItem("退出");
jta = new JTextArea();
add(jta);
setJMenuBar(jmb);
jmb.add(jmenu);
jmenu.add(jmi1);
jmenu.add(jmi2);
jmenu.add(jmi3);
jmi1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
KeyEvent.CTRL_MASK));
jmi2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
KeyEvent.CTRL_MASK));
jmi3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
KeyEvent.CTRL_MASK));
jmi1.addActionListener(new Action());
jmi2.addActionListener(new Action());
jmi3.addActionListener(new Action());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 300);
setVisible(true);
}
class Action implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand() == "打开")
{
JFileChooser jfc = new JFileChooser();
int result = jfc.showOpenDialog(Jframe.this);
File file = null;
if (result == jfc.APPROVE_OPTION)
{
file = jfc.getSelectedFile();
try
{
FileInputStream fr = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fr);
byte b[] = new byte[(int) file.length()];
System.out.println(file.length());
bis.read(b);
jta.setText(new String(b));
bis.close();
} catch (IOException ex)
{
ex.printStackTrace();
}
}
} else if (e.getActionCommand() == "保存")
{
String string = jta.getText();
JFileChooser jfc = new JFileChooser();
int result = jfc.showSaveDialog(Jframe.this);
File file = null;
if (result == jfc.APPROVE_OPTION)
{
file = jfc.getSelectedFile();
try
{
FileWriter fos = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fos);
String[] buf = string.split("\n");
for (int i = 0; i < buf.length; i++)
{
bw.write(buf + "\r\n");
}
bw.close();
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
} catch (IOException ex)
{
ex.printStackTrace();
}
}
else
{
System.exit(0);
}
}
}
}
}