[Resolvido]JFrame com barra de rolagem

1 resposta
P

Boa noite pessoas,

eu implementei o seguinte JFrame:

package com.leds.obs_legend;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.colorchooser.AbstractColorChooserPanel;


@SuppressWarnings("serial")
public class Legend extends JFrame{


	private Legend close;
	/**
	 * Initialize the contents of the frame.
	 */
	public Legend() {
		super("TerraME GIMS :: Legend");
		setBounds(100, 100, 986, 833);
		
		
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 946, 55);
	
		panel.setBackground(Color.WHITE);
		panel.setForeground(Color.WHITE);
		panel.setLayout(null);
		
		JLabel lblLegend = new JLabel("Legend :: Properties");
		lblLegend.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblLegend.setBounds(10, 11, 121, 14);
		panel.add(lblLegend);
	
		getContentPane().setLayout(new GridLayout(0, 1, 0, 0));
        
        JPanel pnlPrincipal = new JPanel();
        getContentPane().add(pnlPrincipal);
        GridBagLayout gbl_pnlPrincipal = new GridBagLayout();
        gbl_pnlPrincipal.columnWidths = new int[]{970, 0};
        gbl_pnlPrincipal.rowHeights = new int[]{64, 721, 16, 0};
        gbl_pnlPrincipal.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_pnlPrincipal.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
        pnlPrincipal.setLayout(gbl_pnlPrincipal);
       
        
        
        JPanel panel_5 = new JPanel();
        GridBagConstraints gbc_panel_5 = new GridBagConstraints();
        gbc_panel_5.insets = new Insets(0, 0, 5, 0);
        gbc_panel_5.anchor = GridBagConstraints.NORTH;
        gbc_panel_5.fill = GridBagConstraints.BOTH;
        gbc_panel_5.gridx = 0;
        gbc_panel_5.gridy = 0;
        pnlPrincipal.add(panel_5, gbc_panel_5);
        panel_5.setBackground(Color.WHITE);
        panel_5.setLayout(null);
        
        JLabel lblLegendProperties = new JLabel("Legend :: Properties");
        lblLegendProperties.setBounds(25, 25, 112, 14);
        lblLegendProperties.setVerticalAlignment(SwingConstants.BOTTOM);
        lblLegendProperties.setFont(new Font("Tahoma", Font.BOLD, 11));
        panel_5.add(lblLegendProperties);
        
        JPanel panel_4 = new JPanel();
        GridBagConstraints gbc_panel_4 = new GridBagConstraints();
        gbc_panel_4.insets = new Insets(0, 0, 5, 0);
        gbc_panel_4.fill = GridBagConstraints.BOTH;
        gbc_panel_4.gridx = 0;
        gbc_panel_4.gridy = 1;
        pnlPrincipal.add(panel_4, gbc_panel_4);
        panel_4.setLayout(null);
        
        JPanel panel_1 = new JPanel();
        panel_1.setBounds(10, 11, 933, 96);
        panel_4.add(panel_1);
        panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Parameters", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panel_1.setLayout(null);
        
        JLabel lblType = new JLabel("Type");
        lblType.setBounds(132, 26, 32, 14);
        panel_1.add(lblType);
        
        JComboBox<String> cBType = new JComboBox<String>();
        cBType.setModel(new DefaultComboBoxModel<String>(new String[] {"bool", "number", "string", "datetime"}));
        cBType.setBounds(132, 51, 70, 20);
        panel_1.add(cBType);
        
        JLabel lblGrouping = new JLabel("Grouping");
        lblGrouping.setBounds(22, 26, 64, 14);
        panel_1.add(lblGrouping);
        
        JComboBox<String> cBGrouping = new JComboBox<String>();
        cBGrouping.setModel(new DefaultComboBoxModel<String>(new String[] {"equalsteps", "quantil", "stdeviation", "uniquevalue"}));
        cBGrouping.setBounds(22, 51, 100, 20);
        panel_1.add(cBGrouping);
        
        JSpinner spinner = new JSpinner();
        spinner.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
        spinner.setBounds(226, 51, 40, 20);
        panel_1.add(spinner);
        
        JLabel lblSlices = new JLabel("Slices");
        lblSlices.setBounds(226, 26, 64, 16);
        panel_1.add(lblSlices);
        
        JLabel lblPrecision = new JLabel("Precision");
        lblPrecision.setBounds(406, 26, 64, 16);
        panel_1.add(lblPrecision);
        
        JSpinner spinner_1 = new JSpinner();
        spinner_1.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
        spinner_1.setBounds(406, 51, 40, 20);
        panel_1.add(spinner_1);
        
        JLabel lblMaximum = new JLabel("Maximum");
        lblMaximum.setBounds(346, 26, 58, 16);
        panel_1.add(lblMaximum);
        
        JSpinner spinner_2 = new JSpinner();
        spinner_2.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
        spinner_2.setBounds(346, 51, 40, 20);
        panel_1.add(spinner_2);
        
        JLabel lblMinimum = new JLabel("Minimum");
        lblMinimum.setBounds(286, 26, 64, 16);
        panel_1.add(lblMinimum);
        
        JSpinner spinner_3 = new JSpinner();
        spinner_3.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
        spinner_3.setBounds(286, 51, 40, 20);
        panel_1.add(spinner_3);
        
        JSpinner spinner_4 = new JSpinner();
        spinner_4.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
        spinner_4.setBounds(466, 51, 40, 20);
        panel_1.add(spinner_4);
        
        JLabel lblWidth = new JLabel("Width");
        lblWidth.setBounds(466, 26, 64, 16);
        panel_1.add(lblWidth);
        
        JButton btnHelpGrouping = new JButton("");
        btnHelpGrouping.setContentAreaFilled(false);
        btnHelpGrouping.setFocusPainted(false);
        btnHelpGrouping.setIcon(new ImageIcon(Legend.class.getResource("/com/leds/icons/help.png")));
        btnHelpGrouping.setBorderPainted(false);
        btnHelpGrouping.setBounds(80, 20, 25, 25);
        panel_1.add(btnHelpGrouping);
        
        JButton btnHelpType = new JButton("");
        btnHelpType.setContentAreaFilled(false);
        btnHelpType.setFocusPainted(false);
        btnHelpType.setIcon(new ImageIcon(Legend.class.getResource("/com/leds/icons/help.png")));
        btnHelpType.setBorderPainted(false);
        btnHelpType.setBounds(165, 20, 25, 25);
        panel_1.add(btnHelpType);
        
        JSpinner spinner_6 = new JSpinner();
        spinner_6.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
        spinner_6.setBounds(526, 51, 40, 20);
        panel_1.add(spinner_6);
        
        JLabel lbl = new JLabel("stdWi");
        lbl.setBounds(526, 26, 64, 16);
        panel_1.add(lbl);
        
        JSpinner spinner_7 = new JSpinner();
        spinner_7.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
        spinner_7.setBounds(586, 51, 40, 20);
        panel_1.add(spinner_7);
        
        JLabel label_2 = new JLabel("stdWii");
        label_2.setBounds(586, 26, 64, 16);
        panel_1.add(label_2);
        
        
        
        final JPanel panel_2 = new JPanel();
        panel_2.setLayout(null);
        panel_2.setBorder(new TitledBorder(null, "ColorBar", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panel_2.setBounds(10, 118, 948, 557);
        panel_4.add(panel_2);
        

        
        final JPanel panel_3 = new JPanel();
        panel_3.setLayout(null);
        panel_3.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Attribute Color", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panel_3.setBounds(10, 21, 619, 505);
        panel_2.add(panel_3);
        
        JSpinner spinner_5 = new JSpinner();
        spinner_5.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
        spinner_5.setBounds(106, 19, 44, 20);
        panel_3.add(spinner_5);
        
        JLabel label = new JLabel("Attribute Value");
        label.setBounds(10, 25, 86, 14);
        panel_3.add(label);
        
        JButton button_4 = new JButton("");
        button_4.setIcon(new ImageIcon(Legend.class.getResource("/com/leds/icons/help.png")));
        button_4.setFocusPainted(false);
        button_4.setContentAreaFilled(false);
        button_4.setBorderPainted(false);
        button_4.setBounds(160, 16, 26, 23);
        panel_3.add(button_4);
        
        JColorChooser tcc = new JColorChooser();

		tcc.setBounds(10, 53, 600, 396);
		panel_3.add(tcc);
		tcc.setBorder(BorderFactory.createTitledBorder("Color"));
		UIManager.put("ColorChooser.sampleText","SampleText");
		UIManager.put("ColorChooser.previewText","Preview");
		UIManager.put("ColorChooser.swatchesNameText","Swatches");
		tcc.updateUI();
		AbstractColorChooserPanel[] oldPanels = tcc.getChooserPanels();
        tcc.removeChooserPanel(oldPanels[1]);
        tcc.removeChooserPanel(oldPanels[2]);
        tcc.removeChooserPanel(oldPanels[4]);
        
        AbstractColorChooserPanel colorPanel = tcc.getChooserPanels()[0];
        JPanel p = (JPanel) colorPanel.getComponent(0);
        p.remove(2);
        p.remove(1);
   
        colorPanel = tcc.getChooserPanels()[1];
        JLabel lblCodeColorRGB = (JLabel) colorPanel.getComponent(1);
        lblCodeColorRGB.setText("RGB Color Code (Hexadecimal)");
        JPanel pnlRGBCode = (JPanel) colorPanel.getComponent(0);
        pnlRGBCode.remove(3);
        pnlRGBCode.remove(4);
        pnlRGBCode.remove(10);
        JRadioButton green = (JRadioButton) pnlRGBCode.getComponent(1);
        green.setText("Green");
        JRadioButton red = (JRadioButton) pnlRGBCode.getComponent(0);
        red.setText("Red");
        JRadioButton blue = (JRadioButton) pnlRGBCode.getComponent(2);
        blue.setText("Blue");
        		     		     
		
		JButton button = new JButton("New Attribute Color");
		button.setBounds(10, 469, 152, 25);
		panel_3.add(button);
		
		JButton button_3 = new JButton("");
		button_3.setIcon(new ImageIcon(Legend.class.getResource("/com/leds/icons/help.png")));
		button_3.setFocusPainted(false);
		button_3.setContentAreaFilled(false);
		button_3.setBorderPainted(false);
		button_3.setBounds(172, 469, 25, 25);
		panel_3.add(button_3);
		
		JButton button_5 = new JButton("");
		button_5.setFocusPainted(false);
		button_5.setContentAreaFilled(false);
		button_5.setBorderPainted(false);
		button_5.setBounds(394, 78, 25, 25);
		panel_2.add(button_5);
		
		JButton button_6 = new JButton("");
		button_6.setFocusPainted(false);
		button_6.setContentAreaFilled(false);
		button_6.setBorderPainted(false);
		button_6.setBounds(394, 129, 25, 25);
		panel_2.add(button_6);
		
		final JPanel panel_6 = new JPanel();
		panel_6.setBorder(new TitledBorder(null, "Color List", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_6.setBounds(639, 21, 299, 505);
		panel_2.add(panel_6);
		panel_6.setLayout(null);
		
		JList<?> list = new JList<Object>();
		list.setBounds(10, 27, 278, 400);
		panel_6.add(list);
		list.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		
		JButton button_1 = new JButton("Edit");
		button_1.setBounds(10, 455, 84, 25);
		panel_6.add(button_1);
		
		JButton button_2 = new JButton("Delete");
		button_2.setBounds(150, 455, 84, 25);
		panel_6.add(button_2);
		
		JButton button_9 = new JButton("");
		button_9.setIcon(new ImageIcon(Legend.class.getResource("/com/leds/icons/help.png")));
		button_9.setFocusPainted(false);
		button_9.setContentAreaFilled(false);
		button_9.setBorderPainted(false);
		button_9.setBounds(104, 456, 26, 23);
		panel_6.add(button_9);
		
		JButton button_10 = new JButton("");
		button_10.setIcon(new ImageIcon(Legend.class.getResource("/com/leds/icons/help.png")));
		button_10.setFocusPainted(false);
		button_10.setContentAreaFilled(false);
		button_10.setBorderPainted(false);
		button_10.setBounds(244, 456, 26, 23);
		panel_6.add(button_10);
		
		JButton button_7 = new JButton("Ok");
		button_7.setBounds(343, 686, 95, 25);
		panel_4.add(button_7);
		
		JButton button_8 = new JButton("Cancel");
		button_8.setBounds(527, 686, 95, 25);
		panel_4.add(button_8);
		
        
        
		
	}
	public Legend getClose() {
		return close;
	}
	public void setClose(Legend close) {
		this.close = close;
	}
	
	
	
}

Agora o que preciso é o seguinte:

Ao redimensionar a janela, preciso que apareçam barras de rolagem horizontais e verticais que
permitam que mesmo a janela menor que meus painéis eu tenha acesso a todos os meus componentes
através das barras.

Alguma de ideia de como fazer isso. Agradeço a ajuda.
Tentei colocar uma JScrollPane no pnlPrincipal que é o painel
onde estão todos os elementos, mas não consegui, fica uma bagunça
tudo quando eu coloco.

Grato.

1 Resposta

P

Pessoal consegui somente colocando estas linhas de código:

JScrollPane jScrollPane = new JScrollPane(pnlPrincipal);


        jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane.setViewportView(pnlPrincipal);

        getContentPane().add(jScrollPane, BorderLayout.CENTER);

Obg a todos!

Criado 4 de janeiro de 2013
Ultima resposta 4 de jan. de 2013
Respostas 1
Participantes 1