无法更改Borderlayout的大小

时间:2016-01-18 14:53:46

标签: java resize

美好的一天,

我正在构建一个java prorgam,但是我无法使用setSize或SetMaximumSize或setPreferredSize更改borderlayouts的大小。它引起了我的视觉问题 例如,我的软底部有一个非常大的组合框(见附图)。我的组合框combobox2的容器" pane2.setSize(new Dimension(50,50))"无法调整大小。

查看快照:Attached image

enter image description here

感谢您的帮助。

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JComboBox;
import javax.swing.BorderFactory;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.JTable;
import javax.swing.JOptionPane; 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.awt.FlowLayout;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.JFileChooser;

public class TabbedPane extends JFrame {

    public TabbedPane() {

         //This will create the title you see in the upper left of the window    
        setTitle("Sabre Local Data writer 1.0");  
        setSize(600,800); //set size so the user can "see" it
        setResizable(false);


        JTabbedPane jtp = new JTabbedPane();

        //This creates the template on the windowed application that we will be using
       getContentPane().add(jtp);

       JPanel jp1 = new JPanel();//This will create the first tab
       JPanel jp2 = new JPanel();//This will create the second tab
       JPanel jp3 = new JPanel();

       jp1.setBorder(BorderFactory.createEmptyBorder(75, 75, 75, 75));

       //Type JTextField

       JTextField names = new JTextField(50);       
       JTextField pass = new JTextField(10);


       //Type  JComboBox
       JComboBox combo1 = new JComboBox();
       combo1.setPreferredSize(new Dimension(100, 20));
       combo1.addItem("Option 1");
       combo1.addItem("Option 2");
       combo1.addItem("Option 3");
       combo1.addItem("Option 4");

       //Type  JComboBox
       JComboBox combo2 = new JComboBox();
       combo2.setPreferredSize(new Dimension(100, 20));
       combo2.addItem("Option 1");
       combo2.addItem("Option 2");
       combo2.addItem("Option 3");
       combo2.addItem("Option 4");

       //Type  JTextArea
       JTextArea textarea1 = new JTextArea("", 20, 20);  

       //Type  JRadioButton
       JRadioButton radio1 = new JRadioButton("Item 1");
       JRadioButton radio2 = new JRadioButton("Item 2");


       ButtonGroup radiogroup1 = new ButtonGroup();
       radiogroup1.add(radio1);
       radiogroup1.add(radio2);

       //Type  JCheckBox    
      JCheckBox check1 = new JCheckBox("Case 1");
      JCheckBox check2 = new JCheckBox("Case 2");

      ButtonGroup checkboxgroup1 = new ButtonGroup();
      checkboxgroup1.add(check1);
      checkboxgroup1.add(check2);


    //Type  FileChooser   
      JFileChooser chooser = new JFileChooser();
      chooser.showOpenDialog(new JFrame());


       jp1.setLayout(new GridLayout(0,1));

       JPanel pane = new JPanel(new GridLayout(0,1));
       pane.add(new JLabel("Username: "));
       pane.add(names);
       pane.add(new JLabel("Password: "));
       pane.add(pass);
       pane.add(new JLabel("Veuillez Sélectionner un fichier: "));
       JButton select = new JButton("Parcourir");
       pane.add(select);
       pane.add(new JLabel("Veuillez Sélectionner une option: "));
       pane.add(combo1);
       pane.add(new JLabel("Veuillez saisir un texte: "));
       pane.add(textarea1);
       pane.add(new JLabel("Veuillez sélectionner une option: "));
       pane.add(radio1);
       pane.add(radio2);
       pane.add(new JLabel("Veuillez cocher une option: "));
       pane.add(check1);
       pane.add(check2);
       pane.add(new JLabel(""));
       JButton submit = new JButton("Valider");
       pane.add(submit);
       //submit.addActionListener(new Handler());
       jp1.add(pane);


       //This adds the first and second tab to our tabbed pane object and names it
       jtp.addTab("Insérer données", new ImageIcon("saisie.png"), jp1);
       jtp.addTab("Liste Saisies", new ImageIcon("liste.png"), jp2);
       jtp.addTab("Configuration", new ImageIcon("configuration.png"), jp3);

        //This creates a new button called "Press" and adds it to the second tab
       JButton test = new JButton("Press");
       jp2.add(test);



       jp2.setLayout(new BorderLayout());


       // Create columns Settings
       String columnNames1[] = { "Position", "Heure et date", "Consulter", "Action" };

       // Create some data
       String dataValues1[][] =
       {
           { "12", "234", "Consulter", "Effacer" },
           { "-123", "43", "Consulter", "Effacer" },
           { "93", "89.2", "Consulter", "Effacer" },
           { "279", "9033", "Consulter", "Effacer" }
       };

       // Create a new table instance
       JTable table1 = new JTable(dataValues1, columnNames1);

       // Add the table to a scrolling pane
       JScrollPane scrollPane1 = new JScrollPane(table1);
       jp2.add( scrollPane1, BorderLayout.CENTER);

       jp3.setLayout(new BorderLayout());


       // Create columns Settings
       String columnNames[] = { "Column 1", "Column 2", "Column 3" };

       // Create some data
       String dataValues[][] =
       {
           { "12", "234", "67" },
           { "-123", "43", "853" },
           { "93", "89.2", "109" },
           { "279", "9033", "3092" }
       };

       // Create a new table instance
       JTable table = new JTable(dataValues, columnNames);

       // Add the table to a scrolling pane
       JScrollPane scrollPane = new JScrollPane(table);
       jp3.add( scrollPane, BorderLayout.NORTH);

       JPanel pane2 = new JPanel(new GridLayout(0,1));
       pane2.setSize(new Dimension(50,50));
       pane2.add(combo2);
       jp3.add( pane2, BorderLayout.CENTER);

        //This is an Action Listener which reacts to clicking on 
        //the test button called "Press"
        ButtonHandler phandler = new ButtonHandler();
        test.addActionListener(phandler);
        setVisible(true); //otherwise you won't "see" it 
    }

    //This is the internal class that defines what the above Action Listener
    //will do when the test button is pressed.
    class ButtonHandler implements ActionListener{
           public void actionPerformed(ActionEvent e){
                   JOptionPane.showMessageDialog(null, "I've been pressed", "What happened?", JOptionPane. INFORMATION_MESSAGE);
           }
    }

    //example usage
     public static void main (String []args){
        TabbedPane tab = new TabbedPane();
    }

}

1 个答案:

答案 0 :(得分:0)

那么您想要调整大小? 一个元素或整个布局。

我的第一个猜测是你应该在创建JPanel之后调用this.pack(),但如果这不能解决你的问题,请详细说明你的问题。