出于某种原因,我无法向下滚动任何教导?我尝试添加到一个面板,然后面板添加到滚动条但没有任何作用。我需要一些帮助来解决这个问题,我已经存货数小时了。
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.AncestorListener;
public class StartActivity extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -1470898831456146593L;
private JTextField textField;
public JTextField[] textStock = new JTextField[190];
public tags[] tags = new tags[90];
public String[] tg = new String[90];
public JList set = new JList();
private JPanel topPanel;
@SuppressWarnings("resource")
public StartActivity(){
setSize(1800, 900); // create an instance
setBackground(Color.gray);
setFocusable(true);
setLayout(null);
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
getContentPane().add(new JScrollPane());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//FileReader file = null; // get the tags and their name
//FileReader file2 = null;
InputStreamReader file;
InputStream is = getClass().getResourceAsStream("/resource/tagsName.txt");
InputStream is2 = getClass().getResourceAsStream("/resource/ref.txt");
file = new InputStreamReader(is);
InputStreamReader file2 = new InputStreamReader(is2);
//file = new FileReader("src/resource/tagsName.txt");
BufferedReader br = new BufferedReader(file);
try {
file2 = new FileReader("src/resource/ref.txt");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader br2 = new BufferedReader(file2);
// textField = new JTextField();
// textField.setBounds(35, 136, 86, 20);
// add(textField);
// textField.setColumns(10);
int x = 35;
int y= 136;
for (int i=0 ; i < 190 ; i++){ // add the text field to enter the quote
textStock[i] = new JTextField("");
textStock[i].setBounds(x,y,86,20);
getContentPane().add(textStock[i]);
textStock[i].setColumns(10);
y = y + 25;
if (y > 900){
y = 136;
x = x + 91;
}
}
JLabel label1 = new JLabel("Enter Stock Symbols");
label1.setBounds((x-35)/2, 100, 200, 20);
getContentPane().add(label1);
//addKeyListener(this);
String tg = "";
String rf = "";
x = x + 200;
y= 136;
int i = 0;
int z = x;
try { //
while((tg = br.readLine()) != null){ // add textfields to enter their tags
rf =br2.readLine();
tags[i] = new tags(tg, rf); // tags are a separte method that has their own info
tags[i].setBounds(x, y, 200, 23); // tags are declared as buttons
getContentPane().add(tags[i]);
y = y + 28;
if (y > 900){
y = 136;
x = x + 205;
}
tg = "";
i = i +1;
}
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
br.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JLabel label2 = new JLabel("Choose stock tags Retrival");
label2.setBounds((x+z)/2, 100, 200, 20);
getContentPane().add(label2);
//set = new JList();
JButton list1= new JButton("List 1");
list1.setBounds(35, 100, 89, 23);
list1.addActionListener(this);
getContentPane().add(list1);
JButton clear = new JButton("Clear");
clear.setBounds(130, 100, 89, 23);
clear.addActionListener(this);
getContentPane().add(clear);
JButton clearT = new JButton("Clear Tags");
clearT.setBounds(getWidth()/2, 100, 100, 23);
clearT.addActionListener(this);
getContentPane().add(clearT);
JButton run = new JButton("Run");
run.setBounds(getWidth()/2, 32, 89, 23);
run.addActionListener(this);
getContentPane().add(run);
JButton analyse = new JButton("Analyse");
analyse.setBounds(getWidth()/2 - 200, 32, 89, 23);
analyse.addActionListener(this);
getContentPane().add(analyse);
//Frame frame = (Frame) this.getParent().getParent();
//frame.setTitle(" Financial Analysis");
try {
} catch (Exception e) {
// TODO: handle exception
}
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
/**
* call method depending on their button
*/
System.out.println(arg0.getActionCommand());
if (arg0.getActionCommand()== "Run"){
stockdata();
}
//System.out.println("true");
if (arg0.getActionCommand()== "List 1"){
listSet(1);
}
if (arg0.getActionCommand()== "Analyse"){
choose();
System.out.println(arg0.getActionCommand());
}
if (arg0.getActionCommand()== "Clear"){
clear();
System.out.println(arg0.getActionCommand());
}
if (arg0.getActionCommand()== "Clear Tags"){
clearT();
System.out.println(arg0.getActionCommand());
}
}
private void clearT() {
// TODO Auto-generated method stub
for (int i=0 ; i < 190 ; i++){
try{
tags[i].setState(false);
tags[i].setStatus(false);
}catch(Exception e){
}
}
}
private void clear() {
// TODO Auto-generated method stub
for (int i=0 ; i < 190 ; i++){
textStock[i].setText("");
}
}
private void choose() {
/*
* analyse quotes with parameters that I choose
*/
int m =0;
String[] quotes = new String[200];
int s =0;
String[] tq = new String[20];
String qurl = "http://finance.yahoo.com/d/quotes.csv?s=";
for (int i=0 ; i < 60 ; i++){
String stock =textStock[i].getText();
if (m == 0 && stock != ""){
qurl = qurl + stock ;
m =1 ;
quotes[s] = stock;
s=s+1;
}
else if(stock != ""){
qurl = qurl +"+"+ stock ;
quotes[s] = stock;
s=s+1;
}
}
qurl = qurl + "&f=l1vhgd1rp6";
System.out.println(qurl);
Analysis tab = new Analysis(qurl, quotes); // param are declared in another method
tab.setVisible(true);
}
private void stockdata() {
/*
* retrieves stock data
*/
int m =0;
String[] tq = new String[20];
String[] quotes = new String[200];
int s =0;
String qurl = "http://finance.yahoo.com/d/quotes.csv?s=";
for (int i=0 ; i < 60 ; i++){
String stock =textStock[i].getText();
if (m == 0 && stock != ""){
qurl = qurl + stock ;
quotes[s] = stock;
s=s+1;
m =1 ;
}
else if(stock != ""){
qurl = qurl +"+"+ stock ;
quotes[s] = stock;
//System.out.println(quotes[s]);
s=s+1;
}
}
qurl = qurl + "&f=";
int z =0;
for (int i=0 ; i < 60 ; i++){
if(tags[i].getStatus()){
qurl = qurl + tags[i].getReference();
tq[z]= tags[i].getName();
z =z+1;
}
}
System.out.println(qurl);
// this method retrives data and creates a table to show them
StockData tab = new StockData(qurl, tq, quotes);
tab.setVisible(true);
}
private void listSet(int fileNumber){
// this method populates a set of stocks
InputStream is = getClass().getResourceAsStream("/resource/set"+fileNumber + ".txt");
InputStreamReader file = new InputStreamReader(is);
BufferedReader br = new BufferedReader(file);
String tg;
int i = 0;
try {
while((tg = br.readLine()) != null){
if (i<190){
textStock[i].setText(tg);
i = i +1;
}}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main( String args[] )
{
StartActivity mainFrame = new StartActivity();
mainFrame.setVisible( true );
}
}
答案 0 :(得分:1)
所以我认为这两行代码就是问题......
getContentPane().add( topPanel );
getContentPane().add(new JScrollPane());
如果我没有弄错,你需要将面板添加到JScrollPane,然后将JScrollPane添加到框架中。所以做一些像
这样的事情 getContentPane().add(new JScrollPane(topPanel));
我认为这就足够了。
答案 1 :(得分:0)
我认为你想在面板中添加一些组件,并将面板放在滚动窗格中。
在这种情况下,您确实需要将组件放在面板中:
topPanel.add(label1);
topPanel.add(label2);
topPanel.add(list1);
//... and so on
以及滚动窗格中的面板:
getContentPane().add(new JScrollPane(topPanel));