java无法显示滚动条
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.io.*;publiccla...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class MenuFrame extends JFrame
{JMenuBar mnuNotePad=new JMenuBar();
JMenu mnuFile=new JMenu("文件");
JMenu mnuEdit=new JMenu("编辑");
JMenu mnuFormat=new JMenu("格式");
JMenu mnuCheck=new JMenu("查看");
JMenu mnuHelp=new JMenu("帮助");
JPopupMenu popupMenu=new JPopupMenu();
JMenuItem mnuNew=new JMenuItem("新建");
JMenuItem mnuOpen=new JMenuItem("打开");
JMenuItem mnuSave=new JMenuItem("保存");
JMenuItem mnuExit=new JMenuItem("退出");
JMenuItem mnuCopy=new JMenuItem("复制");
JMenuItem mnuCut=new JMenuItem("剪切");
JTextArea jTextArea1=new JTextArea();
public void init()
{setSize(new Dimension(400,300));
setTitle("文本框");
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setJMenuBar(mnuNotePad);
mnuNotePad.add(mnuFile);
mnuNotePad.add(mnuEdit);
mnuNotePad.add(mnuFormat);
mnuNotePad.add(mnuCheck);
mnuNotePad.add(mnuHelp);
mnuFile.add(mnuNew);
mnuFile.add(mnuOpen);
mnuFile.add(mnuSave);
mnuFile.add(mnuExit);
add(jTextArea1);
jTextArea1.setLineWrap(true);
JScrollPane jsp=new JScrollPane(jTextArea1);
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
add(jsp);
mnuNew.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
jTextArea1.setText("");
}
});
mnuOpen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try
{
openText();
}
catch (IOException e1)
{
}
}
});
mnuSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try
{
saveText();
}
catch (IOException e2)
{
}
}
});
mnuExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(-1);
}
});
setLayout(new BorderLayout());
popupMenu.add(mnuCopy);
popupMenu.add(mnuCut);
jTextArea1.setComponentPopupMenu(popupMenu);
this.add(jTextArea1,java.awt.BorderLayout.CENTER);
}
public void openText() throws IOException
{FileDialog fd=new FileDialog(this,"打开文件对话框",FileDialog.LOAD);
fd.setVisible(true);
FileInputStream fis=new FileInputStream(fd.getDirectory()+fd.getFile());
jTextArea1.setText("");
int n=0;
while((n=fis.read())!=-1)
jTextArea1.append(""+(char)n);
fis.close();
}
public void saveText() throws IOException
{FileDialog fd=new FileDialog(this,"打开文件对话框",FileDialog.SAVE);
fd.setVisible(true);
FileOutputStream fos=new FileOutputStream(fd.getDirectory()+fd.getFile()+".txt");
String str=jTextArea1.getText();
for (int n=0;n<str.length() ;n++ )
{fos.write(str.charAt(n));
}
fos.close();
} 展开
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class MenuFrame extends JFrame
{JMenuBar mnuNotePad=new JMenuBar();
JMenu mnuFile=new JMenu("文件");
JMenu mnuEdit=new JMenu("编辑");
JMenu mnuFormat=new JMenu("格式");
JMenu mnuCheck=new JMenu("查看");
JMenu mnuHelp=new JMenu("帮助");
JPopupMenu popupMenu=new JPopupMenu();
JMenuItem mnuNew=new JMenuItem("新建");
JMenuItem mnuOpen=new JMenuItem("打开");
JMenuItem mnuSave=new JMenuItem("保存");
JMenuItem mnuExit=new JMenuItem("退出");
JMenuItem mnuCopy=new JMenuItem("复制");
JMenuItem mnuCut=new JMenuItem("剪切");
JTextArea jTextArea1=new JTextArea();
public void init()
{setSize(new Dimension(400,300));
setTitle("文本框");
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setJMenuBar(mnuNotePad);
mnuNotePad.add(mnuFile);
mnuNotePad.add(mnuEdit);
mnuNotePad.add(mnuFormat);
mnuNotePad.add(mnuCheck);
mnuNotePad.add(mnuHelp);
mnuFile.add(mnuNew);
mnuFile.add(mnuOpen);
mnuFile.add(mnuSave);
mnuFile.add(mnuExit);
add(jTextArea1);
jTextArea1.setLineWrap(true);
JScrollPane jsp=new JScrollPane(jTextArea1);
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
add(jsp);
mnuNew.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
jTextArea1.setText("");
}
});
mnuOpen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try
{
openText();
}
catch (IOException e1)
{
}
}
});
mnuSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try
{
saveText();
}
catch (IOException e2)
{
}
}
});
mnuExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(-1);
}
});
setLayout(new BorderLayout());
popupMenu.add(mnuCopy);
popupMenu.add(mnuCut);
jTextArea1.setComponentPopupMenu(popupMenu);
this.add(jTextArea1,java.awt.BorderLayout.CENTER);
}
public void openText() throws IOException
{FileDialog fd=new FileDialog(this,"打开文件对话框",FileDialog.LOAD);
fd.setVisible(true);
FileInputStream fis=new FileInputStream(fd.getDirectory()+fd.getFile());
jTextArea1.setText("");
int n=0;
while((n=fis.read())!=-1)
jTextArea1.append(""+(char)n);
fis.close();
}
public void saveText() throws IOException
{FileDialog fd=new FileDialog(this,"打开文件对话框",FileDialog.SAVE);
fd.setVisible(true);
FileOutputStream fos=new FileOutputStream(fd.getDirectory()+fd.getFile()+".txt");
String str=jTextArea1.getText();
for (int n=0;n<str.length() ;n++ )
{fos.write(str.charAt(n));
}
fos.close();
} 展开
若以下回答无法解决问题,邀请你更新回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询