gpt4 book ai didi

java - 更改流程布局中的面板大小

转载 作者:行者123 更新时间:2023-12-01 18:18:42 25 4
gpt4 key购买 nike

我正在用java开发一个GUI表单。我在主框架中添加了两个具有 FlowLayout 的面板。但现在我想更改面板的大小,我尝试使用 setsize() 进行更改,但我看不出有什么区别。

这是我的代码:

public class Main_window extends JFrame implements ActionListener {

JFrame MainFrm = new JFrame("MCQ Generator");
JPanel LeftPanel = new JPanel();
JPanel RightPanel = new JPanel();
JButton BrowseButton = new JButton("Browse/Open");
TextArea ta1 = new TextArea();
TextArea ta2 = new TextArea();
JButton ClearWindow = new JButton("Clear Window");
JButton generateButton = new JButton("Generate MCQs");
String fname;

Main_window() {
MainFrm.setLayout(new FlowLayout(FlowLayout.LEFT));
MainFrm.setSize(1050, 400);
MainFrm.add(LeftPanel, FlowLayout.LEFT);
MainFrm.add(RightPanel);
LeftPanel.setLayout(new BorderLayout(30, 30));
//LeftPanel.setSize(10, 10);
//RightPanel.setSize(10, 10);
RightPanel.setLayout(new BorderLayout(40, 40));
LeftPanel.setOpaque(true);
LeftPanel.setBackground(Color.LIGHT_GRAY);
LeftPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
RightPanel.setOpaque(true);
RightPanel.setBackground(Color.LIGHT_GRAY);
RightPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JPanel InnerPanel = new JPanel();
InnerPanel.setLayout(new FlowLayout());
InnerPanel.setBackground(Color.LIGHT_GRAY);
InnerPanel.add(new JLabel("Choose File: "));
LeftPanel.add(InnerPanel, BorderLayout.NORTH);
LeftPanel.add(ta1, BorderLayout.CENTER);
RightPanel.add(new JLabel("Questions Generated:"), BorderLayout.NORTH);
RightPanel.add(ta2, BorderLayout.CENTER);
//ta1.setSize(10, 100);
//ta2.setSize(10, 100);
BrowseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Text Documents(*.txt)", "txt");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(LeftPanel);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You choose to open this file: " + chooser.getCurrentDirectory() + "\\" + chooser.getSelectedFile().getName());
fname = new String(chooser.getCurrentDirectory() + "\\" + chooser.getSelectedFile().getName());
try {
Jdbc_conn.truncateInputTable(ta1);
displayInput(fname);
//Jdbc_conn.truncateInputTable(ta1);
Give_input.getInput(fname);
} catch (Exception e) {
e.printStackTrace();
}
}
}

});
InnerPanel.add(BrowseButton);
LeftPanel.add(generateButton, BorderLayout.SOUTH);
generateButton.addActionListener(this);
RightPanel.add(ClearWindow, BorderLayout.SOUTH);
ClearWindow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
Jdbc_conn.truncateMcqAndNew_nountab(ta2);
} catch (Exception e) {
e.printStackTrace();
}
}
});
MainFrm.setVisible(true);
MainFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

最佳答案

通常,当 setSize() 不起作用时,请尝试使用 setPreferredSize()

在本例中,如 Oracle tutorial说:“FlowLayout 类将组件放在一行中,按其首选大小调整大小。如果容器中的水平空间太小而无法将所有组件放在一行中,则 FlowLayout 类使用多行。如果容器比一行组件所需的宽度宽,则默认情况下,该行在容器内水平居中。要指定该行向左或向右对齐,请使用 FlowLayout 构造函数采用对齐参数。FlowLayout 类的另一个构造函数指定组件周围放置多少垂直或水平填充。”

关于java - 更改流程布局中的面板大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28190127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com