I already have a JInternalFrame with buttons and text fields but now I want to add a JScrollPane to this Internal Frame, and I don´t know how to.

It is possible add a JScrollPane without add all components again to the JInternalFrame? How?

Thanks!!

I have added the buttons and the texts fields with NetBeans, Design view.

有帮助吗?

解决方案

A very basic example of creating a JScrollPane and add a JPanel to its viewport, and then add that to your JInternalFrame:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class AddStuffToScrollPane {
   public static void main(String[] args) {
      JPanel panel = new JPanel(new BorderLayout());
      panel.add(new JTextArea(20, 30), BorderLayout.CENTER);
      panel.add(new JPanel(new GridLayout(1, 0)) {
         {
            add(new JButton("Foo"));
            add(new JButton("Bar"));
         }
      }, BorderLayout.PAGE_END);
      JScrollPane scrollPane = new JScrollPane(panel);

      JInternalFrame internalFrame = new JInternalFrame("InternalFrame", true,
            true);
      internalFrame.getContentPane().add(scrollPane);
      internalFrame.setPreferredSize(new Dimension(200, 200));
      internalFrame.setSize(internalFrame.getPreferredSize());
      internalFrame.setVisible(true);
      JDesktopPane desktopPane = new JDesktopPane();
      desktopPane.setPreferredSize(new Dimension(400, 400));
      desktopPane.add(internalFrame);
      JOptionPane.showMessageDialog(null, desktopPane);
   }
}

The most important take home message that you should get if you get nothing at all: gear your GUI classes towards creating JPanels, not JFrames, not JInternalFrames. If you can create a decent JPanel, then you can put it anywhere it is needed: inside of a JFrame, or JInternalFrame, or a JScrollPane,... anywhere!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top