2

テキストエリアにスクロールバーを作りたいのですが、JPanel Layoutをnullにするとスクロールバーが表示されません!

私は試した

JScrollPane scrollbar1 = 
  new JScrollPane(
    ta1,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

しかし、null レイアウトのために機能しませんでした。

これが私の現在のコードです:

import javax.swing.*;

import java.awt.*;
public class app extends JFrame {

    public static void main(String[] args)
    {
        new app();
    }

    public app()
    {
        this.setSize(400,400);
        this.setLocation(0,0);
        this.setResizable(false);
        this.setTitle("Application");           
        JPanel painel = new JPanel(null);           
        // Creating the Input
        JTextField tf1 = new JTextField("Some random text", 15);            
        tf1.setBounds(5,5,this.getWidth()-120,20);
        tf1.setColumns(10);
        tf1.setText("Omg");         
        painel.add(tf1);            
        // Creating the button          
        JButton button1 = new JButton("Send");          
        button1.setBounds(290, 5, 100, 19);         
        painel.add(button1);            
        // Creating the TextArea            
        JTextArea ta1 = new JTextArea(15, 20);
        JScrollPane scr = new JScrollPane();
        ta1.setBounds(5, 35, 385, 330);
        ta1.setLineWrap(true);
        ta1.setWrapStyleWord(true);         
        painel.add(ta1);
        this.add(painel);
        this.setVisible(true);
    }
}

正しく動作させたい。誰かが私を助けることができる場合は、下にコメントを残してください!

4

4 に答える 4

3

以下のすべての問題を修正しました。これは作業コードです。変更についてはコメントをお読みください。

import javax.swing.*;

import java.awt.*;

public class app extends JFrame {

    public static void main(String[] args) {
        new app();
    }

    public app() {
        this.setSize(400, 400);
        this.setLocation(0, 0);
        this.setResizable(false);
        this.setTitle("Application");
        JPanel painel = new JPanel(null);
        // Creating the Input
        JTextField tf1 = new JTextField("Some random text", 15);
        tf1.setBounds(5, 5, this.getWidth() - 120, 20);
        tf1.setColumns(10);
        tf1.setText("Omg");

        // resultsTA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        painel.add(tf1);
        // Creating the button
        JButton button1 = new JButton("Send");
        button1.setBounds(290, 5, 100, 19);
        painel.add(button1);
        // Creating the TextArea
        JTextArea ta1 = new JTextArea(15, 20);
        JScrollPane scr = new JScrollPane(ta1,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);// Add your text area to scroll pane 
        ta1.setBounds(5, 35, 385, 330);
        ta1.setLineWrap(true);
        ta1.setWrapStyleWord(true);
        scr.setBounds(20, 30, 100, 40);// You have to set bounds for all the controls and containers incas eof null layout
        painel.add(scr);// Add you scroll pane to container
        this.add(painel);
        this.setVisible(true);
    }
}

編集。Java の oracle からチュートリアルを読んでください。そして、適切なレイアウトマネージャーの使用を開始してください...お役に立てば幸いです

于 2013-08-17T20:15:23.320 に答える
2

これは、多くの @mKorbelsポイントの基本的な例です。、 のデフォルト レイアウトがJPanel()FlowLayout()そのコンポーネントの推奨サイズをどのように使用しているかに注意してください。への呼び出しf.setSize()は、スクロールバーを強制的に表示するためのオプションです。

画像

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.*;

public class App {

    public static void main(String[] args) {
        new App();
    }

    public App() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("Application");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel panel = new JPanel();
                JTextField tf1 = new JTextField("Some random text", 15);
                tf1.setColumns(10);
                tf1.setText("Omg");
                panel.add(tf1);
                JButton button1 = new JButton("Send");
                panel.add(button1);
                JTextArea ta = new JTextArea(15, 20);
                JScrollPane scr = new JScrollPane(ta);
                scr.setVerticalScrollBarPolicy(
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                ta.setLineWrap(true);
                ta.setWrapStyleWord(true);
                f.add(panel, BorderLayout.NORTH);
                f.add(scr, BorderLayout.CENTER);
                f.pack();
                Dimension d = scr.getPreferredSize();
                f.setSize(d.width, d.height);
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        });
    }
}
于 2013-08-17T20:15:45.790 に答える
2

あなたのコンストラクターにあなたJTextAreaを渡してから、あなたのオブジェクトを. したがって、次のようになります。JScrollPaneJScrollPaneContainerJTextArea

JScrollPane scr = new JScrollPane(ta1);
panel.add(scr);
于 2013-08-17T19:46:34.837 に答える
2

If someone can help me, leave a comment below please!

  • なぜあなたはあなたの頭の壁で粉砕しようとしているのですかJScrollPane、動的、サイズ変更可能に指定されてLayoutManagerいます、AbsoluteLayoutこれはその基本的なプロパティを壊すことができます

  • 上から

    1. public class app extends JFrame {

      • public class App {---> Java 命名規則
      • 何も拡張せず、JFrameローカル変数として作成します
    2. new app();---> Oracle チュートリアルの初期スレッドを参照

    3. 別のものを作成しJPanel、そこに置きJTextFieldJButton

    4. 何かを重ねましたかtf1.setBounds(5,5,this.getWidth()-120,20);

    5. NullLayoutを使用しないと正しく動作しませんInsets

    6. to のbuilt_inFlowLayoutを変更JPanel painel = new JPanel(null);BorderLayout、そこにJScrollPanetoJTextAreaを付けますCENTER area

    7. to でJScrollPane直接置くことができ、別のものをand toまたはで、APIに実装しましたJTextAreaJFrames CENTER areaJPanelJTextFieldJButtonSOUTHNORTHJFrameBorderLayout

    8. JScrollPaneそこに配置されているよりも小さいJScrollbars場合にのみ表示DimensionJComponent

    9. JFrame.pack()の代わりに使用しますsetSize。この行は前にある必要がありますsetVisible

于 2013-08-17T20:11:17.823 に答える