2013-05-12 151 views
0

嗨,我是最後一年的工程學生,儘管我已經學習了java,但我的應用知識還很簡單。但是,我必須爲我的項目使用Java。這是我寫前端代碼:在AWT中添加一個滾動條/滾動條到框架

import java.awt.*; 
    import java.awt.event.*; 
    import java.applet.*; 
    import java.util.*; 


    public class ex extends Applet 
    { 
     static Frame f; 
     DrawCanvas can; 
     SetControls cont; 

    public void init() 
    { 
      can=new DrawCanvas(); 
      setLayout(new BorderLayout()); 
      add("Center", can); 
      add("North", cont = new SetControls(can, f)); 
    } 

      public void destroy() 
    { 
      remove(cont);  
      remove(can); 
     } 

     public void start() 
    { 
      cont.setEnabled(true); 
     } 

      public void stop() 
    { 
      cont.setEnabled(false); 
     } 

      public void processWindowEvent(AWTEvent e) 
    { 
      if (e.getID() == Event.WINDOW_DESTROY) 
     { 
         System.exit(0); 
     } 
     } 

     public static void main(String args[]) 
    { 
      f = new Frame("Title"); 
      ex fe = new ex(); 

      fe.init(); 
      fe.start(); 

      f.add("Center", fe); 
      f.setSize(1500,1500); 

      f.show(); 
     } 
} 

    class DrawCanvas extends Canvas 
{ 
    //declaration of variables 
    public DrawCanvas() 
    { 
    } 

    public void paint(Graphics g) 
    { 
     //various flags are set for functions to be called and what shud 
       //be drawn when those functions are called 

    } 
} 


    class SetControls extends Panel implements ActionListener 
{ 
     TextArea text1; 
     TextField txt; 
     Button load, init1, cluster,exit; 
     Label head, vig; 

      //declaration of variables used 

      FileDialog fc; 
     String fname; 
     Frame f; 
     DrawCanvas canvas; 

     public SetControls(DrawCanvas can, Frame f) 
    { 
      //initialization of variables used 

      canvas = can; 
      this.f = f; 

      head = new Label("Clustering of Genes:"); 
      cluster = new Button("Cluster Now"); 
      load = new Button("Load"); 
      init1 = new Button("Filter"); 
      exit = new Button("Exit"); 
      txt = new TextField(2); 
      text1 = new TextArea(5,80); 
      vig = new Label("Parameter"); 


      load.addActionListener(this); 
      init1.addActionListener(this); 
       txt.addActionListener(this); 
       cluster.addActionListener(this); 
      exit.addActionListener(this); 


      add(head); 
      add(load); 
      add(init1); 
      add(text1); 
      add(vig); 
      add(txt); 
      add(cluster); 
      add(exit); 

    } 

public void actionPerformed(ActionEvent ae) 
    { 
    String str = ae.getActionCommand(); 
    System.out.println("Command: " + str); 

    if(str.equals("Load")) 
     { 
     text1.setText("Loading microarray data file..."); 

     fc = new FileDialog(f, "Choose a file", FileDialog.LOAD); 
     fc.setDirectory("."); 
     fc.setVisible(true); 
     fname = fc.getFile(); 
     //function call to read the file  
     text1.setText("Input Data File Loaded: " + fname);  
     } 
    else if((str.equals("Filter")||str.equals("Cluster Now"))) 
     { 
     if(str.equals("Filter")) 
      { 
      //function calls and setting of text are 
      } 
     else 
      { 
      //function calls and setting of text area 
       } 

     } 


    else{ 
     //call close function when exit is pressed 
     }   
    } 
} 

我知道你在想,爲什麼我使用AWT,而不是搖擺但此刻我更舒服AWT。到目前爲止,代碼運行良好。但問題是,當我按下其中一個按鈕時,顯示的信息很多,超出了框架;所以我想添加一個滾動條或一個滾動窗格,以便我可以向下滾動查看其餘的信息。我已經嘗試了很多東西,如f.add(新的ScrollPane())等,但他們都沒有工作。請幫幫我!

+0

檢查了這一點http://www.coderanch.com/t/533808/GUI/java/add-Scrollbar-Frame-AWT – 2013-05-12 16:17:19

+0

這是如何添加一些東西到中心或南方,而不是一個字符串:http://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#add%28java.awt.Component ,%20java.lang.Object%29 – 2013-05-12 16:19:58

+0

1)請在句子開頭添加大寫字母。還要爲單詞I使用大寫字母,並使用JEE或WAR等縮寫詞和首字母縮略詞。這使人們更容易理解和幫助。 2)爲什麼編寫一個小程序?如果由於規格而到期。由老師,請參考[爲什麼CS老師應該停止教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)。 3)使用Swing而不是AWT。在[Swing extras over AWT]上看到這個答案(http://stackoverflow.com/a/6255978/418556)有很多很好的理由放棄使用AWT組件。 – 2013-05-12 16:25:30

回答

2
import java.awt.FlowLayout; 
import java.awt.Frame; 
import java.awt.Label; 
import java.awt.Scrollbar; 

public class AWTScrollbar { 

public static void main(String[] args) { 

    Frame frame = new Frame("Scrollbar"); 

    FlowLayout layout = new FlowLayout(); 

    frame.setLayout(layout); 

    Label label = new Label("Scrollbar"); 

    frame.add(label); 

    Scrollbar scrollbar = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255); 

    frame.add(scrollbar); 

    frame.setVisible(true); 

    frame.pack(); 

} 

} 

可能,這將是有益的你:)

+0

嗨謝謝你的答案,但不會影響其他框架? – ppra 2013-05-12 16:30:04

+0

它只會影響{frame}對象,如上例所示。 – 2013-05-12 16:33:43