各位大大晚安
事情是這樣的~
用JAVA作一個記事本基本上就是個國民題~
似乎每個人都會練習到...
但是我現在卡在edit新增一個讓使用者選字型的形式+尺寸+顏色的地方
下面是我目前研究到的程式碼~
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyFrame10151 extends JFrame {
//global variables
private JTextArea txt=null;
//constructors
public MyFrame10151(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1000,500);
getContentPane().setBackground(Color.pink);
//new Button
txt=new JTextArea();
getContentPane().add(txt);
add(new JScrollPane(txt), BorderLayout.CENTER);
menu();
}
//methods
private void menu() {
//menu bar
JMenuBar mbar=new JMenuBar();
JMenu mFile=new JMenu("File");
mFile.setMnemonic(KeyEvent.VK_F);
mbar.add(mFile);
JMenuItem iNew=new JMenuItem("New");
mFile.add(iNew);
iNew.setMnemonic(KeyEvent.VK_N);
iNew.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
clear();
}
}
);
JMenuItem iOpen=new JMenuItem("Open");
mFile.add(iOpen);
iOpen.setMnemonic(KeyEvent.VK_O);
iNew.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
}
);
JMenuItem iSave=new JMenuItem("Save");
mFile.add(iSave);
iSave.setMnemonic(KeyEvent.VK_S);
iSave.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
save();
}
}
);
mFile.addSeparator();
JMenuItem iExit=new JMenuItem("Exit");
iExit.setMnemonic(KeyEvent.VK_X);
iExit.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
);
mFile.add(iExit);
this.setJMenuBar(mbar);
}
private void clear() {
int answer=JOptionPane.showConfirmDialog(null,
"Are you sure?", "Clear", JOptionPane.YES_NO_OPTION);
System.out.println(answer);
if(answer==JOptionPane.YES_OPTION) txt.setText("");
}
private void save() {
JFileChooser fc=new JFileChooser();
fc.setCurrentDirectory(new File("c:/"));
int answer = fc.showSaveDialog(this);
if(answer==JFileChooser.APPROVE_OPTION) {
System.out.println(fc.getSelectedFile());
try {
PrintWriter bw=new PrintWriter(new FileWriter(fc.getSelectedFile()));
bw.write(txt.getText());
bw.close();
}catch(Exception e) {
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
MyFrame10151 win1=new MyFrame10151("My NotePad");
win1.setVisible(true);
//System.out.println(win1.txt);
}
}
我真的有搜尋很多~
https://www.youtube.com/watch?v=HY8jYkiBPU8
https://www.logicbig.com/tutorials/java-swing/menu-search-highlighting.html
https://docs.oracle.com/javase/tutorial/uiswing/components/menu.html
https://blog.csdn.net/Peng_Hong_fu/article/details/52682179
https://codertw.com/程式語言/308106/
http://hk.uwenku.com/question/p-gafotmps-da.html
https://github.com/JustinSDK/JavaSE6Tutorial/blob/master/docs/CH19.md
但是還是不知道該如何下手
可以提示我一下嗎....QQ