jtextarea中添加滚动条问题
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;importjava.io.*;publiccla...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Image extends JFrame{
JMenuBar jb;
JMenu jm1;
JMenuItem ji1;
JMenuItem ji2;
JTextArea ja;
JTextField jt1;
JLabel jl1;
JPanel flowlayout;
JFrame jp;
JScrollPane jsp;
public void setImage(){
jp=new JFrame();
jp.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
ja=new JTextArea();
jsp=new JScrollPane(ja);
jp.add(jsp);
jp.setTitle("查询");
jb=new JMenuBar();
jm1=new JMenu("file");
ji1=new JMenuItem("new(O)",'O');
ji2=new JMenuItem("open(S)",'S');
ji1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
ji2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
ji2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
FileDialog fd=new FileDialog(jp, "打开",FileDialog.LOAD);
fd.setVisible(true);
String strfile=fd.getDirectory()+fd.getFile();
if(strfile!=null){
try{
FileInputStream ab=new FileInputStream(strfile);
byte[] by=new byte[10*1024];
int len=ab.read(by);
ja.append(new String(by,0,len));
ab.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
});
jm1.add(ji1);
jm1.add(ji2);
jb.add(jm1);
jp.setJMenuBar(jb);
ja=new JTextArea();
jsp=new JScrollPane(ja);
jp.setSize(600, 400);
jp.setLocation(100,100);
jp.setResizable(true);
getflowlayout();
jp.setLayout(new BorderLayout());
jp.add(ja,BorderLayout.CENTER);
jp.add(flowlayout,BorderLayout.NORTH);
jp.setVisible(true);
}
public void getflowlayout(){
flowlayout=new JPanel();
flowlayout.setLayout(new FlowLayout());
jl1=new JLabel("姓名");
jt1=new JTextField(10);
flowlayout.add(jl1);
flowlayout.add(jt1);
}
public static void main(String[] args) {
Image a=new Image();
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setImage();
}
}
我的问题是在打开一个文件的时候 面板容量不够的情况下可以拖动滚动条显示所有的内容!不知道怎么加这个jscrollpane 望高手指点! 展开
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Image extends JFrame{
JMenuBar jb;
JMenu jm1;
JMenuItem ji1;
JMenuItem ji2;
JTextArea ja;
JTextField jt1;
JLabel jl1;
JPanel flowlayout;
JFrame jp;
JScrollPane jsp;
public void setImage(){
jp=new JFrame();
jp.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
ja=new JTextArea();
jsp=new JScrollPane(ja);
jp.add(jsp);
jp.setTitle("查询");
jb=new JMenuBar();
jm1=new JMenu("file");
ji1=new JMenuItem("new(O)",'O');
ji2=new JMenuItem("open(S)",'S');
ji1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
ji2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
ji2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
FileDialog fd=new FileDialog(jp, "打开",FileDialog.LOAD);
fd.setVisible(true);
String strfile=fd.getDirectory()+fd.getFile();
if(strfile!=null){
try{
FileInputStream ab=new FileInputStream(strfile);
byte[] by=new byte[10*1024];
int len=ab.read(by);
ja.append(new String(by,0,len));
ab.close();
}
catch(Exception e1){
e1.printStackTrace();
}
}
}
});
jm1.add(ji1);
jm1.add(ji2);
jb.add(jm1);
jp.setJMenuBar(jb);
ja=new JTextArea();
jsp=new JScrollPane(ja);
jp.setSize(600, 400);
jp.setLocation(100,100);
jp.setResizable(true);
getflowlayout();
jp.setLayout(new BorderLayout());
jp.add(ja,BorderLayout.CENTER);
jp.add(flowlayout,BorderLayout.NORTH);
jp.setVisible(true);
}
public void getflowlayout(){
flowlayout=new JPanel();
flowlayout.setLayout(new FlowLayout());
jl1=new JLabel("姓名");
jt1=new JTextField(10);
flowlayout.add(jl1);
flowlayout.add(jt1);
}
public static void main(String[] args) {
Image a=new Image();
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setImage();
}
}
我的问题是在打开一个文件的时候 面板容量不够的情况下可以拖动滚动条显示所有的内容!不知道怎么加这个jscrollpane 望高手指点! 展开
1个回答
展开全部
你的这个程序思想有点错误,第一:你定义的Image类既然继承了JFrame类,为什么还要在new JFrame,而把组件添加到它上呢?直接把组件添加到本类上。还有你new了两次JTextArea和JScrollPane。一次都够了。
为什么没有滚动条,因为你有一处代码是错误的jp.add(ja,BorderLayout.CENTER);
把ja改成jsp即可!
将组件封装到滚动窗格中,而把滚动窗格加到JFrame上才是正确的。
我把你的代码优化了一下,其中/**/和//表示改动的代码.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Image extends JFrame {
JMenuBar jb;
JMenu jm1;
JMenuItem ji1;
JMenuItem ji2;
JTextArea ja;
JTextField jt1;
JLabel jl1;
JPanel flowlayout;
//JFrame jp;
JScrollPane jsp;
public void setImage() {
//jp = new JFrame();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//ja = new JTextArea();
//jsp = new JScrollPane(ja);
//jp.add(jsp);
/*1*/this.setTitle("查询");
jb = new JMenuBar();
jm1 = new JMenu("file");
ji1 = new JMenuItem("new(O)", 'O');
ji2 = new JMenuItem("open(S)", 'S');
ji1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK));
ji2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_MASK));
ji2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*1*/FileDialog fd = new FileDialog(Image.this, "打开", FileDialog.LOAD);
fd.setVisible(true);
String strfile = fd.getDirectory() + fd.getFile();
if (strfile != null) {
try {
FileInputStream ab = new FileInputStream(strfile);
byte[] by = new byte[10 * 1024];
int len = ab.read(by);
ja.append(new String(by, 0, len));
ab.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
});
jm1.add(ji1);
jm1.add(ji2);
jb.add(jm1);
this.setJMenuBar(jb);
ja = new JTextArea();
jsp = new JScrollPane(ja);
/*1*/this.setSize(600, 400);
/*1*/this.setLocation(100, 100);
/*1*/this.setResizable(true);
getflowlayout();
/*1*///this.add(jsp);
/*1*/this.setLayout(new BorderLayout());
/*1*/this.add(jsp, BorderLayout.CENTER);
/*1*/this.add(flowlayout, BorderLayout.NORTH);
/*1*/this.setVisible(true);
}
public void getflowlayout() {
flowlayout = new JPanel();
flowlayout.setLayout(new FlowLayout());
jl1 = new JLabel("姓名");
jt1 = new JTextField(10);
flowlayout.add(jl1);
flowlayout.add(jt1);
}
public static void main(String[] args) {
Image a = new Image();
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setImage();
}
}
为什么没有滚动条,因为你有一处代码是错误的jp.add(ja,BorderLayout.CENTER);
把ja改成jsp即可!
将组件封装到滚动窗格中,而把滚动窗格加到JFrame上才是正确的。
我把你的代码优化了一下,其中/**/和//表示改动的代码.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Image extends JFrame {
JMenuBar jb;
JMenu jm1;
JMenuItem ji1;
JMenuItem ji2;
JTextArea ja;
JTextField jt1;
JLabel jl1;
JPanel flowlayout;
//JFrame jp;
JScrollPane jsp;
public void setImage() {
//jp = new JFrame();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//ja = new JTextArea();
//jsp = new JScrollPane(ja);
//jp.add(jsp);
/*1*/this.setTitle("查询");
jb = new JMenuBar();
jm1 = new JMenu("file");
ji1 = new JMenuItem("new(O)", 'O');
ji2 = new JMenuItem("open(S)", 'S');
ji1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK));
ji2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_MASK));
ji2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*1*/FileDialog fd = new FileDialog(Image.this, "打开", FileDialog.LOAD);
fd.setVisible(true);
String strfile = fd.getDirectory() + fd.getFile();
if (strfile != null) {
try {
FileInputStream ab = new FileInputStream(strfile);
byte[] by = new byte[10 * 1024];
int len = ab.read(by);
ja.append(new String(by, 0, len));
ab.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
});
jm1.add(ji1);
jm1.add(ji2);
jb.add(jm1);
this.setJMenuBar(jb);
ja = new JTextArea();
jsp = new JScrollPane(ja);
/*1*/this.setSize(600, 400);
/*1*/this.setLocation(100, 100);
/*1*/this.setResizable(true);
getflowlayout();
/*1*///this.add(jsp);
/*1*/this.setLayout(new BorderLayout());
/*1*/this.add(jsp, BorderLayout.CENTER);
/*1*/this.add(flowlayout, BorderLayout.NORTH);
/*1*/this.setVisible(true);
}
public void getflowlayout() {
flowlayout = new JPanel();
flowlayout.setLayout(new FlowLayout());
jl1 = new JLabel("姓名");
jt1 = new JTextField(10);
flowlayout.add(jl1);
flowlayout.add(jt1);
}
public static void main(String[] args) {
Image a = new Image();
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setImage();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询