swing手工编码GUI Java
我正在尝试使用Swing和AWT手工编写Java GUI。我正在使用各种布局来尝试实现或类似于下面发布的GUI(这是一个用铅笔制作的模拟布局):
到目前为止,我得到的是这个,但似乎不能使它更“礼貌”,更吸引人,更友好
这是我到目前为止所做的代码:
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");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout(0,0));
JPanel chPanel = new JPanel();
chPanel.setLayout(new GridLayout(3,2));
JPanel listPanel = new JPanel();
listPanel.setLayout(new GridLayout(3,2));
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new GridLayout(0,2));
JPanel addPanel = new JPanel();
addPanel.setLayout(new GridLayout(0,3));
mainPanel.add(chPanel, BorderLayout.LINE_START);
mainPanel.add(listPanel, BorderLayout.CENTER);
mainPanel.add(infoPanel, BorderLayout.LINE_END);
JTable chOneTable = new JTable();
JTable chTwoTable = new JTable();
JTable listTable = new JTable();
JLabel ch1Label = new JLabel("Channel 1");
JLabel ch2Label = new JLabel("Channel 2");
JLabel listLabel = new JLabel("List");
JButton rmvChOneButton = new JButton("Remove Channel");
JButton rmvChTwoButton = new JButton("Remove Channel");
chPanel.add(ch1Label);
chPanel.add(ch2Label);
chPanel.add(chOneTable);
chPanel.add(chTwoTable);
chPanel.add(rmvChOneButton);
chPanel.add(rmvChTwoButton);
listPanel.add(listLabel);
listPanel.add(listTable);
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 rentableLabel = new JLabel("Rentable");
JLabel synLabel = 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();
JButton btnAddProg = new JButton("Add Program");
JList channelList = new JList();
JList timeList = new JList();
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(rentableLabel);
infoPanel.add(rentCB);
infoPanel.add(synLabel);
infoPanel.add(txtSynopsis);
infoPanel.add(btnAddProg);
infoPanel.add(channelList);
infoPanel.add(timeList);
frame.add(mainPanel);
frame.setVisible(true);
}
}
它不必与上面显示的模拟布局完全相同,但要尽可能地相似,或者至少更加用户友好
我想使用GridBagLayout和SwingLayout以外的任何东西
关于如何改进代码并使其看起来更相似,有什么想法吗
感谢您的帮助
布莱恩
# 1 楼答案
使用Eclipse和WindowBuilder。您可以稍后返回并“手工编码”特定部分,如果愿意,仍然可以返回WindowBuilder
# 2 楼答案
GUI最需要的是:
JPanel
中,并将边框添加到面板中李>BorderLayout
的WEST
中有一组组件“推到顶部”,则将它们添加到另一个具有BorderLayout.NORTH
布局/约束的面板中。以下是我的意思。# 3 楼答案
看一看这个示例程序,这能满足您的需要吗:-)
我真的不知道你在
JButton
和'JSpinner'之间使用了什么,这就是为什么从来没有在那里添加过任何东西,希望你自己可以这样做以下是相同的输出:
# 4 楼答案
请看一下MigLayout。它的许可非常全面:
JNLP demo application应该会显示很好的示例和相应的源代码
另外,尽量避免嵌套逻辑上不相关的组件。随着嵌套程度的增加,对齐、边框和填充变得非常痛苦