当前位置: 代码迷 >> J2SE >> jscrollpane显示不出来滚动轴
  详细解决方案

jscrollpane显示不出来滚动轴

热度:910   发布时间:2013-02-25 00:00:00.0
【求助】jscrollpane显示不出来滚动轴
import java.awt.*;
import javax.swing.*;
public class OpenFile {
private JFrame f = new JFrame();
private static final int WIDTH = 600 ,HEIGHT = 400;
private JButton open;
private JScrollPane scrollPane;
private JLabel label;
private JPanel northPanel,southPanel;
public void init(){
JTextField ta = new JTextField(20);
open = new JButton("open");
northPanel = new JPanel();
southPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
northPanel.add(ta);
northPanel.add(open);
label = new JLabel();
label.setIcon(new ImageIcon("img/1.jpg"));//图片部分,1.jpg较大只显示了一部分出来,没有滚动轴
scrollPane = new JScrollPane(label);
southPanel.add(scrollPane);
f.add(northPanel,BorderLayout.NORTH);
f.add(southPanel,BorderLayout.SOUTH);
f.setTitle("查看图片");
f.setSize(WIDTH,HEIGHT);
f.setVisible(true);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[]args){
new OpenFile().init();
}
}

------解决方案--------------------------------------------------------
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class T extends JFrame{

private JTextArea jta;
private JScrollPane jsp;

public static void main(String[] args) {
new T();
}

public T(){

this.setLayout(null);
jta = new JTextArea();
jsp = new JScrollPane(jta);
jsp.setBounds(10, 10, 200, 200);
this.add(jsp);

this.setSize(400, 400);
this.setLocation(450, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

------解决方案--------------------------------------------------------
稍微做了点修改,应该能实现你需要的功能。
Java code
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class OpenFile extends JFrame {    private static final int WIDTH = 600 ,HEIGHT = 400;    private JButton open;    private JPanel northPanel;public OpenFile() {        JTextField ta = new JTextField(20);        open = new JButton("open");        northPanel = new JPanel();          northPanel.add(ta);        northPanel.add(open);         final ImageIcon i = new ImageIcon("images/3.jpg");        final JLabel l = new JLabel(i);        JScrollPane sp = new JScrollPane(l);        add(sp,BorderLayout.CENTER);        add(northPanel,BorderLayout.NORTH);        setSize(WIDTH,HEIGHT);        setVisible(true);        setTitle("查看图片");        setLocationRelativeTo(null);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }        public static void main (String[] args) {        OpenFile t=new OpenFile();    }}