I have simple problem as I am not much familiar with Java GUI. I am trying to make visible the JLable on the below code as I find it hard to understand the concept. But still label is not visible but the frame do open on run time.
public class Sample extends JPanel {
public void Sample() {
JPanel p = new JPanel();
JLabel lab1 = new JLabel("User Name", JLabel.LEFT);
p.setLayout(new FlowLayout());
p.add(lab1 = new JLabel("add JLabel"));
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new Sample());
frame.setDefaultCloseOperation(JFrame.开发者_开发百科EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
You are forgot to add panel p
to sample. Either use add(p)
at the end or just remove panel p
cause your sample class is extending JPanel.
Option 1:
JPanel p = new JPanel();
JLabel lab1 = new JLabel("User Name", JLabel.LEFT);
p.setLayout(new FlowLayout());
p.add(lab1 = new JLabel("add JLabel"));
add(p);
option 2:
JLabel lab1 = new JLabel("User Name", JLabel.LEFT);
setLayout(new FlowLayout());
add(lab1 = new JLabel("add JLabel"));
Also why are you overriding initialization of JLabel? In your code JLable will always hold value "add JLabel". If you want to see "User Name" then use this add(lab1);
instead of add(lab1 = new JLabel("add JLabel"));
.
May be you just require this:
JLabel lab1 = new JLabel("User Name", JLabel.LEFT);
setLayout(new FlowLayout());
add(lab1);
Also constructor can not have return type so remove void from your constructor.
The constructor that you are using is not a proper constructor .. A java constructor does not have a return type and the void
is extra. When in the main method you are calling new Sample() it is not actually calling your method but the default constructor that exists by default.
Try like this ..
public Sample() {
JPanel p = new JPanel();
JLabel lab1 = new JLabel("User Name", JLabel.LEFT);
p.setLayout(new FlowLayout());
p.add(lab1 = new JLabel("add JLabel"));
}
also you need to do what @Harry Joy suggested to add the add(p);
statement otherwise the panel is still not added.
Note the comments.
import java.awt.*;
import javax.swing.*;
public class Sample extends JPanel {
public Sample() {
// set the layout in the constructor
super(new FlowLayout(FlowLayout.LEADING));
// best not to set size OR preferred size!
setPreferredSize( new Dimension(200,200) );
JLabel lab1 = new JLabel("User Name");
add(lab1);
}
public static void main(String[] args) {
// construct the GUI on the EDT
SwingUtilities.invokeLater( new Runnable() {
public void run() {
JFrame frame = new JFrame("User Details");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Sample());
// important!
frame.pack();
frame.setVisible(true);
}
});
}
}
Note also, that it is generally not considered a good idea to extend a component unless adding custom functionality. That would mean (for example) defining new methods for the Sample
panel (Which might better be labelled a UserDetails
or UserDetailsContainer
if I guess correctly where you are going with that code..). Or it might be a Login
component..
Sample is the Jpanel.
Sample extends JPanel means you inherit from JPanel.
just drop JPanel p and all your "p."s
@SuppressWarnings("serial") public class MyGui extends JFrame{
private MyContentPane myContentPane = new MyContentPane();
public MyGui(){
super("title");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(myContentPane);
this.createMenu();
this.pack();
this.setVisible(true);
}
private void createMenu(){
JMenuBar myMenuBar = new JMenuBar();
JMenu xMenu = new JMenu("x");
JMenu x = new JMenu("x");
JMenuItem xItem = new JMenuItem("letter");
JMenuItem exitItem = new JMenuItem("exit");
xItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
myContentPane.xPanel();
}
});
xItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
myContentPane.setxPanel();
}
});
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
displayMenu.add(letterItem);
displayMenu.add(colorItem);
fileMenu.add(exitItem);
myMenuBar.add(displayMenu);
myMenuBar.add(fileMenu);
this.setJMenuBar(myMenuBar);
}
}
精彩评论