【发布时间】:2017-05-06 04:27:19
【问题描述】:
所以我正在做一个小型 Java Punnett Square 项目(提出了一个问题,该问题提供了更多关于它到底做什么的细节)。在问完最后一个问题后,我决定开始重写程序,因为它非常混乱和混乱。这是我想要的格式:
这是我的输出截图:
文本字段不在标签的右侧,我的计算按钮没有显示我的表格。
代码如下:
//imports
public class GeneticsGUI
{
public static void main(String[] args)
{
JFrame window = new JFrame("Genetics");
JPanel panel = new JPanel(new GridLayout(25,2));
JLabel o = new JLabel("Options:");
panel.add(o);
String[] options = new String[] {"2x2 Punnett Square", "4x4 Punnett Square"};
JComboBox<String> list = new JComboBox<>(options);
panel.add(list);
String selected = (String) list.getSelectedItem();
JLabel p1 = new JLabel("Parent 1:");
JTextField par1 = new JTextField();
JLabel p2 = new JLabel("Parent 1:");
JTextField par2 = new JTextField();
panel.add(p1,BorderLayout.WEST);
panel.add(par1,BorderLayout.CENTER);
panel.add(p2,BorderLayout.WEST);
panel.add(par2,BorderLayout.CENTER);
JButton submit = new JButton("Calculate");
submit.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==submit)
{
String p1 = par1.getText();
String p2 = par2.getText();
if(selected == "2x2 Punnett Square")
{
String[][] mono = Genetics.monohybridPunett(p1, p2);
for(int row = 0; row<2 ; row++)
{
for(int col = 0; col<2; col++)
{
mono[row][col] = " "+ mono[row][col];
}
}
JTable table = new JTable(mono.length,mono.length);
table.setShowGrid(true);
for(int i = 0; i<mono.length; i++)
for(int j = 0; j<mono.length; j++)
table.setValueAt(mono[i][j], i, j);
DefaultTableCellRenderer t = new DefaultTableCellRenderer();
t.setHorizontalTextPosition(DefaultTableCellRenderer.CENTER);
t.setVerticalTextPosition(DefaultTableCellRenderer.CENTER);
table.setSize(300, 300);
table.isCellEditable(0,0);
table.setRowHeight(100);
panel.add(table,BorderLayout.EAST);;
panel.revalidate();
}
else if(selected == "4x4 Punnett Square")
{
String[][] di = Genetics.dihybridPunett(p1, p2);
JTable table = new JTable(di.length,di.length);
table.setShowGrid(true);
DefaultTableModel model = new DefaultTableModel(di,new Object[]{"AaBb","AaBb","",""});
table.isCellEditable(0, 0);
table.setModel(model);
table.setRowHeight(100);
table.getColumnModel().getColumn(0).setPreferredWidth(100);
table.getColumnModel().getColumn(1).setPreferredWidth(100);
table.getColumnModel().getColumn(2).setPreferredWidth(100);
table.getColumnModel().getColumn(3).setPreferredWidth(100);
panel.add(table,BorderLayout.CENTER);
panel.revalidate();
}
}
}
});
panel.add(submit);
window.add(panel);
window.setSize(800, 800);
window.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
window.setVisible(true);
}
}
再说一次,我并不是 GUI 或 Swing 方面的专家,所以想知道我需要做些什么来解决这个格式问题。
编辑:
这是我当前的 GridBagLayout 实现代码。
JFrame window = new JFrame("Genetics");
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel o = new JLabel("Options:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 0;
panel.add(o, c);
String[] options = new String[] {"2x2 Punnett Square", "4x4 Punnett Square"};
JComboBox<String> list = new JComboBox<>(options);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.gridx = 1;
c.gridy = 0;
panel.add(list, c);
String selected = (String) list.getSelectedItem();
JLabel p1 = new JLabel("Parent 1:");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 1;
c.insets = new Insets(40,0,0,0);
c.gridx = 0;
c.gridy = 1;
panel.add(p1, c);
JTextField par1 = new JTextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
panel.add(par1, c);
JLabel p2 = new JLabel("Parent 2:");
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,0,0,0);
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 2;
panel.add(p2, c);
JTextField par2 = new JTextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 2;
panel.add(par2, c);
JButton submit = new JButton("Calculate");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 3;
c.anchor = GridBagConstraints.LINE_START;
panel.add(submit, c);
【问题讨论】:
-
顺便说一句...
selected == "2x2 Punnett Square"不是how to compare strings -
将 JPanel 的布局设置为 GridLayout,然后在向 JPanel 添加组件时使用 BorderLayout 常量是没有意义的,这表明您忽略了先查看教程。要点 1)查看教程,2)嵌套 JPanel,每个 JPanel 都使用自己的布局管理器,以实现您的目标。
-
对顶部的 JPanel 使用 GridBagLayout,其中包含标签和 JTextFields,对整个 GUI 使用 BorderLayout,添加顶部的 JPanel BorderLayout.PAGE_START。在主 JPanel、BorderLayout.CENTER\ 中添加图表或 JTable,或其他任何内容(可能在 JScrollPane 中)
-
This link 展示了我如何处理 GridBagLayout GUI。
标签: java swing user-interface