我正在尝试向Panel添加几个JTextFields,JButtons和JLabel,但只显示按钮。我已经为我的JPanel设置了布局为null,并且我为每个项目设置了位置/边界。
frame= new JFrame("Find and Replace");
JPanel panel = new JPanel(null);
JLabel findLabel = new JLabel("Find:");
findLabel.setLocation(0, 20);
JLabel replaceLabel = new JLabel("Replace with:");
replaceLabel.setLocation(50, 60);
findField = new JTextField(20);
findField.setLocation(30, 100);
replaceField = new JTextField(20);
replaceField.setLocation(220, 140);
replaceAllButton = new JButton("Find and Replace All");
replaceAllButton.setBounds(20, 200, 100, 25);
replaceButton = new JButton("Replace");
replaceButton.setBounds(120, 200, 100, 25);
panel.add(findLabel);
panel.add(findField);
panel.add(replaceLabel);
panel.add(replaceField);
panel.add(replaceButton);
panel.add(replaceAllButton);
答案 0 :(得分:1)
您可以在此处使用GridBagLayout而不是使用null布局:
GridBagConstraints gridBagConstraints;
panel = new JPanel();
findLabel = new javax.swing.JLabel();
findField = new javax.swing.JTextField();
replaceLabel = new javax.swing.JLabel();
replaceField = new javax.swing.JTextField();
replaceAllButton = new javax.swing.JButton();
replaceButton = new javax.swing.JButton();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
panel.setLayout(new GridBagLayout());
findLabel.setText("Find :");
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.ipadx = 94;
gridBagConstraints.ipady = 30;
panel.add(findLabel, gridBagConstraints);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.ipadx = 149;
panel.add(findField, gridBagConstraints);
replaceLabel.setText("Replace With :");
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.ipadx = 34;
gridBagConstraints.ipady = 23;
panel.add(replaceLabel, gridBagConstraints);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.ipadx = 149;
panel.add(replaceField, gridBagConstraints);
replaceAllButton.setText("Find And Replace All");
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.ipady = 9;
panel.add(replaceAllButton, gridBagConstraints);
replaceButton.setText("Replace");
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 2;
gridBagConstraints.ipadx = 99;
gridBagConstraints.ipady = 8;
panel.add(replaceButton, gridBagConstraints);
//adding panel to the jframe
getContentPane().add(panel, BorderLayout.CENTER);
pack();
答案 1 :(得分:0)
您已将所有对象添加到面板但忘记添加面板到框架(JFrame的对象)
将此行添加到您的代码中
frame.add(panel);