GUI FlowLayout,锁定所有组件位置

时间:2015-10-11 00:01:44

标签: java swing user-interface resize flowlayout

我一直在搜索,在使用FlowLayout时无法找到锁定窗口中组件位置的任何内容。我正在使用GridLayout,除了不喜欢它的外观之外,我无法调整JTextArea的大小。我把窗户上的所有东西都准确地放在了我想要的位置和正确的尺寸,除非用户调整窗口大小,然后一切都遍布整个地方。

有没有办法锁定JTextArea / JButtons / JLabel所以如果用户调整窗口大小,他们不会移动,或者甚至可能更好地锁定窗口,以便用户无法调整?

以下是我的尝试:

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

2 个答案:

答案 0 :(得分:4)

强烈请求与接受的答案不同,我看到一些问题:

  • 通过锁定设定的大小,您可能会冒险在所有平台(操作系统,显示设置)中显示一个非常糟糕的GUI。
  • 通过设置JTextAreas显示的大小,可以完全防止它在JScrollPane中可滚动。
  • 通过强制使用相对“哑”布局管理器FlowLayout显示GUI,可以限制布局选项。
  • 通过设置任何组件的大小,您可以人为地限制其大小,从而防止布局管理器和组件本身为该平台的组件设置最佳大小。

相反,我建议:

  • 相反,您应该设置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();
         }
      });
   }
}

答案 1 :(得分:3)

您可以使用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);
}

这可以解决您的问题

使用固定大小是一个非常糟糕的主意。尝试使用更灵活的布局可能会结合布局来实现你的目标。关于布局管理器,请参阅here

尝试关注answerHovercraft