GUI FlowLayout,锁定所有组件位置 [英] GUI FlowLayout, lock all components position
问题描述
我一直在四处寻找,但在使用 FlowLayout
时找不到任何关于在窗口中锁定组件位置的信息.我使用的是 GridLayout
,除了不喜欢它的外观,我无法调整 JTextArea
的大小.我把窗口上的所有东西都放在我想要的位置和正确的大小,除非用户调整窗口大小,然后一切都会到处都是.
I've been searching around and cannot find anything on locking the position of components in a window while using a FlowLayout
. I was using GridLayout
, besides not liking the look of it, I wasn't able to adjust the size of the JTextArea
. I got everything on the window exactly where I'd like it and the correct size unless the user resizes the window then everything goes all over the place.
有什么办法可以锁定 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
推荐答案
您可以使用 JFrame::setResizable(false)
来锁定正在调整大小的窗口.
You can use JFrame::setResizable(false)
to lock the window being resizable.
所以你的代码可能像
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);
}
这可能会解决您的这个问题
使用固定大小是一个非常非常糟糕的主意.尝试使用更灵活的布局可能会组合布局来实现您的目标.关于布局管理器,请参见此处.
Using fixed size is a very very bad idea. Try to use more flexible layout may be combination of layout to achieve you goal. About layout manager see here.
尝试遵循 答案气垫船-充满鳗鱼">气垫船 .
Try to follow the answer of Hovercraft .
这篇关于GUI FlowLayout,锁定所有组件位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!