【发布时间】:2014-01-16 21:51:57
【问题描述】:
我和一个朋友正在制作一个记事本。当您单击相应的按钮时,我们正在尝试更改字体。例如,如果我按下斜体按钮,之后我写的文本将是斜体,直到我按下正常按钮,这会将其转回正常字体。据我所知,代码看起来不错,但是当我按下字体按钮时,什么也没有发生。这是所有代码。
package com.note.pad;
import javax.swing.*; // for the main JFrame design
import java.awt.*; // for the GUI stuff
import java.awt.event.*; // for the event handling
import java.util.Scanner; // for reading from a file
import java.io.*; // for writing to a file
public class Notepad extends JFrame implements ActionListener{
private TextArea textArea = new TextArea("", 0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
private MenuBar menuBar = new MenuBar(); //First, create a menu bar.
private Menu file = new Menu(); //File menu.
private Menu fonts = new Menu(); //Font menu.
private MenuItem italicFont = new MenuItem();
private MenuItem normalFont = new MenuItem();
private MenuItem openFile = new MenuItem(); //Open option
private MenuItem saveFile = new MenuItem(); //Save option
private MenuItem close = new MenuItem(); //Close option
public Notepad(){
this.setSize(500, 300); //Size of Notepad
this.setTitle("BenPad"); //Name of Notepad
setDefaultCloseOperation(EXIT_ON_CLOSE); //Closes when you hit X button
this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12)); //Font
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(textArea);
this.setMenuBar(this.menuBar);
this.menuBar.add(this.file);
this.menuBar.add(this.fonts);
this.file.setLabel("File");
this.fonts.setLabel("Fonts");
this.italicFont.setLabel("Italic");
this.normalFont.setLabel("Normal");
this.fonts.add(this.italicFont);
this.openFile.setLabel("Open");
this.fonts.add(this.normalFont);
this.openFile.addActionListener(this);
this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_0, false));
this.file.add(this.openFile);
this.saveFile.setLabel("Save");
this.saveFile.addActionListener(this);
this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
this.file.add(this.saveFile);
this.close.setLabel("Close");
this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
this.close.addActionListener(this);
this.file.add(this.close);
}
public void actionPerformed (ActionEvent e) {
if (e.getSource() == this.close)
this.dispose();
else if (e.getSource() == this.openFile) {
JFileChooser open = new JFileChooser();
int option = open.showOpenDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
this.textArea.setText("");
try {
Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
while (scan.hasNext())
this.textArea.append(scan.nextLine() + "\n");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
else if (e.getSource() == this.italicFont) {
this.textArea.setFont(new Font("Italic", Font.ITALIC, 12));
}
else if (e.getSource() == this.normalFont) {
this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12));
}
else if (e.getSource() == this.saveFile) {
JFileChooser save = new JFileChooser();
int option = save.showSaveDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
out.write(this.textArea.getText());
out.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
}
public static void main(String args[]) {
Notepad app = new Notepad();
app.setVisible(true);
}
}
【问题讨论】:
-
不要将 Swing 组件与 AWT 组件混用!另请注意 1) 没有支持样式文本的 AWT 组件。 2)
JTextArea不支持样式文本,但JEditorPane或JTextPane将支持。