2 zxd 8 zxd_8 于 2017.01.10 10:22 提问

把JPanel添加到JScrollPane

package com.frame;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
//import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

@SuppressWarnings("serial")
public class testjm1 extends JFrame implements ActionListener{
JButton btnquit,btnsubmit;
JLabel labtitle,lab1[][],lab2[][],lab3[][];
JTextField text1[][],text2[][];
JTextArea txtarea;
JScrollPane Jspane;
int i,j,k;
JPanel pp[][],ppp;
double cbmatrix1[][];//成本转换邻接矩阵1
double cbmatrix2[][];//成本转换邻接矩阵2
int num;
public double[][] getCbmatrix1() {
return cbmatrix1;
}

public void setCbmatrix1(double[][] cbmatrix1) {
    this.cbmatrix1 = cbmatrix1;
}

public double[][] getCbmatrix2() {
    return cbmatrix2;
}

public void setCbmatrix2(double[][] cbmatrix2) {
    this.cbmatrix2 = cbmatrix2;
}
public void init(){
    Jspane = new JScrollPane();
    //分别设置水平和垂直滚动条总是出现
    Jspane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    Jspane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    //分别设置水平和垂直滚动条自动出现
    // Jspane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    // Jspane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    }
public testjm1(){
    super("图中各节点的成本邻接矩阵信息采集");
    setVisible(true);
    this.setFont(new   java.awt.Font("Dialog",1,15));       
    num=8;
    ppp=new JPanel();       
    pp=new JPanel[num+1][num+1];
    lab1=new JLabel[num+1][num+1];
    lab2=new JLabel[num+1][num+1];
    lab3=new JLabel[num+1][num+1];
    text1=new JTextField[num+1][num+1];
    text2=new JTextField[num+1][num+1];
    cbmatrix1=new double[num+1][num+1];
    cbmatrix2=new double[num+1][num+1];
    btnquit=new JButton("退出");
    btnsubmit=new JButton("提交");
    labtitle=new JLabel("请输入图中各节点之间的权重区间:");
    txtarea=new JTextArea();
    setLayout(null);
    //setSize(300 + num * 200, 300 + num * 200);
    setSize(800,700);
    //ppp.setBounds(0, 0,300 + num * 200,300 + num * 200);
    ppp.setBounds(0,0,650,650);
    ppp.setLayout(null);
    this.add(ppp);
    ppp.add(txtarea);
    ppp.add(labtitle);
    ppp.add(btnquit);
    ppp.add(btnsubmit);
    labtitle.setBounds(30, 50, 280, 30);
    btnsubmit.setBounds(320, 50, 80, 30);
    JScrollPane ScrollPane = new JScrollPane(ppp);      
    ScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
    ScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

     //ScrollPane.setBounds(0, 0,300 + num * 200,300 + num * 200);
    ScrollPane.setBounds(0,0,650,650);
     this.getContentPane().add(ScrollPane);

     this.setVisible(true);
    btnsubmit.addActionListener(this);
    for (i = 0; i <= num; i++) {
        Component heng = add(new Label("节点" + i));
            ppp.add(heng);
          heng.setBounds(150 + i * 180, 100 , 50, 30);
        Component shu = add(new Label("节点" + i));
        ppp.add(shu);
        shu.setBounds(30, 145 + i * 50, 50, 30);// 120>130
    }
    for (i = 0; i <= num; i++) {
        for (j = 0; j <= num; j++) {
            k=0;
            lab1[i][j]=new JLabel("[");
            lab1[i][j].setSize(5, 25);
            lab2[i][j]=new JLabel(",");
            lab2[i][j].setSize(5, 25);
            lab3[i][j]=new JLabel("]");
            lab3[i][j].setSize(5, 25);
            if (i == j) {   
                text1[i][j]=new JTextField("0",2);                  
                text2[i][j]=new JTextField("0",2);
                pp[i][j]=new JPanel();
                ppp.add(pp[i][j]);
                pp[i][j].add(lab1[i][j]);
                pp[i][j].add(text1[i][j]);
                pp[i][j].add(lab2[i][j]);
                pp[i][j].add(text2[i][j]);
                pp[i][j].add(lab3[i][j]);

                pp[i][j].setBounds(100 + j * 180, 140 + i * 50, 170, 25);
            } else {
                text1[i][j]=new JTextField(2);
                text2[i][j]=new JTextField(2);              
                pp[i][j]=new JPanel();
                ppp.add(pp[i][j]);
                pp[i][j].add(lab1[i][j]);
                pp[i][j].add(text1[i][j]);
                pp[i][j].add(lab2[i][j]);
                pp[i][j].add(text2[i][j]);
                pp[i][j].add(lab3[i][j]);
                pp[i][j].setBounds(100 + j * 180, 140 + i * 50, 170, 25);               
            }

        }
    }

}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==btnsubmit){
        //System.out.println("########");
    for(int i=0;i<=num;i++)
        for(int j=0;j<=num;j++){

// if(text1[i][j].getText()!=null||text1[i][j].getText()!=""){
// cbmatrix1[i][j]=Integer.parseInt(text1[i][j].getText());
// }
if(text1[i][j].getText().trim().equals("")){
cbmatrix1[i][j]=0;
}
else
cbmatrix1[i][j]=Double.parseDouble(text1[i][j].getText());
if(text2[i][j].getText().trim().equals("")){
cbmatrix2[i][j]=0;
}
else
cbmatrix2[i][j]=Double.parseDouble(text2[i][j].getText());
}
this.dispose();
}
}
public static void main(String[] args) {
testjm1 t=new testjm1();
}
}
把JPanel添加到JScrollPane里面,运行之后JScrollPane没办法显示JPanel中所有组件

1个回答

dabocaiqq
dabocaiqq   2017.01.21 23:08
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!