2017-10-11 14 views
-1

여기에 스윙을 사용하여 Java 프로그램을 작성하고 있지만 약간의 문제가 있습니다. 내 텍스트 영역에 스크롤 막대를 추가 할 수 있습니다GridLayout (1,1)으로 스크롤 막대

code image

내가이 문제를 해결하고 표시하는 방법을 찾고 있어요 : 여기

//Text area 
    public static JTextArea output = new JTextArea("======================== The Outputs HERE ================================"); 

    // JBouton 
    private JButton bouton = new JButton(); 
    private JButton tpBouton = new JButton("TP 205"); 

    // JLabel 
    private JLabel label = new JLabel("TP DE 205"); 
    public static JLabel initialState = new JLabel("TEST"); 
    public static JLabel state = new JLabel(""); 
    public static JLabel tableShow = new JLabel("Entrer l'element a rechercher : "); 
    public static JLabel finalState = new JLabel(""); 
    private JLabel explicitText = new JLabel(); 

    // JTextField 
    private JFormattedTextField input = new JFormattedTextField(NumberFormat.getIntegerInstance()); 
    //JTextField input = new JTextField("Choisir l'element a rechercher ..."); 

    // JPanel 
    JPanel mainBGRadio = new JPanel(); 
    JPanel secondBGRadio = new JPanel(); 
    JPanel b7 = new JPanel(); 
    JPanel main = new JPanel(); 
    JPanel container = new JPanel(); 
    JPanel outPutField = new JPanel(); 

    // Ensemble 
    private Ensemble set = new Ensemble(); 

    // JRadioButton 

    // for the main group 
    private JRadioButton lookup = new JRadioButton("Look Up"); 
    private JRadioButton insert = new JRadioButton("Insert"); 
    private JRadioButton delete = new JRadioButton("Delete"); 
    private JRadioButton difference = new JRadioButton("Difference"); 
    private JRadioButton intersection = new JRadioButton("Intersection"); 
    private JRadioButton union = new JRadioButton("Union"); 

    // for the second group 

    private JRadioButton sortedList = new JRadioButton("liste Trie"); 
    private JRadioButton simpleList = new JRadioButton("liste Simple"); 
    private JRadioButton doubleList = new JRadioButton("liste Avec Doublons"); 
    private JRadioButton simpleVector = new JRadioButton("vecteur Simple"); 
    private JRadioButton sortedVector = new JRadioButton("vecteur Trie"); 
    private JRadioButton booleanVector = new JRadioButton("vecteur Boolean"); 

    // JButtonGroup 

    // some bouton group 

    private ButtonGroup mainBG = new ButtonGroup(); 
    private ButtonGroup secondBG = new ButtonGroup(); 

    // ArrayList 

    private ArrayList<JRadioButton> radioList = new ArrayList(){{ 
     add(lookup); 
     add(insert); 
     add(delete); 
     add(union); 
     add(intersection); 
     add(difference); 
    }}; 

    private ArrayList<JRadioButton> radioFunctionList = new ArrayList(){{ 
     add(sortedList); 
     add(doubleList); 
     add(simpleList); 
     add(simpleVector); 
     add(sortedVector); 
     add(booleanVector); 
    }}; 

    // Font 

    Font font = new Font("ubuntu", Font.BOLD, 45); 
    Font explicitFont = new Font("ubuntu", Font.BOLD, 15); 
    Font stateFont = new Font("ubuntu", Font.BOLD, 25); 
    Font textareaFont = new Font("ubuntu", Font.ITALIC, 19); 


    public Fenetre(){ 

    } 

/** 
* this is the main window with all its contains element 
*/ 
public void montrer(){ 

     // window Params 
     this.setTitle("TP 205"); 
     this.setSize(800, 800); 
     this.setLocationRelativeTo(null); 

     // bouton configuration 

     bouton.setText("Simuler"); 
     bouton.addActionListener(new BtnClass()); 
     tpBouton.addActionListener(new BtnClass()); 
     tpBouton.setBackground(Color.BLACK); 
     tpBouton.setFont(explicitFont); 
     tpBouton.setForeground(Color.WHITE); 


     // JLabel configuration 

     explicitText.setText("Bienvenur sur notre tp de 205"); 
     label.setFont(font); 
     explicitText.setFont(explicitFont); 
     explicitText.setForeground(Color.WHITE); 
     label.setForeground(Color.WHITE); 
     state.setForeground(Color.WHITE); 
     state.setFont(stateFont); 
     initialState.setForeground(Color.WHITE); 
     initialState.setFont(stateFont); 
     finalState.setForeground(Color.WHITE); 
     finalState.setFont(stateFont); 

     // input configuration 

     input.setFont(explicitFont); 
     input.setPreferredSize(new Dimension(150, 30)); 
     input.setForeground(Color.BLACK); 
     input.setMaximumSize(new Dimension(Integer.MAX_VALUE, input.getMinimumSize().height)); 
     output.setFont(textareaFont); 
     output.setWrapStyleWord(true); 
     output.setLineWrap(true); 

     JScrollPane scrool = new JScrollPane(); 
     scrool.setBorder(BorderFactory.createTitledBorder("LIONEL")); 
     scrool.setViewportView(output); 

     // layout configuration 

     JPanel b1 = new JPanel(); 
     b1.setLayout(new BoxLayout(b1, BoxLayout.LINE_AXIS)); 
     b1.add(label); 

     JPanel b2 = new JPanel(); 
     b2.setLayout(new BoxLayout(b2, BoxLayout.LINE_AXIS)); 
     b2.add(explicitText); 

     JPanel b3 = new JPanel(); 
     b3.setLayout(new BoxLayout(b3, BoxLayout.LINE_AXIS)); 
     b3.add(tpBouton); 
     b3.add(bouton); 

     JPanel b4 = new JPanel(); 
     b4.setLayout(new BoxLayout(b4, BoxLayout.LINE_AXIS)); 
     b4.add(state); 

     JPanel b5 = new JPanel(); 
     b5.setLayout(new BoxLayout(b5, BoxLayout.LINE_AXIS)); 
     b5.add(finalState); 

     JPanel b6 = new JPanel(); 
     b6.setLayout(new BoxLayout(b6, BoxLayout.LINE_AXIS)); 
     b6.add(initialState); 

     b7.setLayout(new BoxLayout(b7, BoxLayout.LINE_AXIS)); 
     b7.add(tableShow); 
     b7.add(input); 
     b7.setVisible(false); 

     this.setEventSimpleRadio(); 
     this.setEventSecondRadio(); 
     secondBGRadio.setVisible(false); 
     mainBGRadio.setLayout(new BoxLayout(mainBGRadio, BoxLayout.LINE_AXIS)); 
     secondBGRadio.setLayout(new BoxLayout(secondBGRadio, BoxLayout.LINE_AXIS)); 
     container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS)); 
     main.setLayout(new GridLayout(2, 1)); 

     b1.setBackground(Color.ORANGE); 
     b2.setBackground(Color.ORANGE); 
     b3.setBackground(Color.ORANGE); 
     b4.setBackground(Color.ORANGE); 
     b5.setBackground(Color.ORANGE); 
     b6.setBackground(Color.ORANGE); 
     b7.setBackground(Color.ORANGE); 


     mainBGRadio.setBackground(Color.ORANGE); 
     secondBGRadio.setBackground(Color.ORANGE); 

     container.add(b1); 
     container.add(b2); 
     container.add(mainBGRadio); 
     container.add(secondBGRadio); 
     container.add(b7); 
     container.add(b6); 
     container.add(b4); 
     container.add(b5); 

     container.add(b3); 
     outPutField.add(output); 
     outPutField.add(scrool); 
     outPutField.setLayout(new GridLayout(0,1, 5, 5)); 
     container.setBackground(Color.ORANGE); 
     main.add(container); 
     main.add(outPutField); 


     this.setContentPane(main); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setVisible(true); 

그것이 무엇을 생산의 스크린 샷 텍스트 영역이 오버플로되어있을 때 스크롤 막대를 제대로 스크롤하고 작은 스크롤 막대가 있습니다.

+4

유효한 [mcve]를 게시하십시오. 코드에는 관련없는 구성 요소를 포함하여 불필요한 코드가 많이 포함되어 있으며 코드가 충분하지 않아서 컴파일 또는 실행하기에 충분하지 않습니다. JTextfield를 둘 이상의 컨테이너에 추가하지 마십시오. 뷰포트 (양호)와 JPanel (불량) 모두에 추가합니다. 이 마지막 비트는 뷰포트에서 뷰포트를 제거하므로 볼 수없는 것일 수 있습니다. –

+0

다른 말로하면,이 행을 제거하십시오 :'outPutField.add (output);'는 당신을 망쳐 놓기 때문입니다. –

+0

thakns ... 문제는 ... 프로그램에 4 개의 클래스 파일이 있고 모든 클래스 파일에는 200 개가 넘는 라인이 있습니다 ... 이유는 여기에 가져 오지 않았기 때문입니다 ... –

답변

1

일 것 JTextArea에 사용을위한 기본 논리 :

//public static JTextArea output = new JTextArea("==== The Outputs HERE =====; 
public static JTextArea output = new JTextArea(5, 30); 

이 당신이 데이터의 5 행과 행 당 30 문자로 텍스트 영역을 만들 수 있습니다. 5 행 이상의 데이터가 생기면 스크롤바가 나타납니다. 또한

output = "========= The Outputs HERE ================================"); 

, 당신이 모든 변수에 static 키워드를 사용해서는 안됩니다 :

는 그런 다음 생성자는 기본 텍스트를 할당 할 수 있습니다. 이것은 잘못된 수업 디자인을 보여줍니다. How to Use Text Area에있는 Swing 튜토리얼의 섹션을 읽고 클래스를 더 잘 디자인하는 방법을 보여주는 실습 예제를 읽어보십시오.

모든 Swing 기본 사항에 대한 자습서로의 링크를 편리하게 유지하십시오.

+0

thakns. 나는 많은 다른 사람들이 자식 클래스가 정체 된 devellopement에 대한 다른 클래스의 사람들을 사용하기 때문에 그것을 그렇게했습니다. –

+0

@LionelT,'thakns .' - 도움이되어 기쁩니다. 사람들이 문제가 해결되었음을 알 수 있도록 체크 표시를 클릭하여 답변을 "수락"하는 것을 잊지 마십시오. – camickr

+0

'나는 그것을 그렇게했기 때문에 ... '- 다시 말하자면 그것은 거친 디자인입니다. 정적 키워드를 사용하는 방법이 아닙니다. 이 자습서는 더 나은 디자인을 보여줍니다. 코드를보다 쉽게 ​​관리하고 디버깅 할 수 있도록보다 나은 설계 방식을 익히십시오. – camickr