2014-01-08 106 views
2

我想在窗口中顯示字符串列表,並且試圖使用由JScrollPane覆蓋的JPanel,因爲字符串列表的大小未知。問題在於窗口正在水平顯示文本,我想在行後顯示。如何解決這個問題?這是我迄今爲止編寫的代碼。JScrollPane僅使用水平滾動條顯示內容

package interface_classes; 

import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.util.ArrayList; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JButton; 
import javax.swing.JTextArea; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import javax.swing.JScrollPane; 
import javax.swing.ScrollPaneConstants; 

public class ErrorMessageW { 

    private JFrame errorMessageW; 
    private ArrayList<String> errors; 
    private JPanel panel; 
    private JScrollPane scrollPane; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     final ArrayList<String> err = new ArrayList<>(); 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        ErrorMessageW window = new ErrorMessageW(err); 
        window.errorMessageW.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public ErrorMessageW(ArrayList<String> err) { 
     errors = err; 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     errorMessageW = new JFrame(); 
     errorMessageW.setTitle("Unfilled forms"); 
     errorMessageW.setBounds(100, 100, 367, 300); 
     errorMessageW.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JButton btnOk = new JButton("OK"); 
     btnOk.setBounds(239, 208, 89, 23); 
     btnOk.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mouseClicked(MouseEvent arg0) { 
       errorMessageW.dispose(); 
      } 
     }); 
     errorMessageW.getContentPane().setLayout(null); 
     errorMessageW.getContentPane().add(btnOk); 

     scrollPane = new JScrollPane(); 
     scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); 
     scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
     scrollPane.setBounds(10, 10, 330, 175); 
     errorMessageW.getContentPane().add(scrollPane); 

     panel = new JPanel(); 
     for(String s : errors){ 
      JTextArea text = new JTextArea(1,20); 
      text.setText(s); 
      text.setFont(new Font("Verdana",1,10)); 
      text.setForeground(Color.RED); 
      panel.add(text); 

     } 
     scrollPane.setViewportView(panel); 
    } 

    public JFrame getErrorMessageW() { 
     return errorMessageW; 
    } 

    public void setErrorMessageW(JFrame errorMessageW) { 
     this.errorMessageW = errorMessageW; 
    } 


} 

這就是我得到 enter image description here

這就是我想要的,但使用JScrollPane的: enter image description here

+2

'errorMessageW.setBounds(100,100,367,300);'Java的圖形用戶界面可能需要在多個平臺上工作,在不同的屏幕分辨率和使用不同的PLAFs。因此,它們不利於組件的準確放置。爲了組織強大的圖形用戶界面,請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[空格]的佈局填充和邊框(http: //stackoverflow.com/q/17874717/418556)。 –

+0

@AndrewThompson用戶只能閱讀錯誤信息,單擊確定並填充表格。 – laura

+0

@AndrewThompson我試過這段代碼'JPanel panel = new JPanel(); \t \t panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS)); \t \t scrollPane.setViewportView(panel); \t \t for(String s:errors){ \t \t \t JLabel text = new JLabel(); \t \t \t text.setFont(new Font(「Verdana」,1,10)); \t \t \t text.setForeground(Color.RED); \t \t \t text.setText(s); \t \t \t panel.add(text); \t \t}',根據Balduz的建議,結果與第一張圖相同。 – laura

回答

2

enter image description here

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 
import java.util.ArrayList; 
import javax.swing.event.ListSelectionEvent; 
import javax.swing.event.ListSelectionListener; 

public class ErrorMessageW { 

    private JFrame errorMessageW; 
    private ArrayList<String> errors; 
    private JPanel panel; 
    private JScrollPane scrollPane; 
    private JTextArea errorMessage = new JTextArea(3, 30); 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     final ArrayList<String> err = new ArrayList<String>(); 
     err.add("Short String"); 
     err.add("A very very very very very very very very very very very " 
       + "very very very very very very very very very very very " 
       + "very very very very very very very very long String"); 
     EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       try { 
        ErrorMessageW window = new ErrorMessageW(err); 
        window.errorMessageW.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public ErrorMessageW(ArrayList<String> err) { 
     errors = err; 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     errorMessageW = new JFrame(); 
     JPanel contentPane = new JPanel(new BorderLayout(5, 15)); 
     contentPane.setBorder(new EmptyBorder(10, 10, 10, 10)); 
     errorMessage.setLineWrap(true); 
     errorMessage.setWrapStyleWord(true); 
     JScrollPane jsp = new JScrollPane(
       errorMessage, 
       JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
       JScrollPane.HORIZONTAL_SCROLLBAR_NEVER 
       ); 
     contentPane.add(jsp, BorderLayout.PAGE_START); 

     errorMessageW.add(contentPane); 
     errorMessageW.setTitle("Unfilled forms"); 
     errorMessageW.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     JButton btnOk = new JButton("OK"); 
     btnOk.addMouseListener(new MouseAdapter() { 

      @Override 
      public void mouseClicked(MouseEvent arg0) { 
       errorMessageW.dispose(); 
      } 
     }); 
     JPanel btnConstrain = new JPanel(new FlowLayout(FlowLayout.TRAILING)); 
     btnConstrain.add(btnOk); 
     contentPane.add(btnConstrain, BorderLayout.PAGE_END); 

     scrollPane = new JScrollPane(); 
     scrollPane.setVerticalScrollBarPolicy(
       ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); 
     scrollPane.setHorizontalScrollBarPolicy(
       ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
     contentPane.add(scrollPane, BorderLayout.CENTER); 

     DefaultListModel<String> listModel = new DefaultListModel<String>(); 
     for (String s : errors) { 
      listModel.addElement(s); 
     } 
     final JList<String> errorList = new JList<String>(listModel); 
     Dimension preferredSize = new Dimension(errorMessage.getPreferredSize().width,200); 
     errorList.setPreferredSize(preferredSize); 
     ListSelectionListener errorSelect = new ListSelectionListener() { 

      @Override 
      public void valueChanged(ListSelectionEvent e) { 
       errorMessage.setText(errorList.getSelectedValue()); 
      } 
     }; 
     errorList.addListSelectionListener(errorSelect); 
     scrollPane.setViewportView(errorList); 
     errorMessageW.pack(); 
    } 

    public JFrame getErrorMessageW() { 
     return errorMessageW; 
    } 

    public void setErrorMessageW(JFrame errorMessageW) { 
     this.errorMessageW = errorMessageW; 
    } 
} 
+0

請注意,代碼確實有一個重大的黑客攻擊,我沒有時間修復,但應警告您。它將'JList'的首選大小設置爲與'JTextArea'相同的首選寬度。請參閱[我應該避免使用Java Swing中的set(Preferred | Maximum | Minimum)Size方法?](http://stackoverflow.com/q/7229226/418556)(是) –

+0

..更好的方法它會覆蓋列表的首選大小,以返回與文本區域返回的寬度相同的寬度。這樣,如果PLAF被更改,列表將自動調整。 –

0

首先,你可以嘗試,而不是創建的JTextArea多個實例, ,只使用一個,並像這樣附加每個錯誤:

JTextArea text = new JTextArea(1, 20); 
    text.setFont(new Font("Verdana",1,10)); 
    text.setForeground(Color.RED); 
    for(String s : errors) { 
     text.append(s + "\n"); 
    } 
    panel.add(text); 

不過,如果你需要創建多個JTextArea,你可以使用一個BoxLayout這樣的:

panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
0

你只需要編寫一個函數,增加了一個新行字符作爲你的ArrayList的一個元素,在ArrayList中的每個其他元素之後。下面我做了一個測試程序,顯示如何做到這一點。我也檢查了oputput。只需粘貼代碼並理解代碼的工作。祝一切順利!

 import java.util.ArrayList; 

    public class TestingArraylist { 


static ArrayList<String> errors = new ArrayList<String>(); 
static final String[] warnings = new String[]{"Error 0 occured","Error 1 occured","Error 2 occured","Error 3 occured","Error 4 occured"};; 


    public static void addNewLineToArrayList(String[] elementofarraylist){ 

    for(int i =0;i<elementofarraylist.length;i++){ 
    errors.add(elementofarraylist[i]); 
    errors.add("\n"); //this is what you need to do! 
    } 
} 

public static void main(String[] args) { 

    addNewLineToArrayList(warnings); 
    //checking below if our work has really succeded or not!! 
    for(int j =0;j<errors.size();j++){ 

    System.out.print(errors.get(j)); 

    } 

} 

}