Swing - 文本输入和输出到textarea

时间:2013-09-02 05:37:37

标签: java swing jtable jtextarea

我想用Swing制作简单的GUI,它有2个输入文本字段和一个按钮。

根据输入,该程序将从表中提取name / rol-no / age / emailid,并需要在同一屏幕中显示下面的文本区域。文本区域应该是可滚动的。我希望输入/输出在同一个屏幕上。有人可以告诉哪种布局是理想的吗?

---------------------------------------------------------------------------
School(label1):textfield1____
class(label2):textfield2____
SUbmit(Button1)

      name(label2)     roll-no(label3)      age(label4)   email-id(label5) 
row-1  -----
row-2  ----
row-3  ----
row-4  ----
.
.
row-nth ---
----------------------------------------------------------------------------

您好
尝试下面的代码..但表positreinc incorreclty.Please帮助。 公共课堂考试{

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    final JFrame f = new JFrame();
    f.setSize(1000, 1000);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setResizable(true);

    final JPanel pnl = new JPanel(new GridBagLayout());
    final GridBagConstraints c = new GridBagConstraints();

    JLabel lbl = new JLabel("School ");
    c.gridx = 0;
    c.gridy = 0;
    pnl.add(lbl, c);

    final JTextField tf = new JTextField(20);
    c.gridx = 1;
    c.gridy = 0;
    tf.setText("");
    pnl.add(tf, c);

    JLabel lb2 = new JLabel("Class");
    c.gridx = 0;
    c.gridy = 1;
    pnl.add(lb2, c);

    final JTextField tf2 = new JTextField(20);
    c.gridx = 1;
    c.gridy = 1;
    tf2.setText("");
    pnl.add(tf2, c);

    final String[] columnNames = { "name", "roll-no", "age", "email-id" };
    Object[][] columnData = new String[2][4];
    columnData[0][0] = "X";
    columnData[0][1] = "1";
    columnData[0][2] = "22";
    columnData[0][3] = "X@XMAIL.COM";
    columnData[1][0] = "Y";
    columnData[1][1] = "2";
    columnData[1][2] = "24";
    columnData[1][3] = "Y@XMAIL.COM";
    JTable table = new JTable(columnData, columnNames);
    table.setBounds(300, 600, 700, 150);

    JScrollPane scrollpane = new JScrollPane(table);
    pnl.add(scrollpane);

    f.getContentPane().add(BorderLayout.NORTH, pnl);
    f.setVisible(true);

}

}

3 个答案:

答案 0 :(得分:1)

我会先使用JPanel使用BorderLayout,暂时将其称为content ...

然后我会使用JPanel创建一个GridBagLayout并将字段添加到其中,暂时调用此form ...

我会将form添加到content位置的BorderLayout.NORTH

然后我会在JTable中将JScrollPane添加到BorderLayout.CENTER的{​​{1}}位置

enter image description here

首先看看......

了解更多详情......

根据更新后的问题更新了示例

你的意思更像......

enter image description here

content

不是将所有字段和表格添加到同一个面板,而是使用“主”面板,该面板使用import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class TestLayout30 { public static void main(String[] args) { new TestLayout30(); } public TestLayout30() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new MainPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class MainPane extends JPanel { public MainPane() { setLayout(new BorderLayout()); JPanel pnl = new JPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); JLabel lbl = new JLabel("School "); c.gridx = 0; c.gridy = 0; c.anchor = GridBagConstraints.WEST; pnl.add(lbl, c); final JTextField tf = new JTextField(20); c.gridx = 1; c.gridy = 0; c.weightx = 1; tf.setText(""); pnl.add(tf, c); JLabel lb2 = new JLabel("Class"); c.gridx = 0; c.gridy = 1; c.weightx = 0; pnl.add(lb2, c); final JTextField tf2 = new JTextField(20); c.gridx = 1; c.gridy = 1; c.weightx = 1; tf2.setText(""); pnl.add(tf2, c); add(pnl, BorderLayout.NORTH); final String[] columnNames = {"name", "roll-no", "age", "email-id"}; Object[][] columnData = new String[2][3]; columnData[0][0] = "X"; columnData[0][4] = "1"; columnData[0][5] = "22"; columnData[0][6] = "X@XMAIL.COM"; columnData[1][0] = "Y"; columnData[1][7] = "2"; columnData[1][8] = "24"; columnData[1][9] = "Y@XMAIL.COM"; JTable table = new JTable(columnData, columnNames); table.setBounds(300, 600, 700, 150); JScrollPane scrollpane = new JScrollPane(table); add(scrollpane); } } } 并将字段(在单独的面板上)添加到BorderLayout位置,表格到NORTH位置。这通常称为复合布局。它允许您设计复杂的布局,其中包括尝试将所有内容融入单个组件的所有复杂性......

您还可以使用CENTER GridBagConstraintsweightx = 1weighty = 1fill = GridBagConstraints.BOTH添加表格,但这只会变得混乱...

已更新为可选但不可编辑的示例

默认情况下,gridwidth = GridBagConstraints.REMAINDER是可编辑的。更改它的唯一方法是覆盖DefaultTableModel方法并将其设为isCellEditable return

enter image description here

false

另外,避免使用import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.DefaultComboBoxModel; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.table.DefaultTableModel; public class TestLayout30 { public static void main(String[] args) { new TestLayout30(); } public TestLayout30() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new MainPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class MainPane extends JPanel { public MainPane() { setLayout(new BorderLayout()); JPanel pnl = new JPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); JLabel lbl = new JLabel("School "); c.gridx = 0; c.gridy = 0; c.anchor = GridBagConstraints.WEST; pnl.add(lbl, c); final JTextField tf = new JTextField(20); c.gridx = 1; c.gridy = 0; c.weightx = 1; tf.setText(""); pnl.add(tf, c); JLabel lb2 = new JLabel("Class"); c.gridx = 0; c.gridy = 1; c.weightx = 0; pnl.add(lb2, c); final JTextField tf2 = new JTextField(20); c.gridx = 1; c.gridy = 1; c.weightx = 1; tf2.setText(""); pnl.add(tf2, c); add(pnl, BorderLayout.NORTH); final String[] columnNames = {"name", "roll-no", "age", "email-id"}; DefaultTableModel model = new DefaultTableModel(columnNames, 0) { @Override public boolean isCellEditable(int row, int column) { return false; } }; Object[][] rowData = new String[2][11]; rowData[0][0] = "X"; rowData[0][12] = "1"; rowData[0][13] = "22"; rowData[0][14] = "X@XMAIL.COM"; rowData[1][0] = "Y"; rowData[1][15] = "2"; rowData[1][16] = "24"; rowData[1][17] = "Y@XMAIL.COM"; for (Object[] row : rowData) { model.addRow(row); } JTable table = new JTable(model); JScrollPane scrollpane = new JScrollPane(table); add(scrollpane); } } } ,它只会产生更多问题然后解决......

答案 1 :(得分:0)

您可以使用TableLayout或GridBagLayout。以下是两者的链接。
1. GridBagLayout
2. TableLayout

答案 2 :(得分:0)

我厌倦了Java复杂的布局管理器,这些管理器没有多大意义并转移到:

http://www.miglayout.com/

易于安装,10~20分钟即可掌握所有基础知识,它是天堂,让我的设计变得更加轻松。 我推荐它比其他布局管理器。