2015-10-11 80 views
0

我一直在四處搜索,找不到任何東西鎖定組件在窗口中的位置,而使用FlowLayout。我使用GridLayout,除了不喜歡它的外觀,我無法調整JTextArea的大小。我把窗戶上的所有東西都放在我想要的位置和正確的尺寸上,除非用戶調整窗口大小,然後所有的東西都會遍佈整個地方。圖形用戶界面FlowLayout,鎖定所有組件的位置

有什麼辦法可以鎖定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 

回答

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

請嘗試關注Hovercraftanswer

+0

完美的,因爲一切都在那裏我想它反正。謝謝! – NoobCoderChick

+0

哦,好的。我有一個GridLayout,但我無法準確獲得JTextArea的大小,並放置在我想要的位置,並且FlowLayout看起來很完美。這是一個班級任務,但我明白你的意思。 – NoobCoderChick

+0

@ sjud9227:設置尺寸是非常糟糕的做法。 –

4

強烈求與接受的答案不同,我看到了一些問題:

  • 通過一組尺寸鎖定,你可能會在所有平臺上顯示一個可怕的GUI(OS,顯示設置)除了你自己的。
  • 通過設置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(); 
     } 
     }); 
    } 
}