Java的。文本区域中的GUI WindowBuilder滚动条不起作用

时间:2013-01-16 00:07:53

标签: java class user-interface textarea scrollbar

我不知道为什么文本区域中的滚动条不起作用。我在互联网上找到了很多解决方案,但没有1对我有帮助。

textArea1 = new JTextArea();
textArea1.setBounds(13, 28, 182, 199);
panel.add(textArea1);

JScrollBar scrollBar = new JScrollBar();
scrollBar.setBounds(205, 1, 17, 242);
panel.add(scrollBar);

我发现不能将Panel的布局绝对,如果我将其更改为Group布局相同。 怎么了?你可以帮帮我吗?谢谢。

更新:

package lt.kvk.i3_2.kalasnikovas_stanislovas;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTextPane;
import javax.swing.DropMode;
import javax.swing.JFormattedTextField;
import java.awt.Component;
import javax.swing.Box;
import java.awt.Dimension;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JMenuItem;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import java.awt.SystemColor;
import java.awt.Font;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.JScrollBar;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JLabel;
import javax.swing.JToolBar;

public class KDVizualizuotas {

    private JFrame frmInformacijaApieMuzikos;
    private JTextField txtStilius;
    private JTextArea textArea1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    KDVizualizuotas window = new KDVizualizuotas();
                    window.frmInformacijaApieMuzikos.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public KDVizualizuotas() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmInformacijaApieMuzikos = new JFrame();
        frmInformacijaApieMuzikos.setResizable(false);
        frmInformacijaApieMuzikos.setIconImage(Toolkit.getDefaultToolkit().getImage(KDVizualizuotas.class.getResource("/lt/kvk/i3_2/kalasnikovas_stanislovas/resources/Sidebar-Music-Blue-icon.png")));
        frmInformacijaApieMuzikos.setTitle("Muzikos stiliai");
        frmInformacijaApieMuzikos.setBounds(100, 100, 262, 368);
        frmInformacijaApieMuzikos.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        txtStilius = new JTextField();
        txtStilius.setBounds(10, 34, 128, 20);
        txtStilius.setColumns(10);

        JButton btnIekoti = new JButton("Ie\u0161koti");
        btnIekoti.setBounds(146, 36, 89, 19);
        btnIekoti.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
        //      textArea1.append(txtStilius.getText()+"\n");
        //      txtStilius.getText();



                 Scanner input = new Scanner(System.in);
                    try {
                        FileReader fr = new FileReader("src/lt/kvk/i3_2/kalasnikovas_stanislovas/Stiliai.txt");
                        BufferedReader br = new BufferedReader(fr);
                        String stiliuSarasas;
                        while((stiliuSarasas = br.readLine()) != null) {
                            System.out.println(stiliuSarasas);
                            textArea1.append(stiliuSarasas+"\n");
                            }
                        fr.close();
                        }
                    catch (IOException e) {
                        System.out.println("Error:" + e.toString());
                    }





            }
        });

        JPanel panel = new JPanel();
        panel.setBounds(10, 65, 224, 243);
        panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
        panel.setBackground(SystemColor.text);

        JLabel lblveskiteMuzikosStili = new JLabel("\u012Eveskite muzikos stili\u0173:");
        lblveskiteMuzikosStili.setBounds(10, 14, 222, 14);
        frmInformacijaApieMuzikos.getContentPane().setLayout(null);
        panel.setLayout(null);
        frmInformacijaApieMuzikos.getContentPane().add(panel);


        JLabel lblInformacijaApieMuzikos = new JLabel("Informacija apie muzikos stili\u0173:");
        lblInformacijaApieMuzikos.setBounds(12, 3, 190, 14);
        panel.add(lblInformacijaApieMuzikos);

        textArea1 = new JTextArea();
        textArea1.setBounds(13, 28, 182, 199);
        panel.add(textArea1);

        JScrollBar scrollBar = new JScrollBar();
        scrollBar.setBounds(205, 1, 17, 242);
        panel.add(scrollBar);
        frmInformacijaApieMuzikos.getContentPane().add(txtStilius);
        frmInformacijaApieMuzikos.getContentPane().add(btnIekoti);
        frmInformacijaApieMuzikos.getContentPane().add(lblveskiteMuzikosStili);

        JMenuBar menuBar = new JMenuBar();
        frmInformacijaApieMuzikos.setJMenuBar(menuBar);

        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

        JMenuItem mntmExit = new JMenuItem("Exit");
        mntmExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);             
            }

        });
        mntmExit.setIcon(new ImageIcon(KDVizualizuotas.class.getResource("/lt/kvk/i3_2/kalasnikovas_stanislovas/resources/exitas.png")));
        mnFile.add(mntmExit);

        JMenu mnEdit = new JMenu("Edit");
        menuBar.add(mnEdit);

        JMenu mnHelp = new JMenu("Help");
        menuBar.add(mnHelp);

        JMenuItem mntmHelp = new JMenuItem("Help");
        mnHelp.add(mntmHelp);

        JMenu mnAbout = new JMenu("About");
        menuBar.add(mnAbout);

        JMenuItem mntmAbout = new JMenuItem("About");
        mntmAbout.setIcon(new ImageIcon(KDVizualizuotas.class.getResource("/lt/kvk/i3_2/kalasnikovas_stanislovas/resources/questionmark.png")));
        mnAbout.add(mntmAbout);
    }
}

1 个答案:

答案 0 :(得分:0)

您需要将要包含在滚动窗格中的组件添加到其中。

您可以通过JScrollPane的构造函数或JScrollPane#setViewportView方法

执行此操作

enter image description here

public class ScrollPaneTest {

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

  public ScrollPaneTest() {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
        }

        JPanel bigPane = new JPanel();
        bigPane.setBackground(Color.BLUE);
        // This is not recommended, but is used for demonstration purposes
        bigPane.setPreferredSize(new Dimension(1024, 768)); 

        JScrollPane scrollPane = new JScrollPane(bigPane);
        scrollPane.setPreferredSize(new Dimension(400, 400));

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(scrollPane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    });
  }
}

更新了适当的布局

你应该避免null或绝对布局,它们只会在长期错误中伤害你。

您还可以找到How to use Scroll Panes使用

enter image description here

public class BadLayout {

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

  public BadLayout() {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
        }

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TestPane());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

      }
    });
  }

  public class TestPane extends JPanel {

    private JTextField searchField;
    private JButton searchButton;
    private JTextArea searchResults;

    public TestPane() {
      setLayout(new BorderLayout());

      searchResults = new JTextArea();
      searchResults.setLineWrap(true);
      searchResults.setWrapStyleWord(true);
      JScrollPane scrollPane = new JScrollPane(searchResults);

      JPanel results = new JPanel(new BorderLayout());
      results.setBorder(new EmptyBorder(4, 8, 8, 8));
      results.add(scrollPane);
      add(results);

      JPanel search = new JPanel(new GridBagLayout());
      search.setBorder(new EmptyBorder(8, 8, 4, 8));
      searchField = new JTextField(12);
      searchButton = new JButton("Search");

      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = 0;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.weightx = 1;
      gbc.insets = new Insets(0, 0, 0, 4);
      search.add(searchField, gbc);

      gbc.insets = new Insets(0, 0, 0, 0);
      gbc.gridx++;
      gbc.fill = GridBagConstraints.NONE;
      gbc.weightx = 0;
      search.add(searchButton, gbc);

      add(search, BorderLayout.NORTH);
    }

    @Override
    public Dimension getPreferredSize() {
      return new Dimension(200, 400);
    }
  }  
}