帮我解答一道java题,谢谢
importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassMenuTestextend...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MenuTest extends JFrame{
JTextArea theArea;
public MenuTest(){
super("MyMenu");
Container contentPane = this.getContentPane();
theArea = new JTextArea( );
theArea.setText("The first snow came,\nHow beautiful it was!");
theArea.setLineWrap(true);
contentPane.add(new JScrollPane(theArea),BorderLayout.CENTER);
JMenu fontMenu = new JMenu("Font");
JMenuItem fontSize= new JMenuItem("Size");
fontSize.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String sizeString=JOptionPane.showInputDialog( "字号大小" );
int fontSize=Integer.parseInt(sizeString);
theArea.setFont(
new Font("楷体_gb2312",Font.PLAIN,fontSize));
}
});
fontMenu.add(fontSize);
JMenuItem exit= new JMenuItem("Exit");
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fontMenu.add(exit);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fontMenu);
this.setSize(300,200);
this.setVisible(true);
}
public static void main(String[] args){
new MenuTest();
}
}
2.用文字描述点击菜单项“Size”后,程序的执行过程和效果。 展开
import java.awt.*;
import java.awt.event.*;
public class MenuTest extends JFrame{
JTextArea theArea;
public MenuTest(){
super("MyMenu");
Container contentPane = this.getContentPane();
theArea = new JTextArea( );
theArea.setText("The first snow came,\nHow beautiful it was!");
theArea.setLineWrap(true);
contentPane.add(new JScrollPane(theArea),BorderLayout.CENTER);
JMenu fontMenu = new JMenu("Font");
JMenuItem fontSize= new JMenuItem("Size");
fontSize.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String sizeString=JOptionPane.showInputDialog( "字号大小" );
int fontSize=Integer.parseInt(sizeString);
theArea.setFont(
new Font("楷体_gb2312",Font.PLAIN,fontSize));
}
});
fontMenu.add(fontSize);
JMenuItem exit= new JMenuItem("Exit");
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fontMenu.add(exit);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuBar.add(fontMenu);
this.setSize(300,200);
this.setVisible(true);
}
public static void main(String[] args){
new MenuTest();
}
}
2.用文字描述点击菜单项“Size”后,程序的执行过程和效果。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询