在JPanel中设置组件的大小

时间:2014-03-21 22:12:26

标签: java swing jbutton jtextfield layout-manager

我是Java新手。我已经搜索了如何设置JTextFieldJButton的大小,但我仍然无法使其正常工作。希望有人告诉我如何解决它。我声明了高度和宽度,但组件似乎不接受它。 JTextField和JButton带红色&黄色背景应该是相同的高度。 JTextField的宽度也很长。

enter image description here

有我的代码:

package MyPackage;

import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.*;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.*;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.ComponentOrientation;
import javax.swing.JTextField;   

public class MainForm extends JFrame implements ActionListener  {

private PDFNotesBean PDFNotesBean = null;
private JMenuItem cascade = new JMenuItem("Cascade");
private  int numInterFrame=0;
private  JDesktopPane desktop=null;
private ArrayList<File> fileList=new  ArrayList<File>();
private static int categoryButtonWidth= 40;
private static int categoryTextFieldWidth=60;
private static int categoryHight=40;

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run()
        {               
            new MainForm();
        }
    });


}

public  MainForm(){
    super("Example");

    //it is equal to this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    // Name the JMenu & Add Items
    JMenu menu = new JMenu("File");
    menu.add(makeMenuItem("Open"));
    menu.add(makeMenuItem("Save"));
    menu.add(makeMenuItem("Quit"));


    // Add JMenu bar
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    //menuBar.add(menuWindow);
    setJMenuBar(menuBar);


    this.setMinimumSize(new Dimension(400, 500));        
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    desktop = new JDesktopPane();   
    this.setLayout(new BorderLayout());   


    setCategoryPanel();
    this.add(desktop, BorderLayout.CENTER);        
    setVisible(true);        

}


private void setCategoryPanel(){
 //set the color label category       

 JPanel panelCategory=new JPanel();             
 panelCategory.setLayout(new BoxLayout(panelCategory, BoxLayout.LINE_AXIS));

 JButton btnCategory_1=new JButton("");    
 btnCategory_1.setPreferredSize(new Dimension ( categoryButtonWidth, categoryHight));
 btnCategory_1.setBackground(Color.red);
 btnCategory_1.addActionListener(this);
 panelCategory.add(btnCategory_1);

 JTextField txtCategory_1 = new JTextField();
 txtCategory_1.setPreferredSize(new Dimension (categoryTextFieldWidth, categoryHight));
 panelCategory.add(txtCategory_1);


 JButton btnCategory_2=new JButton("");
 btnCategory_2.setPreferredSize(new Dimension ( categoryButtonWidth, categoryHight));      
 btnCategory_2.setBackground(Color.YELLOW);
 btnCategory_2.addActionListener(this);
 panelCategory.add(btnCategory_2);

 JTextField txtCategory_2 = new JTextField( );
 txtCategory_1.setPreferredSize(new Dimension (categoryTextFieldWidth, categoryHight));
 panelCategory.add(txtCategory_2);

 this.add(panelCategory,  BorderLayout.NORTH);
 }

public void actionPerformed(ActionEvent e) {

    // Menu item actions
    String command = e.getActionCommand();      

    if (command.equals("Quit")) {
        System.exit(0);
    } else if (command.equals("Open")) {
        // Open menu item action
        JFileChooser fileChooser = new JFileChooser();     
        int returnVal = fileChooser.showOpenDialog(MainForm.this);
        if (returnVal ==  fileChooser.APPROVE_OPTION) {
            numInterFrame=numInterFrame+1;
            File file = fileChooser.getSelectedFile();
            fileList.add(file);
            AddNote(file);


            // Load file
        } else if (returnVal == JFileChooser.CANCEL_OPTION ) {
            // Do something else
        } 
    } 


     else if (command.equals("Save")) {
        // Save menu item action
        System.out.println("Save menu item clicked");
    }
}

private JMenuItem makeMenuItem(String name) {
    JMenuItem m = new JMenuItem(name);
    m.addActionListener(this);
    return m;
}

    private void AddNote(File file){        
        JInternalFrame internalFrame = new JInternalFrame("PDFAnnotation"
                + file.getName(), true, true, true, true);      
       internalFrame.setBounds(0, 0, 600, 100);
       desktop.add(internalFrame);    

       PDFPanel p=new PDFPanel();
       JPanel e =p.getJPanel(file);     
       internalFrame.add(e, BorderLayout.CENTER);            
       internalFrame.setVisible(true);
       this.add(desktop, BorderLayout.CENTER);

      //resize the internal frame as full screen  
      Dimension size = desktop.getSize();
      int w = size.width ; 
      int h = size.height ; 
      int x=0;
      int y=0;
      desktop.getDesktopManager().resizeFrame(internalFrame, x, y, w, h);
    }

}

1 个答案:

答案 0 :(得分:1)

不要使用setPreferredSize()方法。

所有Swing组件都有一个首选大小。

对于JTextField,您可以使用:

JTextField textField = new JTextField(5);

指示您希望一次显示多少个字符。

对于JButton,宽度由您添加到按钮的文本决定。

使用BoxLayout时,组件的宽度会增加以填充可用空间。

只需使用FlowLayout(这是JPanel的默认设置),组件将以其首选尺寸显示。