gpt4 book ai didi

java - Swing/AWT - 以编程方式创建 GUI

转载 作者:搜寻专家 更新时间:2023-10-31 20:22:32 25 4
gpt4 key购买 nike

我正在尝试通过手动编码(不使用 GUI 工具)使用 Java 创建 GUI。

我正在尝试创建如下图所示的内容,但结果并不如我所愿。 (它是由一个名为 Pencil 的模型应用程序创建的)

enter image description here

这是目前的代码:

import java.awt.*; 
import javax.swing.*;
import javax.swing.JTable;


public class GUI extends JFrame {

public void buildGui() {


JFrame frame = new JFrame("Hotel TV Scheduler");

Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());

JPanel listPanel = new JPanel();
listPanel.setLayout(new FlowLayout());

JTable chOneTable = new JTable();
JTable chTwoTable = new JTable();
JTable listTable = new JTable();

JButton rmvChOneButton = new JButton("Remove Channel");
JButton rmvChTwoButton = new JButton("Remove Channel");

listPanel.add(chOneTable);
listPanel.add(chTwoTable);
listPanel.add(listTable);
listPanel.add(rmvChOneButton);
listPanel.add(rmvChTwoButton);

JPanel infoPanel = new JPanel();
infoPanel.setLayout(new GridLayout());

JLabel titleLabel = new JLabel("Title");
JLabel genreLabel = new JLabel("Genre");
JLabel durationLabel = new JLabel("Duration");
JLabel actorLabel = new JLabel("Actor");
JLabel directorLabel = new JLabel("Director");
JLabel rentLabel = new JLabel("Rentable");
JLabel synopsisLabel = new JLabel("Synopsis");

JTextField txtTitle = new JTextField();
JTextField txtGenre = new JTextField();
JTextField txtDuration = new JTextField();
JTextField txtActor = new JTextField();
JTextField txtDirector = new JTextField();
JTextField txtSynopsis = new JTextField();

JCheckBox rentCB = new JCheckBox();

infoPanel.add(titleLabel);
infoPanel.add(txtTitle);
infoPanel.add(genreLabel);
infoPanel.add(txtGenre);
infoPanel.add(durationLabel);
infoPanel.add(txtDuration);
infoPanel.add(actorLabel);
infoPanel.add(txtActor);
infoPanel.add(directorLabel);
infoPanel.add(txtDirector);
infoPanel.add(rentLabel);
infoPanel.add(rentCB);
infoPanel.add(synopsisLabel);
infoPanel.add(txtSynopsis);


contentPane.add(listPanel);
contentPane.add(infoPanel);

frame.setVisible(true);
}
}

知道我可以使用哪些布局来创建 GUI 设置或如何通过编码实现它吗?

最佳答案

检查 A Visual Guide to Layout Managers .这将帮助您进行布局选择。

另外,考虑在面板中使用面板。每个都有所需的布局,以实现您需要的外观。如果没有 GUI 构建工具,您将需要编译/运行/编译/运行……以查看布局情况。

关于java - Swing/AWT - 以编程方式创建 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9999437/

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