【问题标题】:Set size on component in JPanel在 JPanel 中设置组件的大小
【发布时间】:2014-04-29 12:15:13
【问题描述】:

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

这是我的代码:

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);
    }

}

【问题讨论】:

  • 您是否尝试过为要调整大小的组件覆盖getPreferredSize()?虽然,如果您希望所有组件具有相同的大小,只需将它们放在 GridLayout
  • 1) “设置JTextField和JButton的大小”哪个文本域?哪个按钮?为了尽快获得更好的帮助,请发布MCTaRE(最小完整测试和可读示例)。 ..
  • .. 2) Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置或调整大小。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。其他改变大小的方法:JTextField a) 列数。 b) 字体大小。 JButton a) 字体或图标大小。 b) 插图/边距。
  • @xav 按钮和文本字段的行从左到右显示在框架的 2/3 宽度上。我该怎么做?
  • @user819774:对不起,我不明白...

标签: java swing jbutton jtextfield layout-manager


【解决方案1】:

不要使用setPreferredSize() 方法。

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

对于 JTextField,您可以使用:

JTextField textField = new JTextField(5);

指示一次要显示多少个字符。

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

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

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

【讨论】:

  • 我怎样才能使按钮和文本字段的形式从左到右。我添加了这个代码 panelCategory.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT ),但所有按钮和文本字段都在中心。
  • 我将 setPreferredSize() 替换为 setSize 并修改了按钮,如 JButton btnCategory_2=new JButton(" ")。它没有显示我想要的高度。我希望文本字段和按钮具有相同的高度。另外,我怎样才能使按钮和文本字段的形式从左到右。我添加了这个代码 panelCategory.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT ),但所有按钮和文本字段都在中心。感谢您的帮助
  • ... 你可以只使用空布局并调用 ElementName.setBounds(x,y,w,h);
  • @clockw0rk,Swing 旨在与布局管理器一起使用。不要使用空布局!!!
猜你喜欢
  • 1970-01-01
  • 2013-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
  • 2021-01-16
  • 1970-01-01
  • 2012-09-01
相关资源
最近更新 更多