Components Not Showing
Hi guys...
I am quite new to Java and am wondering whether someone could please point me in the right direction. I have the following code:publicclass BackupDBextends JFrame{
protected JTextField userNameTextField =new JTextField("admin");
privatestatic Logger log = Logger.getLogger(BackupDB.class);
public BackupDB(){
super("Backup");
JPanel p =new JPanel(new BorderLayout());
p.setBorder(new EmptyBorder(5, 5, 5, 5));
p.add(new JLabel("UserName:"));
p.add(userNameTextField);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setSize(dim.width, dim.height);
setVisible(true);
}
publicstaticvoid main(String[] args){
new BackupDB();
}
The problem is that I cannot get any of the components to show on the JPanel - all I get is a grey panel. I am sure that the solution is simple but I cannot see the wood for the trees.
Thanks in advance.
Ota
[1791 byte] By [
Otacustesa] at [2007-11-27 8:39:42]

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BackupDB extends JFrame {
// done this way to make the jtextfield larger
protected JTextField userNameTextField = new JTextField(25);
//private static Logger log = Logger.getLogger(BackupDB.class);
public BackupDB() {
super("Backup");
JPanel p = new JPanel(new BorderLayout(10,10));
//p.setBorder(new EmptyBorder(5, 5, 5, 5));
p.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15)); // use factory here
p.add(new JLabel("UserName:"), BorderLayout.WEST); // have to specify location in borderlayout
p.add(userNameTextField, BorderLayout.EAST); // again, specify borderlayout location
userNameTextField.setText("admin");
getContentPane().add(p); // add your panel to jframe here!!!
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
//setSize(dim.width, dim.height); // not sure about this one?
pack();
setVisible(true);
}
public static void main(String[] args) {
new BackupDB();
}
}
Thanks petes1234 that was most helpful.
I do have another question. I have modified the code so that it includes a few buttons:// done this way to make the jtextfield larger
protected JTextField userNameTextField = new JTextField(25);
protected JButton one = new JButton("1");
protected JButton two = new JButton("2");
protected JButton three = new JButton("3");
protected JButton four = new JButton("4");
protected JButton five = new JButton("5");
public BackupDB() {
super("Backup");
JPanel p = new JPanel(new BorderLayout(10,10));
p.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // use factory here
p.add(new JLabel("UserName:"), BorderLayout.WEST); // have to specify location in borderlayout
p.add(userNameTextField, BorderLayout.EAST); // again, specify borderlayout location
p.setMaximumSize(new Dimension(250,25));
JPanel jp = new JPanel(new GridLayout(4,4,3,3));
jp.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // use factory here
jp.add(one);
jp.add(two);
jp.add(three);
jp.add(four);
jp.add(five);
userNameTextField.setText("admin");
getContentPane().add(p,BorderLayout.WEST); // add your panel to jframe here!!!
getContentPane().add(jp,BorderLayout.EAST); // add your panel to jframe here!!!
pack();
setVisible(true);
}
How do you change the height of the text box so that it does not "expand" to the same height as the text button panel?
Thanks
Ota