2017-10-11 1 views
-1

ここで私はSwingを使ってJavaプログラムを作成していますが、何か問題があります。私は私のテキスト領域にスクロールバーを追加することができます。ここではGridLayout(1,1)でスクロールバー

//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); 

は、それが生成するもののスクリーンショットです:

code image

私はこの問題を解決する方法を探していると示していますテキスト領域がオーバーフローしたときにスクロールバーを正しくスクロールし、スクロールバーを小さくします。

+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); 

これは、あなたがデータとラインあたり約30文字の5行にテキストエリアを作成することができます。 5行以上のデータがあると、スクロールバーが表示されます。また

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

、すべてのあなたの変数にstaticキーワードを使用すべきではありません:

次に、あなたのコンストラクタでは、デフォルトのテキストを割り当てることができます。これはあなたのクラスの誤ったデザインを示しています。 How to Use Text AreaのSwingチュートリアルのセクションを読んで、クラスをよりうまく設計する方法を示す実例を読んでください。

すべてのSwingの基本については、このチュートリアルのリンクを参考にしてください。

+0

件。私はそれを好きにしました。他の多くの子クラスは静的なデベロップメントのために他のクラスのものを使用しています。 –

+0

@LionelT、 'thakns .' - 助けてくれてうれしいです。チェックマークをクリックして回答を「受け入れる」ことを忘れないでください。人々は問題が解決されたことを知ります。 – camickr

+0

「私はそれをそのようにしているから...」 - それはやはり奇妙なデザインです。これは、staticキーワードの使用方法ではありません。このチュートリアルでは、より良いデザインを示しています。コードの保守とデバッグを容易にするために、より良い設計アプローチを学んでください。 – camickr