2014-10-21 2 views
1

다음 코드화 된 GUI를 가지고 있지만 오른쪽에 스크롤 막대 길이를 늘리고 싶습니다.Java GUI JScrollBar 길이를 설정하는 방법은 무엇입니까?

모든 아이디어 어떻게해야합니까?

// test class that implements the gui stuff. 
public class testing 
{ 

    //variables 
    private JFrame f = new JFrame("GUI TEST"); 

    private JPanel p = new JPanel(); 
    private JPanel p2 = new JPanel(); 
    private JPanel p3 = new JPanel(); 
    private JPanel p4 = new JPanel(); 
    private JPanel p5 = new JPanel(); 
    private JPanel p6 = new JPanel(); 
    private JPanel p7 = new JPanel(); 
    private JPanel p8 = new JPanel(); 
    private JPanel p9 = new JPanel(); 
    private JPanel p10 = new JPanel(); 
    private JPanel p11 = new JPanel(); 

    private JButton b1 = new JButton("Button"); 

    private JTextField tf1 = new JTextField("           "); 
    private JTextField tf2 = new JTextField("           "); 
    private JTextField tf3 = new JTextField("           "); 

    private JTextArea ta1 = new JTextArea(10,45); 

    private JLabel label1 = new JLabel("Label 1"); 
    private JLabel label2 = new JLabel("Label 2"); 
    private JLabel label3 = new JLabel("Label 3"); 
    private JLabel label4 = new JLabel("Label 4"); 

    private JScrollBar sb1 = new JScrollBar(); 

    //class constructor 
    public testing() 
    { 
     gui(); 
    } 

    public void gui() 
    { 
     //change length of scroll bar 
     f.setVisible(true); 
     f.setSize(600,300); 
     p.add(label1); 
     p.add(tf1); 
     p2.add(label2); 
     p2.add(tf2); 
     p3.add(label3); 
     p3.add(tf3); 
     p4.add(sb1); 
     p4.add(label4); 
     p5.add(ta1); 
     p6.add(b1); 
     p4.setBackground(Color.GRAY); 
     p9.setBackground(Color.GRAY); 
     p10.setBackground(Color.GRAY); 
     p11.setBackground(Color.GRAY); 
     p9.add(p); 
     p9.add(p2); 
     p9.add(p3); 
     p10.add(p5); 
     p11.add(p6); 
     //adds panels to frames 
     f.add(p4, BorderLayout.EAST); 
     f.add(p9, BorderLayout.NORTH); 
     f.add(p10, BorderLayout.CENTER); 
     f.add(p11, BorderLayout.PAGE_END); 
    } 

    public static void main(String[] args) 
    { 
     new testing(); 
    } 
+0

나는 뷰포트의 적절한 사이즈가 당신을 위해 일하는 것이 설정 생각합니다. –

+0

흠. 감사합니다. 나는 그것을 체크 할 것이다. – Charkins12

+0

[getPreferredSize()'] (http://stackoverflow.com/q/7229226/230513)를 실제로 오버라이드 할 때'setPreferredSize()'를 사용하지 마십시오. – trashgod

답변

2

Ordinarilly, 당신은 단순히 당신의 JTextArea 당신을 위해 크기 조정 동작을 처리하는 JScrollPane에 추가 할 것입니다.

f.add(new JScrollPane(ta1), BorderLayout.CENTER); 

는 데모 목적을 위해, 당신은 효과를 볼 수 JScrollBargetPreferredSize() 방법을 대체 할 수 있습니다. 또한

private JScrollBar sb1 = new JScrollBar(){ 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(
      super.getPreferredSize().width, ta1.getPreferredSize().height); 
    } 
}; 

,

  • 스윙 GUI 개체를 구성하고 event dispatch thread을 조작해야합니다.

  • 적절한 구성자를 사용하여 텍스트 구성 요소의 원하는 초기 크기를 설정하십시오.

  • 원하는 크기 조정 동작을 얻으려면 적절한 layout을 사용하십시오.

image

시험으로 :

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollBar; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 

public class Testing { 
    //variables 
    private JFrame f = new JFrame("GUI TEST"); 

    private JPanel p = new JPanel(); 
    private JPanel p2 = new JPanel(); 
    private JPanel p3 = new JPanel(); 
    private JPanel p4 = new JPanel(); 
    private JPanel p5 = new JPanel(); 
    private JPanel p6 = new JPanel(); 
    private JPanel p9 = new JPanel(); 
    private JPanel p10 = new JPanel(); 
    private JPanel p11 = new JPanel(); 

    private JButton b1 = new JButton("Button"); 

    private JTextField tf1 = new JTextField(12); 
    private JTextField tf2 = new JTextField(12); 
    private JTextField tf3 = new JTextField(12); 

    private JTextArea ta1 = new JTextArea(10, 45); 

    private JLabel label1 = new JLabel("Label 1"); 
    private JLabel label2 = new JLabel("Label 2"); 
    private JLabel label3 = new JLabel("Label 3"); 
    private JLabel label4 = new JLabel("Label 4"); 

    private JScrollBar sb1 = new JScrollBar(){ 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(super.getPreferredSize().width, ta1.getPreferredSize().height); 
     } 
    }; 

    //class constructor 
    public Testing() { 
     gui(); 
    } 

    public void gui() { 
     p.add(label1); 
     p.add(tf1); 
     p2.add(label2); 
     p2.add(tf2); 
     p3.add(label3); 
     p3.add(tf3); 
     p4.add(sb1); 
     p4.add(label4); 
     p5.add(ta1); 
     p6.add(b1); 
     p4.setBackground(Color.GRAY); 
     p9.setBackground(Color.GRAY); 
     p10.setBackground(Color.GRAY); 
     p11.setBackground(Color.GRAY); 
     p9.add(p); 
     p9.add(p2); 
     p9.add(p3); 
     p10.add(p5); 
     p11.add(p6); 
     //adds panels to frames 
     f.add(p4, BorderLayout.EAST); 
     f.add(p9, BorderLayout.NORTH); 
     f.add(p10, BorderLayout.CENTER); 
     f.add(p11, BorderLayout.PAGE_END); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.pack(); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(() -> { 
      new Testing(); 
     }); 
    } 
} 
+0

완벽한! 아주 좋은 답장! – Charkins12

관련 문제