gpt4 book ai didi

java - 如何在 Java Swing 中编写此 GUI

转载 作者:搜寻专家 更新时间:2023-11-01 01:13:29 27 4
gpt4 key购买 nike

我目前正在使用以下 GUI 开发一个小型实用程序:

Sample GUI

现在我有一个带有 BorderLayout 布局的容器 (JPanel),可以将所有内容固定到位。我在 BorderLayout.NORTH 和 BorderLayout.SOUTH 上分别放置了另外 2 个 JPanel,每个都有 GridLayout(2 列乘 1 行)。该表位于位于其中心的主容器上。

您认为这是最好的方法吗?我很难处理组件和框架边框之间的间距。

现在我有这段代码:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

public class GUI extends JFrame {

private JButton loadFileBtn = new JButton("Load File");
private JButton generateReportBtn = new JButton("Generate Report");
private JButton exitBtn = new JButton("Exit");
private JLabel fileNameLbl = new JLabel("File Name Here");
private JMenuBar menuBar = new JMenuBar();
private JMenu fileMI = new JMenu("File");
private JMenuItem openFileMenu = new JMenuItem("Open File");
private JSeparator separator = new JSeparator();
private JMenuItem exitMenu = new JMenuItem("Exit");
private JMenu reportMI = new JMenu("Report");
private JMenuItem generateReportMenu = new JMenuItem("Generate Report");
private JMenu helpMI = new JMenu("Help");
private JMenuItem aboutMenu = new JMenuItem("About");
private JTable table = new JTable(5, 2);
private JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
private JPanel panel1 = new JPanel(new BorderLayout());
private JPanel panel2 = new JPanel(new GridLayout(1, 2));
private JPanel panel3 = new JPanel(new GridLayout(1, 2));

public GUI() {

super("Sample GUI");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(300, 300));
setResizable(false);

setLayout(new BorderLayout(10, 10));

fileMI.add(openFileMenu);
fileMI.add(separator);
fileMI.add(exitMenu);

reportMI.add(generateReportMenu);

helpMI.add(aboutMenu);

menuBar.add(fileMI);
menuBar.add(reportMI);
menuBar.add(helpMI);

setJMenuBar(menuBar);

panel1.add(table, BorderLayout.CENTER);

panel2.add(fileNameLbl);
panel2.add(loadFileBtn);

panel3.add(generateReportBtn);
panel3.add(exitBtn);

mainPanel.add(panel2, BorderLayout.NORTH);
mainPanel.add(panel1, BorderLayout.CENTER);
mainPanel.add(panel3, BorderLayout.SOUTH);

add(mainPanel);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GUI app = new GUI();
app.setVisible(true);
}
});
}
}

您认为最好的方法是什么?

如有任何帮助,我们将不胜感激。谢谢。


更新:

现在,我有以下 GUI

enter image description here

我希望组件与边界均匀隔开,就像在模型中一样。

最佳答案

您可以使用 2 件事来实现这一目标:

  1. 使用 BorderFactory.createEmptyBorder(int, int, int, int)
  2. 使用 GridLayout 的 4-args 构造函数>

还有其他 LayoutManager 可以带来相同的功能(如 GridBagLayout,或使用嵌套的 BorderLayout),但如果您对当前的 LayoutManager's,没有迫切需要更改为那些。你的做法也是可以接受的。

enter image description here

您可以考虑将表格包装在 JScrollPane 中以使其更好,如果需要,还可以添加标题和滚动条。

小示例代码:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

public class GUI extends JFrame {

private JButton loadFileBtn = new JButton("Load File");
private JButton generateReportBtn = new JButton("Generate Report");
private JButton exitBtn = new JButton("Exit");
private JLabel fileNameLbl = new JLabel("File Name Here");
private JMenuBar menuBar = new JMenuBar();
private JMenu fileMI = new JMenu("File");
private JMenuItem openFileMenu = new JMenuItem("Open File");
private JSeparator separator = new JSeparator();
private JMenuItem exitMenu = new JMenuItem("Exit");
private JMenu reportMI = new JMenu("Report");
private JMenuItem generateReportMenu = new JMenuItem("Generate Report");
private JMenu helpMI = new JMenu("Help");
private JMenuItem aboutMenu = new JMenuItem("About");
private JTable table = new JTable(5, 2);
private JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
private JPanel panel1 = new JPanel(new BorderLayout());
private JPanel panel2 = new JPanel(new GridLayout(1, 2, 10, 10));
private JPanel panel3 = new JPanel(new GridLayout(1, 2, 10, 10));

public GUI() {

super("Sample GUI");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(300, 300));
setResizable(false);

setLayout(new BorderLayout(10, 10));

fileMI.add(openFileMenu);
fileMI.add(separator);
fileMI.add(exitMenu);

reportMI.add(generateReportMenu);

helpMI.add(aboutMenu);

menuBar.add(fileMI);
menuBar.add(reportMI);
menuBar.add(helpMI);

setJMenuBar(menuBar);

panel1.add(table, BorderLayout.CENTER);

panel2.add(fileNameLbl);
panel2.add(loadFileBtn);

panel3.add(generateReportBtn);
panel3.add(exitBtn);

mainPanel.add(panel2, BorderLayout.NORTH);
mainPanel.add(panel1, BorderLayout.CENTER);
mainPanel.add(panel3, BorderLayout.SOUTH);
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
add(mainPanel);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
GUI app = new GUI();
app.setVisible(true);
}
});
}
}

关于java - 如何在 Java Swing 中编写此 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15363706/

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