GUI FlowLayout,锁定所有组件位置



我一直在搜索周围,无法找到任何在使用FlowLayout时锁定窗口中组件的位置。我使用GridLayout,除了不喜欢它的外观,我无法调整JTextArea的大小。我把窗口上的所有东西都放在了我想要的位置,并且都是正确的大小,除非用户重新调整窗口的大小,否则所有东西都会到处乱放。

是否有任何方法来锁定JTextArea/JButtons/JLabels,所以如果用户调整窗口的大小,他们不移动,或者甚至更好地锁定窗口,所以它不能被用户调整?

这是我尝试过的:

public class CopyFile extends JFrame{
private JFileChooser fc;
private JButton copyButton;
private JButton chooseFileButton;
private JButton destinationButton;
private File workingDirectory;
private JLabel sourceLabel;
private JTextArea displayCopyText;
private JLabel destinationLabel;
private JTextField sourceText;
private JLabel copyText;
private JTextField destinationText;
public static void main(String [] args) {
    CopyFile go = new CopyFile();
    go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    go.setSize(500, 150);
    go.setVisible(true);
}
public CopyFile() {
    super("Copy a text file");
    setLayout(new FlowLayout());
    fc = new JFileChooser();
    //Open dialog box inside project folder to make easier to find files
    workingDirectory = new File(System.getProperty("user.dir"));
    fc.setCurrentDirectory(workingDirectory);
    //create labels and buttons for window
    chooseFileButton = new JButton("CHOOSE SOURCE FILE");
    destinationButton = new JButton("DESTINATION FOLDER");
    copyButton = new JButton("COPY FILE");      
    sourceLabel = new JLabel("SOURCE FILE: ", JLabel.CENTER);
    sourceText = new JTextField(15);
    sourceText.setEditable(false);
    destinationText = new JTextField(15);
    destinationText.setEditable(false); 
    destinationLabel = new JLabel("DESTINATION:", JLabel.CENTER);
    //JScrollPane SP = new JScrollPane();       
    displayCopyText = new JTextArea();
    displayCopyText.setPreferredSize(new Dimension(300, 50));
    displayCopyText.setRows(2);
    displayCopyText.setLineWrap(true);
    displayCopyText.setWrapStyleWord(true);
    displayCopyText.setEditable(false);     

    //add everything to JFrame  
    add(sourceLabel);
    add(sourceText);
    add(chooseFileButton);  
    add(destinationLabel);
    add(destinationText);
    add(destinationButton);
    //add(copyText);
    add(displayCopyText);
    add(copyButton);
    //Create TheHandler object to add action listeners for the buttons.
    TheHandler handler = new TheHandler();
    chooseFileButton.addActionListener(handler);
    destinationButton.addActionListener(handler);
    copyButton.addActionListener(handler);
}
//Inner class to create action listeners    
private class TheHandler implements ActionListener {
    private File selectedDestinationFile;
    private File selectedSourceFile;
    private int returnVal;
    public void actionPerformed(ActionEvent event) {

        //Selecting a source file and displaying what the user is doing.
        if(event.getSource() == chooseFileButton) {     
            returnVal = fc.showOpenDialog(null);
            //Set the path for the source file. 
            if(returnVal == JFileChooser.APPROVE_OPTION) {  
                selectedSourceFile = fc.getSelectedFile();
                sourceText.setText(selectedSourceFile.getName());   
            }       
        }//end if
        //Handle destination button.
        if(event.getSource() == destinationButton) {
            returnVal = fc.showSaveDialog(null);
            if(returnVal == JFileChooser.APPROVE_OPTION) {
                selectedDestinationFile = fc.getSelectedFile();
                destinationText.setText(fc.getSelectedFile().getAbsolutePath());    
            }               
        }//end if
        //Handle copy button
        if(event.getSource() == copyButton) {
            Path sourcePath = selectedSourceFile.toPath();
            Path destinationPath = selectedDestinationFile.toPath();        
            try {
                Files.copy(sourcePath,  destinationPath);
            } catch (IOException e) {
                e.printStackTrace();
            }   
            if(returnVal == JFileChooser.APPROVE_OPTION) {      
                displayCopyText.append("SUCCESSFULLY COPIED:n" 
                                + selectedDestinationFile.getName());   
            }
            else {
                displayCopyText.append("COPY WAS CANCELED BY USER.n");
            }   
        }//end if
    }//end actionPerformed      
}//end TheHandler class
}//end class

强烈不同意公认的答案,我看到了一些问题:

    通过锁定一个固定的大小,你可能会在所有平台(操作系统,显示设置)上显示一个糟糕的GUI,而不是你自己的。
  • 通过设置JTextAreas显示的大小,你可以完全阻止它在JScrollPane中被滚动。
  • 通过强迫你的GUI显示一个相对"愚蠢"的布局管理器,FlowLayout,你限制了你的布局选项。
  • 通过设置任何组件的大小,您可以人为地限制其大小,防止布局管理器和组件本身为该平台的组件设置最佳大小。

我建议:

    相反,你应该设置JTextArea的显示行和列
  • 在JScrollPane中显示JTextArea
  • 使用更灵活的布局管理器,或者通常更好的布局组合,通常由嵌套的jpanel使用,每个使用自己的布局管理器
  • 避免设置大小,而是在添加所有组件后在顶层窗口调用pack(),从而让布局和组件自行优化大小。
例如:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
@SuppressWarnings("serial")
public class CopyFile2 extends JPanel {
   private static final int ROWS = 3;
   private static final int COLS = 20;
   private static final int GBC_I = 4;
   private static final Insets INSETS = new Insets(GBC_I, GBC_I, GBC_I, GBC_I);
   private JTextField sourceField = new JTextField(10);
   private JTextField destField = new JTextField(10);
   private JTextArea displayCopyText = new JTextArea(ROWS, COLS);
   public CopyFile2() {
      setLayout(new GridBagLayout());
      add(new JLabel("Source File:"), createGbc(0, 0));
      add(sourceField, createGbc(1, 0));
      add(new JButton("Choose Source File"), createGbc(2, 0));
      add(new JLabel("Destination:"), createGbc(0, 1));
      add(destField, createGbc(1, 1));
      add(new JButton("Destination Folder"), createGbc(2, 1));
      GridBagConstraints gbc = createGbc(0, 2);
      gbc.gridwidth = 2;
      add(new JScrollPane(displayCopyText), gbc);
      add(new JButton("Copy File"), createGbc(2, 2));      
   }
   private GridBagConstraints createGbc(int x, int y) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.insets = INSETS;
      return gbc;
   }
   private static void createAndShowGUI() {
      CopyFile2 paintEg = new CopyFile2();
      JFrame frame = new JFrame("CopyFile2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(paintEg);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}

您可以使用JFrame::setResizable(false)来锁定可调整大小的窗口。

所以你的代码可能像
public static void main(String[] args) {
        CopyFile go = new CopyFile();
        go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        go.setResizable(false);   //No resize is possible
       go.setSize(500, 150);
       go.setVisible(true);
}

这可能会解决你的这个问题

使用固定大小是一个非常非常糟糕的主意。尽量使用更灵活的布局,可能是组合布局来达到你的目标。关于布局管理器,请参见这里。

尝试遵循气垫船的答案

相关内容

  • 没有找到相关文章

最新更新