2016-01-04 40 views
0

是否有人可以告訴我如何添加一些東西,以便每個區域都有滾動條?java GroupLayout滾動條

我想讓我的文本區域滾動條,但我失敗了一遍!

我創建GroupLayout,你可以看到...

import java.awt.*; 
import java.awt.event.*; 
import java.util.Date; 
import javax.swing.*; 


import java.net.*; 
import java.io.IOException; 

public class GUIriks extends JFrame implements ActionListener{ 

    JButton poga1 = new JButton("Poga 1"); //Definejam gui komponentes 
    JTextArea area1 = new JTextArea(30,20); //Definejam gui komponentes 
    JButton poga2 = new JButton("Poga 2"); //Definejam gui komponentes 
    JTextArea area2 = new JTextArea(30,20); //Definejam gui komponentes 
    JButton poga3 = new JButton("Poga 3"); //Definejam gui komponentes 
    JTextArea area3 = new JTextArea(30,20); //Definejam gui komponentes 
    JButton poga4 = new JButton("Poga 4"); //Definejam gui komponentes 
    JTextArea area4 = new JTextArea(30,20); //Definejam gui komponentes 



     public GUIriks(){ 
     super("GUI Rīks"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     Container content = getContentPane(); 

      content.setBackground(Color.gray); 

      GroupLayout layout = new GroupLayout(getContentPane()); 
      getContentPane().setLayout(layout); 
      layout.setAutoCreateGaps(true); 
      layout.setAutoCreateContainerGaps(true); 

      content.add(poga1); 
      content.add(area1); 
      content.add(poga2); 
      content.add(area2); 
      content.add(poga3); 
      content.add(area3); 
      content.add(poga4); 
      content.add(area4); 


      JScrollPane scrollPane1 = new JScrollPane(area1); 
      JScrollPane scrollPane2 = new JScrollPane(area2); 
      JScrollPane scrollPane3 = new JScrollPane(area3); 
      JScrollPane scrollPane4 = new JScrollPane(area4); 
      content.add(scrollPane1, BorderLayout.WEST); 
      content.add(scrollPane2, BorderLayout.NORTH); 
      content.add(scrollPane3, BorderLayout.EAST); 
      content.add(scrollPane4, BorderLayout.EAST); 

      poga1.addActionListener(this); 
      poga2.addActionListener(this); 
      poga3.addActionListener(this); 
      poga4.addActionListener(this); 


      poga1.setBackground(new Color(175,238,238)); 
      poga2.setBackground(new Color(255,182,193)); 
      poga3.setBackground(new Color(211,211,211)); 
      poga4.setBackground(new Color(152,251,152)); 

      layout.setHorizontalGroup(layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
              .addComponent(poga1) 
              .addComponent(poga2) 
              .addComponent(poga3) 
              .addComponent(poga4)) 
         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
              .addComponent(area1) 
              .addComponent(area2) 
              .addComponent(area3) 
              .addComponent(area4)) 
    ); 

    // Vertically, we want to align each label with his textfield 
    // on the baseline of the components 
    layout.setVerticalGroup(layout.createSequentialGroup() 
         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
             .addComponent(poga1) 
             .addComponent(area1)) 
         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
             .addComponent(poga2) 
             .addComponent(area2)) 
         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
             .addComponent(poga3) 
             .addComponent(area3)) 
         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
             .addComponent(poga4) 
             .addComponent(area4)) 
    ); 

      setSize(1000,400); 
      setVisible(true); 

     } 

     public void actionPerformed(ActionEvent a) { 

      if (a.getSource() == poga1){ 

       this.metode1();} 
      else if (a.getSource() == poga2){ 

       this.metode2();} 
      else if (a.getSource() == poga3){ 

       this.metode3();} 
      else if (a.getSource() == poga4){ 

       this.metode4();} 
     } 

     public void metode1() 

     { 
      try { 
       InetAddress[] adreses = InetAddress.getAllByName("www.delfi.lv"); 
       for (int i = 0; i < adreses.length; i++) { 
        area1.append("Adresse ir:" + adreses[i] + "\n"); 
       } 
       } 
      catch (UnknownHostException ex) { 
       area1.append("Nevaru atrastfind www.delfi.lv"); 
       } 
     } 

     public void metode2() 

     { 
      boolean rootaccess = false; 
      for (int port = 1; port < 1024; port += 100) { 
       try { 
        ServerSocket ss = new ServerSocket(port); 
        // ja tas izdodas... tad... 
        rootaccess = true; 
        ss.close(); 
        break; 
       } 
       catch (IOException ex) { 
       } 
      } 
      int startport = 1; 
      if (!rootaccess) startport = 1024; 
      int stopport = 65535; 
      for (int port = startport; port <= stopport; port++) { 
       try { 
        ServerSocket socket1 = new ServerSocket(port); 
        socket1.close(); 
       } 
       catch (IOException ex) { 
        area2.append("Ports " + port + " ir aiznemts \n"); 
       } 
      } 
      area2.append("Skenesana ir pabeigta!"); 
     } 

     public void metode3() 
     { 
      Date sysDate = new Date(); 
      String dateString = sysDate.toString(); 
      area3.setText("Sodien ir " + dateString); 
      area3.append("\n\n\n"); 
      area3.append("Seit var pievienot izvadu no citam metodem \n"); 
     } 
     public void metode4() 
     { 
      try { 
       InetAddress es = InetAddress.getLocalHost(); 
       area4.append("Mani sauc " + es.getHostName() + "\n"); 
       area4.append("Mana adrese ir " + es.getHostAddress() + "\n"); 
       byte[] adrese = es.getAddress(); 
       for (int i = 0; i < adrese.length; i++) { 
        area4.append(adrese[i] + " "); 
       } 

       } 
      catch (UnknownHostException e) { 
       area4.append("Nevaru noteikt lokalo adresi."); 
       area4.append("Baidos ka tīks nestrādā..."); 
       } 
     } 
     public static void main(String[] args){ 
      new GUIriks(); 
     } 

} 
+1

您設置佈局管理器作爲'GroupLayout'但隨後使用'BorderLayout'限制?就個人而言,我會避免'GroupLayout'這不是真的意味着手工編碼 – MadProgrammer

+0

感謝重播,但也許你或別人可以告訴我?在我的代碼中添加滾動條很難嗎? – Montaa

+1

不,不是,但你需要了解代碼的其餘部分在做什麼。您好像正在使用由表單編輯器生成的代碼,然後嘗試修改它,但實際上並未知道代碼的其餘部分在做什麼 – MadProgrammer

回答

2

所以,這是一種很難知道你在做什麼,但我的猜測是,你正在做,是由一個生成的代碼表單編輯器,並試圖對其進行修改以滿足您的新的需求

但你是根本的問題是,你要添加的JTextArea年代到content面板,在JScrollPane小號包裝他們,但然後將其添加回通過佈局的content窗格......這只是一團糟。

相反,你的構造應該是這樣的......

content.add(poga1); 
//content.add(area1); 
content.add(poga2); 
//content.add(area2); 
content.add(poga3); 
//content.add(area3); 
content.add(poga4); 
//content.add(area4); 

JScrollPane scrollPane1 = new JScrollPane(area1); 
JScrollPane scrollPane2 = new JScrollPane(area2); 
JScrollPane scrollPane3 = new JScrollPane(area3); 
JScrollPane scrollPane4 = new JScrollPane(area4); 
content.add(scrollPane1, BorderLayout.WEST); 
content.add(scrollPane2, BorderLayout.NORTH); 
content.add(scrollPane3, BorderLayout.EAST); 
content.add(scrollPane4, BorderLayout.EAST); 

poga1.addActionListener(this); 
poga2.addActionListener(this); 
poga3.addActionListener(this); 
poga4.addActionListener(this); 

poga1.setBackground(new Color(175, 238, 238)); 
poga2.setBackground(new Color(255, 182, 193)); 
poga3.setBackground(new Color(211, 211, 211)); 
poga4.setBackground(new Color(152, 251, 152)); 

layout.setHorizontalGroup(layout.createSequentialGroup() 
     .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
       .addComponent(poga1) 
       .addComponent(poga2) 
       .addComponent(poga3) 
       .addComponent(poga4)) 
     .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
       .addComponent(scrollPane1) 
       .addComponent(scrollPane2) 
       .addComponent(scrollPane3) 
       .addComponent(scrollPane4)) 
); 

// Vertically, we want to align each label with his textfield 
// on the baseline of the components 
layout.setVerticalGroup(layout.createSequentialGroup() 
     .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
       .addComponent(poga1) 
       .addComponent(scrollPane1)) 
     .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
       .addComponent(poga2) 
       .addComponent(scrollPane2)) 
     .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
       .addComponent(poga3) 
       .addComponent(scrollPane3)) 
     .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
       .addComponent(poga4) 
       .addComponent(scrollPane4)) 
); 
+0

非常感謝MadProgrammer。現在一切都很好! :) – Montaa