我在面板上加了一个JScrollPane对象,为什么就在文本域中不能显示出来的,究竟要怎么把它加进去啊?请教各位
----------------解决方案--------------------------------------------------------
能把代码发上来了 看看
----------------解决方案--------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
public class Grad extends JFrame
{
JSplitPane jSplitPane1=new JSplitPane();
JScrollPane jScrollPane1=new JScrollPane();
WelcomePane welcomePane1=new WelcomePane();
public Grad()
{
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
setTitle("JScrollPane");
setBounds(100,50,800,600);
setResizable(false);
getContentPane().add(jSplitPane1);
jSplitPane1.setLeftComponent(jScrollPane1);
jSplitPane1.setRightComponent(welcomePane1);
jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
setVisible(true);
}
public static void main(String args[])
{
new Grad();
}
}
class WelcomePane extends JPanel
{
JLabel la;
WelcomePane()
{
la=new JLabel("欢迎使用JScrollPane-------Welcome!");
la.setBounds(8,5,100,20);
add(la);
}
}
先看看吧 再对比你的程序 多看看API
----------------解决方案--------------------------------------------------------
多谢了
----------------解决方案--------------------------------------------------------
这是我以前写的,希望对你有用
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class MyFileChooser
{
public static void main(String [] args)
{
FileFrame f=new FileFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(true);
}
}
class FileFrame extends JFrame
{
public FileFrame()
{
text=new JTextArea();
text.setEditable(false);
text.setWrapStyleWord(true);
text.setLineWrap(true);
scrollPane=new JScrollPane(text);
setTitle("file");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
Container con=getContentPane();
con.add(scrollPane);
chooser=new JFileChooser();
chooser.setCurrentDirectory(new File("."));
JMenuBar menubar=new JMenuBar();
setJMenuBar(menubar);
JMenu menu=new JMenu("file");
menubar.add(menu);
JMenuItem open=new JMenuItem("Open");
menu.add(open);
JMenuItem exit=new JMenuItem("Exit");
menu.add(exit);
open.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int r=chooser.showOpenDialog(null);
if(r==JFileChooser.APPROVE_OPTION)
{
String name=chooser.getSelectedFile().getPath();
try
{
infile=new BufferedReader(new FileReader(name));
text.setText("");
String line;
while((line=infile.readLine())!=null)
{
text.append(line+"\n");
}
}catch(Exception e1){}
}
}
});
exit.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
private JTextArea text;
private JScrollPane scrollPane;
private BufferedReader infile;
private JFileChooser chooser;
private static final int DEFAULT_WIDTH=1000;
private static final int DEFAULT_HEIGHT=600;
}
----------------解决方案--------------------------------------------------------